题意:走3D迷宫,找到出口
思想:BFS,六个方向一起搜
#include<iostream>
#include<stdio.h>
#include<string.h>
#define N 50
using namespace std;
int vis[N][N][N], Map[N][N][N];
char map[N][N][N];
int a, n, m, num, c1, c2, c3, d1, d2, d3, flag;
int dz[] = {0, 0, 0, 0, -1, 1};
int dx[] = {-1, 1, 0, 0, 0, 0};
int dy[] = {0, 0, -1, 1, 0, 0};
typedef struct space{
int z, x, y, d;
};
void bfs(int z, int x, int y){
int front = 0, near = 1;
space s[30000];
memset(s, 0, sizeof(s));
s[0].z = z;
s[0].x = x;
s[0].y = y;
s[0].d = 0;
vis[z][x][y] = 1;
while(front < near){
for(int i = 0; i < 6; i++)
{
int tz = s[front].z + dz[i];
int tx = s[front].x + dx[i];
int ty = s[front].y + dy[i];
if(Map[tz][tx][ty]){
if(tz >= 0 && tz < a && tx >= 0 && tx < n && ty >= 0 && ty < m && !vis[tz][tx][ty])
{ if(tz == c1 && tx == c2 && ty == c3)
{
num = s[front].d + 1;
flag = 0;
return;
}
vis[tz][tx][ty] = 1;
s[near].z = tz;
s[near].x = tx;
s[near].y = ty;
s[near].d = s[front].d + 1;
near++;
}
}
}
front++;
}
}
int main(){
while(cin>>a>>n>>m && a + n + m){
flag = 1;
memset(vis, 0, sizeof(vis));
memset(Map, 0, sizeof(Map));
for(int i = 0; i < a; i++)
for(int j = 0; j < n; j++)
for(int k = 0; k < m; k++)
cin>>map[i][j][k];
for(int i = 0; i < a; i++)
for(int j = 0; j < n; j++)
for(int k = 0; k < m; k++)
{
if(map[i][j][k] == 'E'){
c1 = i;
c2 = j;
c3 = k;
Map[i][j][k] = 1;
}
else if(map[i][j][k] == 'S')
{
Map[i][j][k] = 1;
d1 = i;
d2 = j;
d3 = k;
}
else if(map[i][j][k] == '.')
Map[i][j][k] = 1;
}
if(c1 != d1 || c2 != d2 || c3 != d3)
bfs(d1, d2, d3);
if(flag)
printf("Trapped!\n");
else
printf("Escaped in %d minute(s).\n", num);
}
return 0;
}