三维迷宫问题,就是BFS. 简单测试后,结果都没有问题,但提交却WA. 反复检查,原来是因为输出漏了一个句号!!!囧.... #include <iostream> #include <deque> using namespace std; typedef struct _cube { bool bIsCube; int x; int y; int z; _cube(bool b, int tx, int ty, int tz) { bIsCube = b; x = tx; y = ty; z = tz; } _cube() { } }Cube; int main() { int L = 0, R = 0, C = 0; while (1) { cin >> L >> R >> C; if (L == 0 && R == 0 && C == 0) { break; } else { char dungeon[30][30][30] ={0}; Cube S, E; for (int i = 0; i < L; i++) { for (int j = 0; j < R; j++) { for (int k = 0; k < C ; k++) { cin >> dungeon[i][j][k]; if (dungeon[i][j][k] == 'S') { S.bIsCube = true; S.x = k; S.y = j; S.z = i; } else if (dungeon[i][j][k] == 'E') { E.bIsCube = true; E.x = k; E.y = j; E.z = i; } } } } int min = 0; deque<Cube> buffer; Cube end(false, 0, 0, 0); buffer.push_back(S); dungeon[S.z][S.y][S.x] = '#'; buffer.push_back(end); bool bExit = false; while (!buffer.empty()) { Cube tmp = buffer.front(); buffer.pop_front(); if (tmp.bIsCube == false) { min++; if (buffer.empty()) { break; } else { buffer.push_back(end); continue; } } else { if (tmp.x == E.x && tmp.y == E.y && tmp.z == E.z) { bExit = true; break; } int dir[6][3] = {{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}}; for (int i = 0; i < 6 ; i++) { int tx = tmp.x + dir[i][0]; int ty = tmp.y + dir[i][1]; int tz = tmp.z + dir[i][2]; if (tx >= 0 && tx < C && ty >= 0 && ty < R && tz >= 0 && tz < L && dungeon[tz][ty][tx] != '#') { Cube newCube(true, tx, ty, tz); buffer.push_back(newCube); dungeon[newCube.z][newCube.y][newCube.x] = '#'; } } } } if (bExit == true) { cout << "Escaped in " << min << " minute(s)." << endl; } else { cout << "Trapped!" << endl; } } } return 0; }