再多组数据情况下,bfs忘记清空队列了!!!
#include <iostream>
#include <queue>
#include <cstring>
using namespace std;
const int N = 35;
char g[N][N][N];
int n, m, k;
int sx, sy, sz, ex, ey, ez;
struct Node{
int x, y, z;
};
queue<Node> q;
int dist[N][N][N];
int bfs()
{
//清空队列
queue<Node> tpq;
q = tpq;
memset(dist, 0x3f, sizeof dist);
dist[sx][sy][sz] = 0;
q.push(Node{sx, sy, sz});
while(q.size())
{
Node t = q.front();
q.pop();
int dx[] = {0, 0, 1, -1, 0, 0}, dy[] = {0, 0, 0, 0, 1, -1}, dz[] = {1, -1, 0, 0, 0, 0};
for(int i = 0; i < 6; ++ i)
{
int a = t.x + dx[i], b = t.y + dy[i], c = t.z + dz[i];
if(a < 0 || a >= n || b < 0 || b >= m || c < 0 || c >= k) continue;
if(g[a][b][c] == '#') continue;
if(dist[a][b][c] != 0x3f3f3f3f) continue; //访问过
dist[a][b][c] = dist[t.x][t.y][t.z] + 1;
if(a == ex && b == ey && c == ez)
{
return dist[a][b][c]; //队列没清空
}
q.push(Node{a, b, c});
}
}
return -1;
}
int main()
{
while(cin >> n >> m >> k && (n && m && k))
{
for(int i = 0; i < n; ++ i)
for(int j = 0; j < m; ++ j)
{
cin >> g[i][j];
for(int t = 0; t < k; ++ t)
{
if(g[i][j][t] == 'S')
{
sx = i, sy = j, sz = t;
}
else if(g[i][j][t] == 'E')
{
ex = i, ey = j, ez = t;
}
}
}
int t = bfs();
if(t == -1)
{
cout << "Trapped!" << endl;
}
else
{
cout << "Escaped in " << t << " minute(s)." << endl;
}
}
return 0;
}