您的位置:首页 > 其它

poj2251 三维bfs求最短距离

2015-12-16 19:57 330 查看
Dungeon Master

Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 22466Accepted: 8769
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?

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!


题意:三维的地图,求起点到终点的最短距离

分析:三维bfs,多判断两个方向上下就行 。

#include<bitset>
#include<map>
#include<vector>
#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<stack>
#include<queue>
#include<set>
#define inf 0x3f3f3f3f
#define mem(a,x) memset(a,x,sizeof(a))

using namespace std;

typedef long long ll;
typedef pair<int,int> pii;

inline int in()
{
int res=0;char c;
while((c=getchar())<'0' || c>'9');
while(c>='0' && c<='9')res=res*10+c-'0',c=getchar();
return res;
}
struct st
{
int x,y,z;
};
const int N=100100;
char a[33][33][33]; //层数 x y
int dis[33][33][33];
int l,n,m;
int dx[]={-1,0,1,0},dy[]={0,-1,0,1};

int bfs(st start,st end)
{
queue<st> q;
q.push(start);
dis[start.x][start.y][start.z]=0;
while(!q.empty())
{
st now = q.front();
q.pop();
if(now.x==end.x && now.y==end.y && now.z==end.z) return dis[end.x][end.y][end.z];

for(int i=0;i<4;i++)
{
int nk=now.x;
int nx=now.y+dx[i];
int ny=now.z+dy[i];
if(nk>=0 && nx>=0 && ny>=0 && nk<l && nx<n && ny<m && a[nk][nx][ny]=='.' && dis[nk][nx][ny]==inf)
{
dis[nk][nx][ny]=dis[now.x][now.y][now.z]+1;
q.push(st{nk,nx,ny});
}
}
int nk=now.x+1;
int nx=now.y;
int ny=now.z;
if(nk>=0 && nk<l && a[nk][nx][ny]=='.' && dis[nk][nx][ny]==inf)
{
dis[nk][nx][ny]=dis[now.x][now.y][now.z]+1;
q.push(st{nk,nx,ny});
}
nk=now.x-1;
nx=now.y;
ny=now.z;
if(nk>=0 && nk<l && a[nk][nx][ny]=='.' && dis[nk][nx][ny]==inf)
{
dis[nk][nx][ny]=dis[now.x][now.y][now.z]+1;
q.push(st{nk,nx,ny});
}

}
return dis[end.x][end.y][end.z];
}
int main()
{
while(~scanf("%d%d%d",&l,&n,&m) && l)
{
for(int k=0;k<l;k++)
{
for(int i=0;i<n;i++) scanf("%s",a[k][i]);
}
mem(dis,inf);
st start,end;
for(int k=0;k<l;k++)
{
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(a[k][i][j]=='S')
{
start=st{k,i,j};

}
else if(a[k][i][j]=='E')
{
end=st{k,i,j};
a[k][i][j]='.';//注意要把end位置设置为'.',要不然会找不到,这里错了好几次
}
}
}
}
int ans=bfs(start,end);

if(ans==inf) puts("Trapped!");
else
printf("Escaped in %d minute(s).\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: