题目看起来很厉害,实际上看懂了并不难,开一个三维的数组,这里需要注意的是第一维是高度,然后就是简单的BFS了,还有不同就是三维的时候有六个方向可以走,在前后左右的基础上多了一个向上和向下的走法,还有一个问题就是多个输入样例要注意每次都要初始化,我做的时候就因为这个WA了好几次,最后在学姐的帮助下才改出来,最后,附代码如下:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
char mp[50][50][50];
int step[50][50][50],vis[50][50][50];
int mv[6][3]={{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}};
int l,r,c,sx,sy,sz,ex,ey,ez;
struct node
{
int z,x,y;
};
void bfs()
{
memset(vis,0,sizeof(vis));
node que[100010];
int frt = 0,til = 0;
que[til].x = sx;
que[til].y = sy;
que[til++].z = sz;
step[sz][sx][sy] = 0;
vis[sz][sx][sy] = 1;
while(frt != til)
{
node p = que[frt++];
int next_x,next_y,next_z;
for(int i = 0;i < 6; i++)
{
next_x = p.x + mv[i][0];
next_y = p.y + mv[i][1];
next_z = p.z + mv[i][2];
if(next_x >= 0 && next_x < r && next_y >= 0 && next_y < c && next_z >= 0 && next_z < l && mp[next_z][next_x][next_y] != '#' && !vis[next_z][next_x][next_y])
{
que[til].x = next_x;
que[til].y = next_y;
que[til++].z = next_z;
step[next_z][next_x][next_y] = step[p.z][p.x][p.y] + 1;
vis[next_z][next_x][next_y] = 1;
}
if(next_x == ex && next_y == ey && next_z == ez)
break;
}
}
}
int main()
{
while(1)
{
scanf("%d %d %d",&l,&r,&c);
if(l == 0 && r == 0 && c == 0)
break;
for(int i = 0;i < l; i++)
{
for(int j = 0;j < r; j++)
{
scanf("%s",mp[i][j]);
for(int k = 0;k < c; k++)
{
if(mp[i][j][k] == 'S')
{
sx = j;
sy = k;
sz = i;
continue;
}
if(mp[i][j][k] == 'E')
{
ex = j;
ey = k;
ez = i;
}
}
}
}
bfs();
if(step[ez][ex][ey] != 0)
printf("Escaped in %d minute(s).\n",step[ez][ex][ey]);
else
printf("Trapped!\n");
}
return 0;
}
标签:Dungeon,til,int,50,next,BFS,Master,&&,que From: https://blog.51cto.com/u_16131191/6356129