搜索小练#2
#include <iostream>
#include <cstring>
#include <queue>
#define N 110
using namespace std;
int l, r, c, sx, sy, sz;
char a[N][N][N];
int vis[N][N][N];
struct node {int x, y, z, cnt;};
int dir[6][3] = {{0, 0, 1}, {0, 0, -1}, {0, 1, 0}, {0, -1, 0}, {1, 0, 0}, {-1, 0, 0}};
inline void bfs () {
queue<node>Q; node t;
vis[sx][sy][sz] = 1;
t.x = sx, t.y = sy, t.z = sz, t.cnt = 0;
Q.push (t);
while (Q.size()) {
node nows = Q.front(); Q.pop();
int nx = nows.x, ny = nows.y, nz = nows.z, ncnt = nows.cnt;
if (a[nx][ny][nz] == 'E') {cout << "Escaped in "<< ncnt <<" minute(s)."<< '\n';return;}
for (int i = 0; i < 6; ++ i) {
node T;
T.x = nx + dir[i][0], T.y = ny + dir[i][1], T.z = nz + dir[i][2];
T.cnt = ncnt + 1;
if (T.x > l || T.y > r || T.z > c || T.x < 1 || T.y < 1 || T.z < 1) continue;
if (a[T.x][T.y][T.z] != '#' && !vis[T.x][T.y][T.z]) {
vis[T.x][T.y][T.z] = 1;
Q.push (T);
}
}
}
cout << "Trapped!\n";
}
int main () {
while (cin >> l >> r >> c) {
if (l == 0 && r == 0 && c == 0) break;
memset (vis, 0, sizeof vis);
for (int i = 1; i <= l; ++ i)
for (int j = 1; j <= r; ++ j)
for (int k = 1; k <= c; ++ k) {
cin >> a[i][j][k];
if (a[i][j][k] == 'S') sx = i, sy = j, sz = k;
}
bfs ();
}
return 0;
}
/*
3 4 5
S....
.###.
.##..
###.#
#####
#####
##.##
##...
#####
#####
#.###
####E
1 3 3
S##
#E#
###
0 0 0
*/
PS:
1>\(debug\)完记得清理干净
2>遇到bfs就写队列
3>模拟三维的方向:
int dir[6][3] = {{0, 0, 1}, {0, 0, -1}, {0, 1, 0}, {0, -1, 0}, {1, 0, 0}, {-1, 0, 0}};

浙公网安备 33010602011771号