Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 24311 | Accepted: 9425 |
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!
题目链接:poj2251
/*题目大意:l,r,c。表示l层,r行,c列。从起点s到终点e的最小步数、其中.为通道,#为墙。只能上下左右走,也能从上一层跳到相对应的下一层 算法分析:三维bfs搜索 坑点:每次处理完后要清空队列中所有元素 */ #include <iostream> #include <cstdio> #include <cstring> #include <string> #include <algorithm> #include <cstdlib> #include <queue> using namespace std; int L, R, C; int vis[40][40][40]; int l[6] = {1, -1, 0, 0, 0, 0}, r[6] = {0, 0, 1, -1, 0, 0}, c[6] = {0, 0, 0, 0, 1, -1}; char map[40][40][40]; struct node { int z, x, y; int time; }; node start, end; queue <node> q; int judge(int a, int b, int c) { if (a>=0 && a<L && b>=0 && b<R && c>=0 && c<C) return 1; return 0; } int dfs() { while (!q.empty()) { node cur, next; cur = q.front(); q.pop(); if (cur.z == end.z && cur.x == end.x && cur.y == end.y && judge(cur.z,cur.x,cur.y)) return cur.time; for (int i = 0; i<6; i++) { next.z = cur.z + l[i]; next.x = cur.x + r[i]; next.y = cur.y + c[i]; next.time = cur.time + 1; if (judge(next.z, next.x, next.y) && map[next.z][next.x][next.y] != '#' && !vis[next.z][next.x][next.y]) { if (next.z == end.z && next.x == end.x && next.y == end.y) return next.time; q.push(next); vis[next.z][next.x][next.y] = 1; } } } return -1; } int main() { while (cin >> L >> R >> C && (L+R+C)) { memset(map, 0, sizeof(map)); memset(vis, 0, sizeof(vis)); for (int i = 0; i<L; i++) { for (int j = 0; j<R; j++) { for (int k = 0; k<C; k++) { cin >> map[i][j][k]; if (map[i][j][k] == 'S') { start.z = i; start.x = j; start.y = k; start.time = 0; vis[i][j][k] = 1; q.push(start); } else if (map[i][j][k] == 'E') { end.z = i; end.x = j; end.y = k; } } } } int ans = dfs(); if (ans == -1) cout << "Trapped!" << endl; else cout << "Escaped in " << ans << " minute(s)."<< endl; while (!q.empty()) q.pop(); } return 0; }