Dungeon Master---2251(bfs)

时间:2023-03-08 22:38:58

http://poj.org/problem?id=2251

有一个三维的牢房地图 求从S点走E点的最小时间;

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<algorithm>
#include<queue>
#include<iostream>
using namespace std; #define N 50
#define INF 0xfffffff
int dir[][] = { {, , }, {-, , }, {, , }, {, , -}, {, , }, {, -, } };
char map[N][N][N];
int L,R,C;
int vis[N][N][N];
struct node
{
int x, y, z, step;
}; int bfs(node s, node e)
{
memset(vis, , sizeof(vis));
queue<node>Q;
s.step = ;
vis[s.x][s.y][s.z] = ;
Q.push(s);
while(Q.size())
{
node p, q = Q.front(); Q.pop();
if(q.x == e.x && q.y == e.y && q.z == e.z )
return q.step;
for(int i=; i<; i++)
{
p.x = q.x + dir[i][];
p.y = q.y + dir[i][];
p.z = q.z + dir[i][];
if( p.x<L && p.x>= && p.y>= && p.y<R && p.z>= && p.z<C && !vis[p.x][p.y][p.z] && map[p.x][p.y][p.z] != '#')
{
p.step = q.step + ;
vis[p.x][p.y][p.z] = ;
Q.push(p);
}
}
}
return -;
} int main()
{
char str[N];
while(scanf("%d%d%d", &L, &R, &C), L + R + C)
{
node s, e;
for(int i=; i<L; i++)
{
gets(str);
for(int j=; j<R; j++)
{
gets(map[i][j]);
for(int k=; k<C; k++)
{
if(map[i][j][k] == 'S')
s.x = i, s.y = j, s.z = k;
if(map[i][j][k] == 'E')
e.x = i, e.y = j, e.z = k;
}
} }
int ans = bfs(s, e);
if(ans==-)
printf("Trapped!\n");
else
printf("Escaped in %d minute(s).\n", ans);
}
return ;
}