POJ2251 - Dungeon Master - 三维bfs搜索
2017-03-15 20:08
302 查看
1.题目描述:
Dungeon Master
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
Sample Output
Source
Ulm Local 1997
2+3.题意概述+解题思路:
起点S终点E,问最短时间,别看图多,其实就是三维,二维推广一下就行,n维也是这样?
4.AC代码:
#include <stdio.h>
#include <queue>
#define N 35
using namespace std;
char mp
, vis
;
int l, r, c, sx, sy, sz, ex, ey, ez;
int dir[6][3] = { {1, 0, 0}, {0, 1, 0}, {0, 0, 1}, {-1, 0, 0}, {0, -1, 0}, {0, 0, -1} };
struct node
{
int x, y, z, t;
};
bool judge(int x, int y, int z)
{
if (x < 0 || x >= l || y < 0 || y >= r || z < 0 || z >= c || vis[x][y][z] || mp[x][y][z] == '#')
return 0;
return 1;
}
int bfs()
{
node cur, now;
queue<node> q;
cur.x = sx;
cur.y = sy;
cur.z = sz;
cur.t = 0;
q.push(cur);
while (!q.empty())
{
cur = q.front();
q.pop();
if (cur.x == ex && cur.y == ey && cur.z == ez)
return cur.t;
for (int i = 0; i < 6; i++)
{
int dx = cur.x + dir[i][0];
int dy = cur.y + dir[i][1];
int dz = cur.z + dir[i][2];
if (!judge(dx, dy, dz))
continue;
vis[dx][dy][dz] = 1;
now.x = dx;
now.y = dy;
now.z = dz;
now.t = cur.t + 1;
q.push(now);
}
}
return -1;
}
int main()
{
while (scanf("%d%d%d", &l, &r, &c) != EOF && l + r + c)
{
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 = i;
sy = j;
sz = k;
}
else if (mp[i][j][k] == 'E')
{
ex = i;
ey = j;
ez = k;
}
}
}
memset(vis, 0, sizeof(vis));
int ans = bfs();
if (ans == -1)
puts("Trapped!");
else
printf("Escaped in %d minute(s).\n", ans);
}
return 0;
}
Dungeon Master
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 31510 | Accepted: 12116 |
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!
Source
Ulm Local 1997
2+3.题意概述+解题思路:
起点S终点E,问最短时间,别看图多,其实就是三维,二维推广一下就行,n维也是这样?
4.AC代码:
#include <stdio.h>
#include <queue>
#define N 35
using namespace std;
char mp
, vis
;
int l, r, c, sx, sy, sz, ex, ey, ez;
int dir[6][3] = { {1, 0, 0}, {0, 1, 0}, {0, 0, 1}, {-1, 0, 0}, {0, -1, 0}, {0, 0, -1} };
struct node
{
int x, y, z, t;
};
bool judge(int x, int y, int z)
{
if (x < 0 || x >= l || y < 0 || y >= r || z < 0 || z >= c || vis[x][y][z] || mp[x][y][z] == '#')
return 0;
return 1;
}
int bfs()
{
node cur, now;
queue<node> q;
cur.x = sx;
cur.y = sy;
cur.z = sz;
cur.t = 0;
q.push(cur);
while (!q.empty())
{
cur = q.front();
q.pop();
if (cur.x == ex && cur.y == ey && cur.z == ez)
return cur.t;
for (int i = 0; i < 6; i++)
{
int dx = cur.x + dir[i][0];
int dy = cur.y + dir[i][1];
int dz = cur.z + dir[i][2];
if (!judge(dx, dy, dz))
continue;
vis[dx][dy][dz] = 1;
now.x = dx;
now.y = dy;
now.z = dz;
now.t = cur.t + 1;
q.push(now);
}
}
return -1;
}
int main()
{
while (scanf("%d%d%d", &l, &r, &c) != EOF && l + r + c)
{
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 = i;
sy = j;
sz = k;
}
else if (mp[i][j][k] == 'E')
{
ex = i;
ey = j;
ez = k;
}
}
}
memset(vis, 0, sizeof(vis));
int ans = bfs();
if (ans == -1)
puts("Trapped!");
else
printf("Escaped in %d minute(s).\n", ans);
}
return 0;
}
相关文章推荐
- POJ 2251 Dungeon Master 简单的三维bfs
- poj 2251 Dungeon Master (三维bfs)
- POJ - 2251 Dungeon Master(15.10.10 搜索专题)bfs
- POJ 2251 Dungeon Master(三维bfs)
- POJ训练计划2251_Dungeon Master(三维BFS)
- poj 2251 Dungeon Master(BFS三维)
- POJ-2251 Dungeon Master(简单的三维bfs)
- POJ - 2251 Dungeon Master (三维bfs)
- POJ 2251 Dungeon Master (三维BFS)
- POJ 2251 Dungeon Master :NYOJ 353 3D dungeon (三维空间上的搜索 bfs )
- POJ 2251 Dungeon Master --- 三维BFS(用BFS求最短路)
- poj2251 Dungeon Master(三维bfs)
- poj-2251 Dungeon Master 三维BFS
- POJ 2251 Dungeon Master (三维BFS)
- POJ 2251 Dungeon Master (搜索 -- BFS)
- POJ 2251 Dungeon Master(三维6方向BFS)
- 算法训练之BFS POJ 2251 Dungeon Master 三维最短路径
- poj 2251 Dungeon Master ----BFS (三维六个方向)
- poj 2251 Dungeon Master (BFS 三维)
- POJ - 2251 Dungeon Master(三维BFS)