You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled with rock. It takes one minute to move one unit north, south, east, west, up or down. You cannot move diagonally and the maze is surrounded by solid rock on all sides.
Is an escape possible? If yes, how long will it take?
Input
The input consists of a number of dungeons. Each dungeon description starts with a line containing three integers L, R and C (all limited to 30 in size).
L is the number of levels making up the dungeon.
R and C are the number of rows and columns making up the plan of each level.
Then there will follow L blocks of R lines each containing C characters. Each character describes one cell of the dungeon. A cell full of rock is indicated by a '#' and empty cells are represented by a '.'. Your starting position is indicated by 'S' and the exit by the letter 'E'. There's a single blank line after each level. Input is terminated by three zeroes for L, R and C.
Output
Each maze generates one line of output. If it is possible to reach the exit, print a line of the form
Escaped in x minute(s).
where x is replaced by the shortest time it takes to escape.
If it is not possible to escape, print the line
Trapped!
Sample Input
3 4 5 S.... .###. .##.. ###.# ##### ##### ##.## ##... ##### ##### #.### ####E 1 3 3 S## #E# ### 0 0 0
Sample Output
Escaped in 11 minute(s). Trapped!
题目大意:
地牢大师(感觉像是一款游戏啊.......)
你被困在一个3D的地牢里面,并且需要发现最快的出去的路,这个地牢由很多小立方体组成,有的是空的可以走,有的被岩石填充了不可以走,移动一下要花费1分钟的时间(可以向前后左右上下移动),不能对角移动和移动到迷宫外面,因为迷宫四周都是有岩石包围的。
这是一个逃亡的问题,你需要花费多长时间呢?
//
简直就是广搜的模板题......直接上吧
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
const int maxn=101;
struct st
{
int x,y,z;
int step;
};
char mp[maxn][maxn][maxn];
int dir[6][3]= {{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}};
int n,m,h;
int dfs(st e,st s)
{
queue<st>q;
q.push(e);
while(q.size())
{
e=q.front();
q.pop();
if(e.x==s.x&&e.y==s.y&&e.z==s.z)
{
return e.step;
}
for(int i=0; i<6; i++)
{
st t=e;
t.x+=dir[i][0];
t.y+=dir[i][1];
t.z+=dir[i][2];
t.step+=1;
if(t.x>=0&&t.x<h&&t.y>=0&&t.y<n&&t.z>=0&&t.z<m&&mp[t.x][t.y][t.z]!='#')
{
mp[t.x][t.y][t.z]='#';
q.push(t);
}
}
}
return -1;
}
int main()
{
while(scanf("%d%d%d",&h,&n,&m),m+n+h)
{
st s,e;
for(int i=0; i<h; i++)
for(int j=0; j<n; j++)
{
scanf("%s",mp[i][j]);
for(int k=0; k<m; k++)
{
if(mp[i][j][k]=='S')
{
e.x=i;
e.y=j;
e.z=k;
e.step=0;
}
if(mp[i][j][k]=='E')
{
s.x=i;
s.y=j;
s.z=k;
}
}
}
int ans=dfs(e,s);
if(ans!=-1)printf("Escaped in %d minute(s).\n",ans);
else printf("Trapped!\n");
}
return 0;
}
无忧望月
#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[6][3] = { {1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1} };
char G[maxn][maxn][maxn];
int OK(int z, int x, int y)//判断这点是否可以走
{
if(z>=0&&z<h && x>=0&&x<m && y>=0&&y<n && G[z][x][y] != '#')
return 1;
return 0;
}
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=0; i<6; i++)
{
node q = s;
q.x += dir[i][0];
q.y += dir[i][1];
q.z += dir[i][2];
q.step += 1;
if(OK(q.z, q.x, q.y) == 1)
{
G[q.z][q.x][q.y] = '#';
Q.push(q);
}
}
}
return -1;
}
int main()
{
while(scanf("%d%d%d", &h, &m, &n), h+m+n)
{
int i, j, k;
node s, e;
for(i=0; i<h; i++)
for(j=0; j<m; j++)
{
scanf("%s", G[i][j]);
for(k=0; k<n; k++)
{
if(G[i][j][k] == 'S')
{
s.z = i;
s.x = j;
s.y = k;
s.step = 0;
}
if(G[i][j][k] == 'E')
{
e.z = i;
e.x = j;
e.y = k;
}
}
}
int ans = DFS(s, e);
if(ans != -1)
printf("Escaped in %d minute(s).\n", ans);
else
printf("Trapped!\n");
}
return 0;
}