hdu 1885 Key Task
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 990 Accepted Submission(s):
378
Problem Description
The Czech Technical University is rather old — you
already know that it celebrates 300 years of its existence in 2007. Some of the
university buildings are old as well. And the navigation in old buildings can
sometimes be a little bit tricky, because of strange long corridors that fork
and join at absolutely unexpected places.
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.
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.
Input
The 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
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.
Output
For 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.
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.
1 #include<iostream> 2 #include<stdio.h> 3 #include<cstring> 4 #include<cstdlib> 5 #include<queue> 6 using namespace std; 7 8 int n,m; 9 char a[101][101]; 10 bool dp[101][101][(1<<4)+1]; 11 int map1[4][2]={{1,0},{0,1},{0,-1},{-1,0}}; 12 struct node 13 { 14 int x,y; 15 int state; 16 int time; 17 }; 18 queue<node>Q; 19 20 int get(char cc) 21 { 22 if(cc=='B' || cc=='b')return 0; 23 else if(cc=='Y' || cc=='y')return 1; 24 else if(cc=='R' || cc=='r')return 2; 25 else return 3; 26 } 27 int bfs(int x,int y) 28 { 29 int i,x1,y1,state; 30 struct node t,cur; 31 32 t.x=x; 33 t.y=y; 34 t.time=0; 35 t.state=0; 36 dp[x][y][0]=true; 37 Q.push(t); 38 while(!Q.empty()) 39 { 40 t=Q.front(); 41 Q.pop(); 42 for(i=0;i<4;i++) 43 { 44 x1=t.x+map1[i][0]; 45 y1=t.y+map1[i][1]; 46 if(x1>=0&&x1<n && y1>=0&&y1<m && a[x1][y1]!='#') 47 { 48 if(a[x1][y1]=='X') 49 { 50 printf("Escape possible in %d steps. ",t.time+1); 51 return 1; 52 } 53 if(a[x1][y1]>='A'&&a[x1][y1]<='Z') 54 { 55 state=(1<<get(a[x1][y1])); 56 if( (t.state&state)==state && dp[x1][y1][t.state]==false) 57 { 58 dp[x1][y1][t.state]=true; 59 cur=t; 60 cur.time++; 61 cur.x=x1; 62 cur.y=y1; 63 Q.push(cur); 64 } 65 } 66 else if(a[x1][y1]>='a'&&a[x1][y1]<='z') 67 { 68 state=(1<<get(a[x1][y1])); 69 cur=t; 70 cur.state=(cur.state|state); 71 if(dp[x1][y1][cur.state]==false) 72 { 73 dp[x1][y1][cur.state]=true; 74 cur.x=x1; 75 cur.y=y1; 76 cur.time++; 77 Q.push(cur); 78 } 79 } 80 else if(dp[x1][y1][t.state]==false) 81 { 82 dp[x1][y1][t.state]=true; 83 cur.x=x1; 84 cur.y=y1; 85 cur.state=t.state; 86 cur.time=t.time+1; 87 Q.push(cur); 88 } 89 } 90 } 91 } 92 return -1; 93 } 94 int main() 95 { 96 int i,j,x,y,k; 97 bool cur; 98 while(scanf("%d%d",&n,&m)>0) 99 { 100 if(n==0&&m==0)break; 101 memset(dp,false,sizeof(dp)); 102 while(!Q.empty()) 103 { 104 Q.pop(); 105 } 106 for(i=0;i<n;i++) 107 scanf("%s",a[i]); 108 cur=false; 109 for(i=0;i<n;i++) 110 { 111 for(j=0;j<m;j++) 112 { 113 if(a[i][j]=='*') 114 { 115 x=i; 116 y=j; 117 } 118 if(a[i][j]=='X') 119 cur=true; 120 } 121 } 122 if(cur==false) printf("The poor student is trapped! "); 123 else 124 { 125 k=bfs(x,y); 126 if(k==-1)printf("The poor student is trapped! "); 127 } 128 } 129 return 0; 130 }