码迷,mamicode.com
首页 > 其他好文 > 详细

Poj2251 BFS模板题

时间:2019-08-25 00:38:30      阅读:85      评论:0      收藏:0      [点我收藏+]

标签:turn   node   body   quick   app   ==   after   find   lock   

Dungeon Master

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 62948   Accepted: 23020

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!

代码一(STL  queue)

技术图片
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
struct node
{
    int x, y, z;
    int step;
}S, Node;
const int maxn = 35;
int X[6] = {1,-1,0,0,0,0};
int Y[6] = {0,0,1,-1,0,0};
int Z[6] = {0,0,0,0,1,-1};
int n, r, c;
int ans;
bool inq[maxn][maxn][maxn];
char maze[maxn][maxn][maxn];
bool judge(int z, int y, int x)
{
    if(z < 0 || z >= n || y < 0 || y >= r || x < 0 || x >= c || inq[z][y][x] == true|| maze[z][y][x] == #)
        return false;
    return true;
}

bool BFS()
{
    queue<node>q;
    q.push(S);
    while(!q.empty())
    {
        node top = q.front();
        q.pop();
        if(maze[top.z][top.y][top.x] == E)
        {
            if(top.step == 0)
                return false;
            else
            {
                ans = top.step;
                return true;
            }
        }
        int ii;
        for(ii = 0; ii < 6; ii++)
        {
            int newX = top.x + X[ii];
            int newY = top.y + Y[ii];
            int newZ = top.z + Z[ii];
            if(judge(newZ, newY, newX))
            {
                Node.x = newX;
                Node.y = newY;
                Node.z = newZ;
                Node.step = top.step+1;
                q.push(Node);
                inq[newZ][newY][newX] = true;
            }
        }
    }
    return false;
}
int main()
{
    while(scanf("%d%d%d", &n, &r, &c) && n && r && c)
    {
        memset(inq, false, sizeof(inq));
        int i, j, k;
        for(i = 0; i < n; i++)
        {
            for(j = 0; j < r; j++)
            {
                scanf("%s", maze[i][j]);
                for(k = 0; k < c; k++)
                {
                    if(maze[i][j][k] == S)
                    {
                        S.x = k;
                        S.y = j;
                        S.z = i;
                    }
                }
            }
        }
        bool p = BFS();
        if(!p)
            printf("Trapped!\n");
        else
            printf("Escaped in %d minute(s).\n", ans);
    }
    return 0;
}
View Code

 

Poj2251 BFS模板题

标签:turn   node   body   quick   app   ==   after   find   lock   

原文地址:https://www.cnblogs.com/DreamInPal/p/11406428.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!