HOJ 1448

来源:互联网 发布:感情网络用语有哪些? 编辑:程序博客网 时间:2024/06/15 23:04

小测ProblemB。
传送门:http://acm.hit.edu.cn/hojx/showproblem/1448/
1448 - Dungeon Master

Time limit : 1 s Memory limit : 32 mb
Submitted : 643 Accepted : 324
Submit

Problem 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
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.
L, R, C <= 35
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
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 5S.....###..##..###.#############.####...###########.#######E1 3 3S###E####

0 0 0
Sample Output
Escaped in 11 minute(s).
Trapped!

一道BFS“走迷宫类”搜索题,只不过把迷宫变成了三维的,相较于二维的多了两个方向,只要把地图和到达点变成三维的就可以了。
不过这里有一个小问题,WA了两次,后来我把X,Y,Z的划分调了一下后就AC了。。。
也不知道到底是什么原因,回去好好看看。。。
立个flag,过几天把做的一些BFS的题目写个总结。

AC代码:

#include <cstdio>#include <cstring>#include <queue>#define maxn 37using namespace std;int l, r, c;char dun[maxn][maxn][maxn];int get[maxn][maxn][maxn];int sx, sy, sz;struct Node{    int x, y, z;    int step;};int dx[6] = {0, 0, 1, -1, 0, 0};int dy[6] = {1, -1, 0, 0, 0, 0};int dz[6] = {0, 0, 0, 0, 1, -1};queue<Node> escape;int BFS(void){    while(!escape.empty())    escape.pop();    memset(get, 0, sizeof(get));    Node beg;    beg.x = sx, beg.y = sy, beg.z = sz, beg.step = 0;    escape.push(beg);    get[beg.x][beg.y][beg.z] = 1;    while(!escape.empty())    {        Node cur = escape.front();        escape.pop();        if(dun[cur.x][cur.y][cur.z] == 'E')            return cur.step;        for(int i = 0; i < 6; i++)        {            Node next;            next.x = cur.x + dx[i];            next.y = cur.y + dy[i];            next.z = cur.z + dz[i];            if(next.x < 0 || next.x >= l || next.y < 0 || next.y >= r || next.z < 0 || next.z >= c)    continue;            if(get[next.x][next.y][next.z] || dun[next.x][next.y][next.z] == '#')    continue;            get[next.x][next.y][next.z] = 1;            next.step = cur.step + 1;            escape.push(next);        }    }    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", dun[i][j]);                for(int h = 0; h < c; h++)                {                    if(dun[i][j][h] == 'S')                    {                        sx = i;                        sy = j;                        sz = h;                    }                }            }        }        int ans = BFS();        if(ans == -1)            printf("Trapped!\n");        else            printf("Escaped in %d minute(s).\n", ans);    }    return 0;}
原创粉丝点击