Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 14103 | Accepted: 5477 |
Description
Input
Output
Escaped in x minute(s).
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
#include<stdio.h> #include<string.h> #include<queue> #include<iostream> using namespace std; const int maxn = 40; int map[maxn][maxn][maxn]; int vis[maxn][maxn][maxn]; char str[maxn]; int L,R,C; int flag; int step; int dir[6][3] = {1,0,0, 0,0,1, 0,1,0, 0,0,-1, 0,-1,0, -1,0,0}; struct Point{ int x,y,z; int step; }st,en; void bfs(int x, int y, int z) { queue<Point> q; while(!q.empty()) q.pop(); //清空队列 Point now,next; now.x = x; now.y = y; now.z = z; now.step = 0; q.push(now); //起点入队 memset(vis, 0, sizeof(vis)); vis[x][y][z] = 1; while(!q.empty()) { now = q.front(); q.pop(); if(now.x == en.x && now.y == en.y && now.z == en.z) { flag = 1; //标记找到 en.step = now.step; // return; } for(int i = 0; i < 6; i++) { next.x = now.x+dir[i][0]; next.y = now.y+dir[i][1]; next.z = now.z+dir[i][2]; if(map[next.x][next.y][next.z] && !vis[next.x][next.y][next.z]) { vis[next.x][next.y][next.z] = 1; next.step = now.step+1; q.push(next); if(next.x == en.x && next.y == en.y && next.z == en.z) { flag = 1; en.step = next.step; return; } } } } return; } int main() { while(scanf("%d%d%d", &L,&R,&C) != EOF) { if(L == 0 && R == 0 && C == 0) break; memset(map,0,sizeof(map)); gets(str); char c; for(int i = 1; i <= L; i++) { for(int j = 1; j <= R; j++) { for(int k = 1; k <= C; k++) { scanf("%c", &c); if(c == 'S') { st.x = i; st.y = j; st.z = k; } if(c == 'E') { en.x = i; en.y = j; en.z = k; } if(c != '#') map[i][j][k] = 1; //标记可以走标记为 1,避免了判断边界问题 } gets(str); } gets(str); } flag = 0; //初始化 bfs(st.x, st.y, st.z); if(flag == 0) printf("Trapped!\n"); else printf("Escaped in %d minute(s).\n", en.step); } return 0; }