传送门:http://poj.org/problem?id=2251
三维的走迷宫,实际上跟二维差不多,就是把坐标放进队列里面时候编码会卡住,其他的话跟二维没啥区别
#include <iostream>
#include <cstring>
#include <math.h>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
using namespace std;
const int N = 30;
int maps[N][N][N];
int fa[N][N][N];
int dis[N][N][N];
int d[6][3] = {{-1,0,0},{1,0,0},{0,-1,0},{0,1,0},{0,0,-1},{0,0,1}};
int queues[N*N*N];
int su,eu;
int L,R,C;
int bfs()
{
int fronts = 0,rear = 1;
int z1 = su/900,x1 = (su%900)/30,y1 = (su%900)%30;
fa[z1][x1][y1] = 0;queues[fronts] = su;
dis[z1][x1][y1] = 1;
while(rear>fronts){
int u = queues[fronts++];
if(u == eu){
return dis[eu/900][(eu%900)/30][(eu%900)%30];
}
int z = u/900,x = (u%900)/30,y = (u%900)%30;
for(int i = 0;i<6;i++){
int newx = x + d[i][0];
int newy = y + d[i][1];
int newz = z + d[i][2];
if(newz >= 0 && newz <L && newx >= 0 && newx <R && newy >= 0 && newy <C &&
!maps[newz][newx][newy] && !dis[newz][newx][newy]){
queues[rear++] = newz*900 + newx*30 + newy;
fa[newz][newx][newy] = u;
dis[newz][newx][newy] = dis[z][x][y] + 1;
}
}
}
return -1;
}
int main()
{
char s;
char word[N];
//freopen("in.txt","r",stdin);
while(scanf("%d%d%d",&L,&R,&C) == 3){
if(!L&&!R&&!C)break;
s = getchar();
memset(dis,0,sizeof(dis));
for(int i = 0;i<L;i++){
for(int j = 0;j<R;j++){
cin >> word;
for(int k = 0;k<C;k++){
if(word[k] == '#')maps[i][j][k] = 1;
else if(word[k] == '.')maps[i][j][k] = 0;
else if(word[k] == 'S'){su = i*900 + j*30 + k;}
else if(word[k] == 'E'){maps[i][j][k] = 0;eu = i*900 + j*30 + k;}///编码存放
}
}
//s = getchar();
}
int bu = bfs();
if(bu<0)cout << "Trapped!" << endl;
else printf("Escaped in %d minute(s).\n",bu-1);
}
return 0;
}