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!
TL的,因为没有标记已经入队的节点,节点重复入队,导致超时
#include<iostream>
#include<queue>
#include<stdio.h>
#include<string.h>
using namespace std;
struct date
{
int x,y,z;
};
int g[6][3]= {{0,0,1},{0,0,-1},{0,1,0},{0,-1,0},{1,0,0},{-1,0,0}};
int bu[35][35][35];
char s[35][35][35];
int l,c,r;
queue<date>q;
int bfs(int q1,int w,int e)
{
int i;
bu[q1][w][e]=0;
date t,u,v;
t.x=q1;
t.y=w;
t.z=e;
q.push(t);
while(!q.empty())
{
u=q.front();
q.pop();
for(i=0; i<6; i++)
{
v.x=u.x+g[i][0];
v.y=u.y+g[i][1];
v.z=u.z+g[i][2];
if(v.x<0||v.y<0||v.z<0||v.x>=l||v.y>=r||v.z>=c)
continue;
if(s[v.x][v.y][v.z]=='#')
continue;
bu[v.x][v.y][v.z]=bu[u.x][u.y][u.z]+1;
if(s[v.x][v.y][v.z]=='E')
{
return bu[v.x][v.y][v.z];
}
q.push(v);
}
}
return -1;
}
int main()
{
int i,j,k;
while(~scanf("%d %d %d",&l,&r,&c)&&l&&r&&c)
{
for(i=0; i<l; i++)
{
for(j=0; j<r; j++)
{
scanf("%s",s[i][j]);
}
}
int flag=0;
for(i=0; i<l; i++)
{
for(j=0; j<r; j++)
{
for(k=0; k<c; k++)
{
if(s[i][j][k]=='S')
{
flag=1;
break;
}
}
if(flag)
break;
}
if(flag)
break;
}
memset(bu,0,sizeof(bu));
while(!q.empty())
{
q.pop();
}
int ans=bfs(i,j,k);
if(ans==-1)
printf("Trapped!\n");
else
printf("Escaped in %d minute(s).\n",ans);
}
return 0;
}
AC的,用bu数组标记步数,初始化为-1,如果走过,bu为走到该节点所用最短步数,
#include<iostream>
#include<queue>
#include<stdio.h>
#include<string.h>
using namespace std;
struct date
{
int x,y,z;
};
int g[6][3]= {{0,0,1},{0,0,-1},{0,1,0},{0,-1,0},{1,0,0},{-1,0,0}};
int bu[35][35][35];
char s[35][35][35];
int l,c,r;
queue<date>q;
int bfs(int q1,int w,int e)
{
int i;
bu[q1][w][e]=0;
date t,u,v;
t.x=q1;
t.y=w;
t.z=e;
q.push(t);
while(!q.empty())
{
u=q.front();
q.pop();
for(i=0; i<6; i++)
{
v.x=u.x+g[i][0];
v.y=u.y+g[i][1];
v.z=u.z+g[i][2];
if(v.x<0||v.y<0||v.z<0||v.x>=l||v.y>=r||v.z>=c)
continue;
if(s[v.x][v.y][v.z]=='#')
continue;
if(bu[v.x][v.y][v.z]!=-1)
continue;
bu[v.x][v.y][v.z]=bu[u.x][u.y][u.z]+1;
if(s[v.x][v.y][v.z]=='E')
{
return bu[v.x][v.y][v.z];
}
q.push(v);
}
}
return -1;
}
int main()
{
int i,j,k;
while(~scanf("%d %d %d",&l,&r,&c)&&l&&r&&c)
{
for(i=0; i<l; i++)
{
for(j=0; j<r; j++)
{
scanf("%s",s[i][j]);
}
}
int flag=0;
for(i=0; i<l; i++)
{
for(j=0; j<r; j++)
{
for(k=0; k<c; k++)
{
if(s[i][j][k]=='S')
{
flag=1;
break;
}
}
if(flag)
break;
}
if(flag)
break;
}
memset(bu,-1,sizeof(bu));
while(!q.empty())
{
q.pop();
}
int ans=bfs(i,j,k);
if(ans==-1)
printf("Trapped!\n");
else
printf("Escaped in %d minute(s).\n",ans);
}
return 0;
}
(*^▽^*)小菜鸡终于会做一道bfs的题了(虽然是模板题QWQ)