1313: Dungeon Master
Result TIME Limit MEMORY Limit Run Times AC Times JUDGE
3s 8192K 305 120 Standard
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 Specification
The input file 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 Specification
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!
这是我做的第一个广度优先搜索的一个题,这个题目比较繁琐要从上下前后左右进行搜索。我第一次是用深度优先搜索做的结果超时了。
跟一般广搜的题目一样用队列进行搜索,注意别忘了回溯!
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
class node
{
public:
node(int i=0,int j=0,int k=0,int s=0):
x(i),y(j),z(k),step(s){}
int x,y,z;
int step;
};
char Graph[35][35][35];
int visited[35][35][35];
int l,r,c;
int bfs(int i,int j,int k)
{
node start(i,j,k);
queue<node>q;
q.push(start);
visited[i][j][k]=1;
while(!q.empty())
{
node temp;
temp=q.front();
q.pop();
//printf("%d %d %d %d\n",temp.x,temp.y,temp.z,temp.step);
if(Graph[temp.x][temp.y][temp.z]=='E')
return temp.step;
else
{
//up
if(temp.z+1<=c&&
!visited[temp.x][temp.y][temp.z+1]
&&Graph[temp.x][temp.y][temp.z+1]!='#'
)
{
temp.z++;
temp.step++;
q.push(temp);
temp.step--;
visited[temp.x][temp.y][temp.z]=1;
temp.z--;
}
//down
if(temp.z-1>0&&
!visited[temp.x][temp.y][temp.z-1]
&&Graph[temp.x][temp.y][temp.z-1]!='#'
)
{
temp.z--;
temp.step++;
q.push(temp);
temp.step--;
visited[temp.x][temp.y][temp.z]=1;
temp.z++;
}
//left
if(temp.x-1>0&&
!visited[temp.x-1][temp.y][temp.z]
&&Graph[temp.x-1][temp.y][temp.z]!='#'
)
{
temp.x--;
temp.step++;
q.push(temp);
temp.step--;
visited[temp.x][temp.y][temp.z]=1;
temp.x++;
}
//right
if(temp.x+1<=l&&
!visited[temp.x+1][temp.y][temp.z]
&&Graph[temp.x+1][temp.y][temp.z]!='#'
)
{
temp.x++;
temp.step++;
q.push(temp);
temp.step--;
visited[temp.x][temp.y][temp.z]=1;
temp.x--;
}
//front
if(temp.y-1>0&&
!visited[temp.x][temp.y-1][temp.z]
&&Graph[temp.x][temp.y-1][temp.z]!='#'
)
{
temp.y--;
temp.step++;
q.push(temp);
temp.step--;
visited[temp.x][temp.y][temp.z]=1;
temp.y++;
}
//back
if(temp.y+1<=r&&
!visited[temp.x][temp.y+1][temp.z]
&&Graph[temp.x][temp.y+1][temp.z]!='#'
)
{
temp.y++;
temp.step++;
q.push(temp);
temp.step--;
visited[temp.x][temp.y][temp.z]=1;
temp.y--;
}
}
}
return 0;
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
while(scanf("%d%d%d",&l,&r,&c),l&&r&&c)
{
getchar();
memset(visited,0,sizeof(visited));
int startx,starty,startz;
for(int i=1;i<=l;i++)
{
for(int j=1;j<=r;j++)
{
for(int k=1;k<=c+1;k++)
{
scanf("%c",&Graph[i][j][k]);
if(Graph[i][j][k]=='S')
{
startx=i;starty=j;startz=k;
}
}
}
getchar();
}
/*for(int i=1;i<=l;i++)
{
for(int j=1;j<=r;j++)
{
for(int k=1;k<=c;k++)
printf("%c",Graph[i][j][k]);
printf("\n");
}
putchar('\n');
}*/
int min=bfs(startx,starty,startz);
if(min==0)printf("Trapped!\n");
else printf("Escaped in %d minute(s).\n",min);
}
return 0;
}