題目大意:
地牢大師(感覺像是一款遊戲啊.......)
你被困在一個3D的地牢裡面,並且需要發現最快的出去的路,這個地牢由很多小立方體組成,有的是空的可以走,有的被岩石填充了不可以走,移動一下要花費1分鐘的時間(可以向前後左右上下移動),不能對角移動和移動到迷宮外面,因為迷宮四周都是有岩石包圍的。
這是一個逃亡的問題,你需要花費多長時間呢?
//////////////////////////////////////////////////////
簡直就是廣搜的模板題......直接上吧,1A不錯不錯
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std; #define maxn 100 struct node
{
int x, y, z, step;
}; int h, m, n;//高,長和寬
//6個方向可以走
int dir[][] = { {,,},{-,,},{,,},{,-,},{,,},{,,-} };
char G[maxn][maxn][maxn]; int OK(int z, int x, int y)//判斷這點是否可以走
{
if(z>=&&z<h && x>=&&x<m && y>=&&y<n && G[z][x][y] != '#')
return ;
return ;
}
int DFS(node s, node e)
{
queue<node> Q;
Q.push(s); while(Q.size())
{
s = Q.front();Q.pop(); if(s.x==e.x&&s.y==e.y&&s.z==e.z)
return s.step; for(int i=; i<; i++)
{
node q = s;
q.x += dir[i][];
q.y += dir[i][];
q.z += dir[i][];
q.step += ; if(OK(q.z, q.x, q.y) == )
{
G[q.z][q.x][q.y] = '#';
Q.push(q);
}
}
} return -;
} int main()
{
while(scanf("%d%d%d", &h, &m, &n), h+m+n)
{
int i, j, k;
node s, e; for(i=; i<h; i++)
for(j=; j<m; j++)
{
scanf("%s", G[i][j]);
for(k=; k<n; k++)
{
if(G[i][j][k] == 'S')
{
s.z = i;
s.x = j;
s.y = k;
s.step = ;
}
if(G[i][j][k] == 'E')
{
e.z = i;
e.x = j;
e.y = k;
}
}
} int ans = DFS(s, e); if(ans != -)
printf("Escaped in %d minute(s).\n", ans);
else
printf("Trapped!\n");
} return ;
}