题意:输入3个数,代表层数、行数、列数。即3维数组大小,输入以0 0 0为结束。接下来输入迷宫,S表示起点,E表示终点,'.'是可以走的路,'#'是不能走的路。 求从S走到E所需步数,按照题目要求输出。
简单BFS题。只不过地图变成了3维,从原先的4个方向变成了6个方向。统计步数就ok啦~ 刚入门BFS,代码是参照了一个大神的。不懂的地方可以看注释~写的还是比较详细的
Dungeon Master
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 20490 | Accepted: 7937 |
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.
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
where x is replaced by the shortest time it takes to escape.
If it is not possible to escape, print the line
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!
附上AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
char maze[35][35][35]; //记录地图
int vis[35][35][35]; //标记是否已访问
int k,n,m,sx,sy,sz,ex,ey,ez;//输入的行列高,以及起始点坐标和终止点坐标
int to[6][3]={{0,0,1},{0,0,-1},{0,1,0},{0,-1,0},{1,0,0},{-1,0,0}};
struct node //坐标点,存放坐标以及步数
{
int x,y,z,step;
};
int check(int x,int y,int z) //写了一个check函数。判断该点是否可行。不可行返回1,可行返回0
{
if(x<0 || y<0 || z<0 || x>=k || y>=n || z>=m) //是否越出地图边界
return 1;
else if(maze[x][y][z]=='#') //该点在地图上是否可行
return 1;
else if(vis[x][y][z]) //该点是否已访问
return 1;
return 0;
}
int bfs()
{
node cur,next; //cur表示当前点,next表示接下来要走的点
queue<node> Q;
cur.x=sx; //当前点初始化。
cur.y=sy;
cur.z=sz;
cur.step=0; //步数初始化0
vis[sx][sy][sz]=1; //将该点设为已访问。
Q.push(cur); //cur入队
while(!Q.empty()) //如果队列非空
{
cur=Q.front();
Q.pop(); //出队
if(cur.x==ex && cur.y==ey && cur.z==ez) //如果到达终点,返回步数
return cur.step;
for(int i=0;i<6;i++)
{
next=cur;
next.x=cur.x+to[i][0];
next.y=cur.y+to[i][1];
next.z=cur.z+to[i][2];
if(check(next.x,next.y,next.z)) //如果next点不可行,继续走下个方向。
continue;
vis[next.x][next.y][next.z]=1; //若可行,走该点,并标记为已访问。
next.step=cur.step+1; //步数+1.
Q.push(next); //入队
}
}
return 0;
}
int main()
{
int i,j,l;
while(scanf("%d%d%d",&k,&n,&m)!=EOF)
{
if(k==0 || n==0 || m==0)
break;
for(i=0;i<k;i++)
{
for(j=0;j<n;j++)
{
scanf("%s",maze[i][j]); //输入迷宫
for(l=0;l<m;l++)
{
if(maze[i][j][l]=='S') //定位S点坐标
{
sx=i;
sy=j;
sz=l;
}
else if(maze[i][j][l]=='E') //定位E点坐标
{
ex=i;
ey=j;
ez=l;
}
}
}
}
memset(vis,0,sizeof(vis));
int ans=bfs();
if(ans==0)
printf("Trapped!\n");
else
printf("Escaped in %d minute(s).\n",ans);
}
return 0;
}