Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 36879 | Accepted: 14080 |
Description
You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled with rock. It takes one minute to move one unit north, south, east, west, up or down. You cannot move diagonally and the maze is surrounded by solid rock on all sides.
Is an escape possible? If yes, how long will it take?
Is an escape possible? If yes, how long will it take?
Input
The input consists of a number of dungeons. Each dungeon description starts with a line containing three integers L, R and C (all limited to 30 in size).
L is the number of levels making up the dungeon.
R and C are the number of rows and columns making up the plan of each level.
Then there will follow L blocks of R lines each containing C characters. Each character describes one cell of the dungeon. A cell full of rock is indicated by a '#' and empty cells are represented by a '.'. Your starting position is indicated by 'S' and the exit by the letter 'E'. There's a single blank line after each level. Input is terminated by three zeroes for L, R and C.
L is the number of levels making up the dungeon.
R and C are the number of rows and columns making up the plan of each level.
Then there will follow L blocks of R lines each containing C characters. Each character describes one cell of the dungeon. A cell full of rock is indicated by a '#' and empty cells are represented by a '.'. Your starting position is indicated by 'S' and the exit by the letter 'E'. There's a single blank line after each level. Input is terminated by three zeroes for L, R and C.
Output
Each maze generates one line of output. If it is possible to reach the exit, print a line of the form
where x is replaced by the shortest time it takes to escape.
If it is not possible to escape, print the line
Escaped in x minute(s).
where x is replaced by the shortest time it takes to escape.
If it is not possible to escape, print the line
Trapped!
Sample Input
3 4 5 S.... .###. .##.. ###.# ##### ##### ##.## ##... ##### ##### #.### ####E 1 3 3 S## #E# ### 0 0 0
Sample Output
Escaped in 11 minute(s). Trapped!
Source
单纯的一道BFS题
1 #include<cstdio> 2 #include<cstring> 3 #include<queue> 4 using namespace std; 5 char tmp[33][33][33]; 6 bool map[33][33][33],vis[33][33][33]; 7 int L,R,C,step; 8 struct Point{ 9 int l,r,c,step; 10 }S,E,now,next; 11 int dl[6]={-1,+1,0,0,0,0}; 12 int dr[6]={0,0,0,0,-1,+1}; 13 int dc[6]={0,0,-1,+1,0,0}; 14 int bfs() 15 { 16 queue<Point> q; 17 step=0; 18 q.push(S); 19 vis[S.l][S.r][S.c]=1; 20 while(!q.empty()) 21 { 22 now=q.front(); q.pop(); 23 if(now.l==E.l && now.r==E.r && now.c==E.c) return now.step; 24 for(int i=0;i<6;i++) 25 { 26 next.l=now.l+dl[i], next.r=now.r+dr[i], next.c=now.c+dc[i], next.step=now.step+1; 27 if(!map[next.l][next.r][next.c] && !vis[next.l][next.r][next.c]){ 28 q.push(next); 29 vis[next.l][next.r][next.c]=1; 30 } 31 } 32 } 33 return -1; 34 } 35 int main() 36 { 37 while(scanf("%d%d%d",&L,&R,&C) && L!=0) 38 { 39 memset(map,1,sizeof(map)); 40 memset(vis,0,sizeof(vis)); 41 for(int l=1;l<=L;l++){ 42 for(int r=1;r<=R;r++){ 43 scanf("%s",tmp[l][r]+1); 44 } 45 } 46 for(int l=1;l<=L;l++){ 47 for(int r=1;r<=R;r++){ 48 for(int c=1;c<=C;c++){ 49 map[l][r][c]=(tmp[l][r][c]=='#'); 50 if(tmp[l][r][c]=='S') S.l=l, S.r=r, S.c=c, S.step=0; 51 if(tmp[l][r][c]=='E') E.l=l, E.r=r, E.c=c; 52 } 53 } 54 } 55 int ans=bfs(); 56 if(ans==-1) printf("Trapped! "); 57 else printf("Escaped in %d minute(s). ",ans); 58 } 59 }
一定要在push(next)的时候就进行标记,否则队列里会有许多重复的点,会MLE。