标签:des style http io ar color os sp java
1 10 *........X 1 3 *#X 3 20 #################### #XY.gBr.*.Rb.G.GG.y# #################### 0 0
Escape possible in 9 steps. The poor student is trapped! Escape possible in 45 steps.
/************************************************************************* > File Name: 3.cpp > Author:yuan > Mail: > Created Time: 2014年11月26日 星期三 13时09分34秒 ************************************************************************/ #include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #include<queue> using namespace std; char mat[105][105]; int ans; struct node { int x,y; int num,key; }; bool vis[105][105][30]; queue<node> q; int sx,sy; int n,m,top,base; int next[4][2]={1,0,0,1,-1,0,0,-1}; void BFS() { int x1,x2,y1,y2;node p,qq; while(!q.empty()){ p=q.front(); x1=p.x;y1=p.y; if(mat[x1][y1]=='X'){ ans=p.num; return; } for(int i=0;i<4;i++) { int key; x2=x1+next[i][0];y2=y1+next[i][1]; if(x2<0||x2>=n||y2<0||y2>=m||mat[x2][y2]=='#') continue; if(mat[x2][y2]=='.'||mat[x2][y2]=='*'||mat[x2][y2]=='X') { key=p.key; if(vis[x2][y2][key]==0) { qq.x=x2;qq.y=y2;qq.num=p.num+1;qq.key=p.key; q.push(qq); vis[x2][y2][key]=1; } } else if(mat[x2][y2]=='B'||mat[x2][y2]=='Y'||mat[x2][y2]=='R'||mat[x2][y2]=='G') { key=p.key; if(vis[x2][y2][key]==0){ int d; if(mat[x2][y2]=='B') d=0; else if(mat[x2][y2]=='Y') d=1; else if(mat[x2][y2]=='R') d=2; else if(mat[x2][y2]=='G') d=3; int lock=1<<d; if(lock&key){ qq.x=x2;qq.y=y2;qq.num=p.num+1;qq.key=p.key; q.push(qq); vis[x2][y2][key]=1; } } } else if(mat[x2][y2]=='b'||mat[x2][y2]=='y'||mat[x2][y2]=='r'||mat[x2][y2]=='g') { int d; if(mat[x2][y2]=='b') d=0; else if(mat[x2][y2]=='y') d=1; else if(mat[x2][y2]=='r') d=2; else if(mat[x2][y2]=='g') d=3; key=1<<d; if(vis[x2][y2][p.key]==0){ qq.x=x2;qq.y=y2;qq.num=p.num+1;qq.key=p.key|key; q.push(qq); vis[x2][y2][p.key]=1; } } } q.pop(); } } int main() { while(1){ scanf("%d%d",&n,&m); if(n==0&&m==0) break; memset(mat,0,sizeof(mat)); memset(vis,0,sizeof(vis)); while(!q.empty()) q.pop(); for(int i=0;i<n;i++) scanf("%s",mat[i]); for(int i=0;i<n;i++) for(int j=0;j<m;j++) { if(mat[i][j]=='*') {sx=i,sy=j;} } node p; p.x=sx;p.y=sy;p.num=0;p.key=0; q.push(p); vis[sx][sy][0]=1; ans=100000000; BFS(); if(ans<100000000) printf("Escape possible in %d steps.\n",ans); else printf("The poor student is trapped!\n"); } return 0; }
标签:des style http io ar color os sp java
原文地址:http://blog.csdn.net/yuanchang_best/article/details/41541603