本质上与二位迷宫裸题是一致的
1 #include<cstdio> 2 #include<cstring> 3 #include<cstring> 4 #include<algorithm> 5 #include<iostream> 6 #include<queue> 7 using namespace std; 8 #define MAXN 33 9 10 struct node{ 11 int x, y, z; 12 int time; 13 }s,e; 14 int dx[] = { 1, -1, 0, 0, 0, 0 }; 15 int dy[] = { 0, 0, 1, -1, 0, 0 }; 16 int dz[] = { 0, 0, 0, 0, 1, -1 }; 17 18 char mp[MAXN][MAXN][MAXN]; 19 bool vis[MAXN][MAXN][MAXN]; 20 int l, r, c; 21 22 void init() 23 { 24 memset(vis,0,sizeof(vis)); 25 } 26 27 28 void input() 29 { 30 init(); 31 for (int i = 1; i <= l; ++i) 32 for (int j = 1; j <= r; ++j) 33 for (int k = 1; k <= c; ++k) { 34 cin >> mp[i][j][k]; 35 if (mp[i][j][k] == 'S') { 36 s.x = i; 37 s.y = j; 38 s.z = k; 39 s.time = 0; 40 } 41 else if (mp[i][j][k] == 'E') { 42 e.x = i; 43 e.y = j; 44 e.z = k; 45 } 46 } 47 //cout << "s.x = " << s.x << " s.y = " << s.y << " s.z = " << s.z << endl; 48 //cout << "e.x = " << e.x << " e.y = " << e.y << " e.z = " << e.z << endl; 49 } 50 51 bool judge(int i,int j,int k) 52 { 53 if (i < 1 || i > l || j < 1 || j > r || k < 1 || k > c || vis[i][j][k]) 54 return true; 55 return false; 56 } 57 58 int bfs() 59 { 60 queue<node> q; 61 q.push(s); 62 vis[s.x][s.y][s.z] = true; 63 while (!q.empty()) { 64 node now = q.front(); 65 q.pop(); 66 //cout << "x = " << now.x << " y = " << now.y << " z = " << now.z << endl; 67 if (mp[now.x][now.y][now.z] == 'E') { 68 return now.time; 69 } 70 node next; 71 for (int i = 0; i < 6; ++i) { 72 next.x = now.x + dx[i]; 73 next.y = now.y + dy[i]; 74 next.z = now.z + dz[i]; 75 if (judge(next.x, next.y, next.z)) continue; 76 if (mp[next.x][next.y][next.z] != '#') { 77 next.time = now.time + 1; 78 q.push(next); 79 vis[next.x][next.y][next.z] = true; 80 } 81 } 82 } 83 return -1; 84 } 85 86 int main() 87 { 88 while (cin >> l >> r >> c, l + r + c) { 89 input(); 90 int res = bfs(); 91 if (res == -1) puts("Trapped!"); 92 else printf("Escaped in %d minute(s). ",res); 93 } 94 return 0; 95 }