您的位置:首页 > 编程语言 > C语言/C++

poj 2251 Dungeon Master

2013-06-09 00:00 459 查看
摘要: bfs。。。这道和之前那道都是比较简单的bfs

/*
B:Dungeon Master
查看 提交 统计 提问
总时间限制: 1000ms 内存限制: 65536kB
描述
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?
输入
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 exi by the letter 'E'. There's a single blank line after each level. Input is terminated by three zeroes for
L, R and C.
输出
Each maze generates one line of output. If it is possible to reach the exi, 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!
样例输入
3 4 5
S....
.###.
.##..
###.#

#####
#####
##.##
##...

#####
#####
#.###
####E

1 3 3
S##
#E#
###

0 0 0
样例输出
Escaped in 11 minute(s).
Trapped!
*/

#include <cstdio>
#include <list>
#include <string>
#include <cstring>
using namespace std;
int l = 0, r = 0, c = 0;
int s[105][105][105] = {0};
int step[105][105][105] = {0};

struct point
{
int x;
int y;
int z;
};

point start;
point exi;

list <point> lst;

void bfs(point p)
{
if(step[exi.x][exi.y][exi.z]) return;
point tep;
if(p.x + 1 < l && p.x + 1 >= 0 && s[p.x + 1][p.y][p.z] == 0 )
{
tep.x = p.x + 1;
tep.y = p.y;
tep.z = p.z;
step[p.x + 1][p.y][p.z] = step[p.x][p.y][p.z] + 1;
s[p.x + 1][p.y][p.z] = 1;
lst.push_back(tep);
}
if(p.x - 1 < l && p.x - 1 >= 0 && s[p.x - 1][p.y][p.z] == 0 )
{
tep.x = p.x - 1;
tep.y = p.y;
tep.z = p.z;
step[p.x - 1][p.y][p.z] = step[p.x][p.y][p.z] + 1;
s[p.x - 1][p.y][p.z] = 1;
lst.push_back(tep);
}
if( p.y + 1 < r && p.y + 1 >= 0 && s[p.x][p.y + 1][p.z] == 0)
{
tep.x = p.x;
tep.y = p.y + 1;
tep.z = p.z;
step[p.x][p.y + 1][p.z] = step[p.x][p.y][p.z] + 1;
s[p.x][p.y + 1][p.z] = 1;
lst.push_back(tep);
}
if(p.y - 1 < r && p.y - 1 >= 0 && s[p.x][p.y - 1][p.z] == 0 )
{
tep.x = p.x;
tep.y = p.y - 1;
tep.z = p.z;
step[p.x][p.y - 1][p.z] = step[p.x][p.y][p.z] + 1;
s[p.x][p.y - 1][p.z] = 1;
lst.push_back(tep);
}
if(p.z + 1 < c && p.z + 1 >= 0 && s[p.x][p.y][p.z + 1] == 0 )
{
tep.x = p.x;
tep.y = p.y;
tep.z = p.z + 1;
step[p.x][p.y][p.z + 1] = step[p.x][p.y][p.z] + 1;
s[p.x][p.y][p.z + 1] = 1;
lst.push_back(tep);
}
if(p.z - 1 < c && p.z - 1 >= 0&&s[p.x][p.y][p.z - 1] == 0 )
{
tep.x = p.x;
tep.y = p.y;
tep.z = p.z - 1;
step[p.x][p.y][p.z - 1] = step[p.x][p.y][p.z] + 1;
s[p.x][p.y][p.z - 1] = 1;
lst.push_back(tep);
}
return;
}

int main()
{
char t[35];
while(true)
{
scanf("%d%d%d", &l, &r, &c);
if(l == 0 || r == 0 || c == 0) break;
memset(s, 0, sizeof(s));
memset(step, 0, sizeof(step));
lst.clear();
for(int i = 0; i < l; ++i)
{
for(int j = 0; j < r; ++j)
{
scanf("%s", t);
for(int k = 0; k < c; ++k)
{
switch(t[k])
{
case '.':
s[i][j][k] = 0;
break;
case '#':
s[i][j][k] = 1;
break;
case 'S':
start.x = i;
start.y = j;
start.z = k;
s[i][j][k] = 1;
break;
case 'E':
exi.x = i;
exi.y = j;
exi.z = k;
s[i][j][k] = 0;
break;
default:
break;
}
}
}}
//scanf("\n");
lst.push_front(start);
while(!lst.empty())
{
bfs(lst.front());
lst.pop_front();
}
if(step[exi.x][exi.y][exi.z] == 0)
printf("Trapped!\n");
else printf("Escaped in %d minute(s).\n", step[exi.x][exi.y][exi.z]);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  poj cpp