Dungeon Master (BFS)
好久没写博客了,今天写广搜,运行一次就ac了,比较开心。。
Dungeon Master
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 23 Accepted Submission(s) : 15
Problem Description
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?
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!
题意:
给出一个迷宫,‘.’代表路,‘#’代表墙,求多少步能出来,如果出不来,则输出“Trapped!”
代码如下:
#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
class point
{
public:
int x,y,z;
};
int visit[31][31][31];
char pos[31][31][31];
int main()
{
int a,b,c;
while(cin>>a>>b>>c&&a)
{
memset(visit,0,sizeof(visit));
memset(pos,0,sizeof(pos));
int step=0;
point s,e;
for(int i=0; i<a; i++)
for(int j=0; j<b; j++)
for(int k=0; k<c; k++)
{
char c;
cin>>c;
pos[i][j][k]=c;
if(c=='S')
{
s.x=i;
s.y=j;
s.z=k;
}
if(c=='E')
{
e.x=i;
e.y=j;
e.z=k;
}
}
queue<point>que;
que.push(s);
int flag=0;
visit[s.x][s.y][s.z]=1;
while(!que.empty()&&!flag)
{
int t=que.size();
step++;
while(t--)
{
point case1[6];
for(int i=0; i<6; i++)
case1[i]=que.front();
que.pop();
case1[0].x++;
case1[1].x--;
case1[2].y++;
case1[3].y--;
case1[4].z++;
case1[5].z--;
for(int i=0; i<6; i++)
{
if(case1[i].x==e.x&&case1[i].y==e.y&&case1[i].z==e.z)
{
flag=1;
break;
}
if(case1[i].x>=0&&case1[i].x<a&&case1[i].y>=0&&case1[i].y<b&&case1[i].z>=0&&case1[i].z<c&&!visit[case1[i].x][case1[i].y][case1[i].z]&&pos[case1[i].x][case1[i].y][case1[i].z]!='#')
{
que.push(case1[i]);
visit[case1[i].x][case1[i].y][case1[i].z]=1;
}
}
}
}
if(flag)
cout<<"Escaped in "<<step<<" minute(s)."<<endl;
else
cout<<"Trapped!"<<endl;
}
return 0;
}
浙公网安备 33010602011771号