HDU1885 Key Task
The result is that some first-graders have often di?culties finding the right way to their classes. Therefore, the Student Union has developed a computer game to help the students to practice their orientation skills. The goal of the game is to find the way out of a labyrinth. Your task is to write a verification software that solves this game.
The labyrinth is a 2-dimensional grid of squares, each square is either free or filled with a wall. Some of the free squares may contain doors or keys. There are four di?erent types of keys and doors: blue, yellow, red, and green. Each key can open only doors of the same color.
You can move between adjacent free squares vertically or horizontally, diagonal movement is not allowed. You may not go across walls and you cannot leave the labyrinth area. If a square contains a door, you may go there only if you have stepped on a square with an appropriate key before.
InputThe input consists of several maps. Each map begins with a line containing two integer numbers R and C (1 ≤ R, C ≤ 100) specifying the map size. Then there are R lines each containing C characters. Each character is one of the following:
Note that it is allowed to have
- more than one exit,
- no exit at all,
- more doors and/or keys of the same color, and
- keys without corresponding doors and vice versa.
You may assume that the marker of your position (“*”) will appear exactly once in every map.
There is one blank line after each map. The input is terminated by two zeros in place of the map size.OutputFor each map, print one line containing the sentence “Escape possible in S steps.”, where S is the smallest possible number of step to reach any of the exits. If no exit can be reached, output the string “The poor student is trapped!” instead.
One step is defined as a movement between two adjacent cells. Grabbing a key or unlocking a door does not count as a step.Sample Input
1 10 *........X 1 3 *#X 3 20 #################### #XY.gBr.*.Rb.G.GG.y# #################### 0 0
Sample Output
Escape possible in 9 steps. The poor student is trapped! Escape possible in 45 steps.
题解:BFS+状态压缩;我们可以记录每一种状态是否出现过来搜索,因为只有4种钥匙,故用二进制表示每一种钥匙的状态;
然后就是普通的BFS,在处理时加上钥匙的处理,以及加判是否为门,如果为门的话,判断上个位置时是否已有该门对应的钥匙,
如有,则该点入队,否则跳过;如果遇到出口,就输出步数,如到最后队列为空时都没有出去,则输出被困在里面;
参考代码为:
1 #include<bits/stdc++.h> 2 using namespace std; 3 const int N=105; 4 int n,m,sx,sy,ex,ey; 5 char g[N][N]; 6 bool vis[N][N][16]; 7 int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}}; 8 struct node{ 9 int x,y,key,step; 10 }; 11 int judged(char ch) 12 { 13 if(ch=='B')return 1<<0; 14 if(ch=='Y')return 1<<1; 15 if(ch=='R')return 1<<2; 16 if(ch=='G')return 1<<3; 17 } 18 int judgek(char ch) 19 { 20 if(ch=='b')return 1<<0; 21 if(ch=='y')return 1<<1; 22 if(ch=='r')return 1<<2; 23 if(ch=='g')return 1<<3; 24 return 0; 25 } 26 void solve() 27 { 28 memset(vis,0,sizeof(vis)); 29 queue<node>q; 30 node u,v; 31 u.x=sx;u.y=sy;u.key=0;u.step=0;vis[u.x][u.y][u.key]=1; 32 q.push(u); 33 while(!q.empty()) 34 { 35 u=q.front(); q.pop(); 36 if(g[u.x][u.y]=='X') 37 { 38 printf("Escape possible in %d steps.\n",u.step); 39 return; 40 } 41 for(int i=0;i<4;i++) 42 { 43 v.x=u.x+dir[i][0];v.y=u.y+dir[i][1];v.step=u.step+1;v.key=u.key|judgek(g[v.x][v.y]); 44 if(v.x<1||v.x>n || v.y<1||v.y>m)continue; 45 if(g[v.x][v.y]=='#' || vis[v.x][v.y][v.key])continue; 46 if(g[v.x][v.y]=='B'||g[v.x][v.y]=='Y'||g[v.x][v.y]=='R'||g[v.x][v.y]=='G') 47 { 48 if(!(u.key&judged(g[v.x][v.y]))) continue; 49 } 50 vis[v.x][v.y][v.key]=1; 51 q.push(v); 52 } 53 } 54 printf("The poor student is trapped!\n"); 55 } 56 int main() 57 { 58 while(scanf("%d%d",&n,&m)&&n+m) 59 { 60 for(int i=1;i<=n;i++) scanf("%s",g[i]+1); 61 for(int i=1;i<=n;i++) 62 { 63 for(int j=1;j<=m;j++) 64 { 65 if(g[i][j]=='*') 66 { 67 sx=i;sy=j;g[sx][sy]='.'; 68 break; break; 69 } 70 } 71 } 72 solve(); 73 } 74 return 0; 75 }