还是迷宫只是从之前的二维变成了三维而已。
#include <cstring> #include <cstdio> #include <queue> #include <iostream> using namespace std; struct test { int x,y,z; int step; }; int direction[6][3]={1,0,0, 0,1,0, 0,0,1, -1,0,0, 0,-1,0, 0,0,-1}; int main() { int xi, yi, zi; queue<test> q; test temp, start, end; while(~scanf("%d%d%d", &zi,&yi,&xi) && zi+yi+xi) { while(!q.empty()) q.pop(); char s[31][31][31]; int vis[31][31][31]; memset(vis, 0, sizeof(vis)); for(int i = 0;i < zi;i++) for(int j = 0; j< yi;j++) for(int k = 0; k<xi;k++) { cin>>s[i][j][k]; if(s[i][j][k] == 'S') { start.x = k; start.y = j; start.z = i; } else if(s[i][j][k] == 'E') { end.x = k; end.y = j; end.z = i; } } // printf("start: %d %d %d end: %d %d %d", start.z, start.y, start.x, end.z, end.y, end.x); start.step = 0; q.push(start); vis[start.z][start.y][start.x] = 1; int ans = 0; while(!q.empty()) { start = q.front(); if(start.z == end.z && start.x == end.x && start.y == end.y) {ans = start.step; break; } q.pop(); for(int i = 0; i< 6; i++) { temp.z = start.z + direction[i][0]; temp.y = start.y + direction[i][1]; temp.x = start.x + direction[i][2]; if(temp.x <xi&&temp.x>=0&&temp.y <yi&&temp.y>=0&&temp.z>=0&&temp.z<zi&&!vis[temp.z][temp.y][temp.x]&&s[temp.z][temp.y][temp.x] != '#') { temp.step = start.step+1; // printf("temp: %d %d %d %d ", temp.z, temp.y, temp.x, start.step); q.push(temp); vis[temp.z][temp.y][temp.x] = 1; } } } if(ans) printf("Escaped in %d minute(s). ", ans); else printf("Trapped! "); } return 0; }