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

B - Dungeon Master POJ - 2251

时间:2020-03-22 19:57:31      阅读:68      评论:0      收藏:0      [点我收藏+]

标签:span   cti   order   function   input   with   script   ini   print   

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!

BFS:
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <queue>
#include <map>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <numeric>
#include <cmath>
#include <iomanip>
//#include <unordered_set>
//#include <unordered_map>
//#include <xfunctional>
using namespace std;
int dir[6][3] = { { 0,0,1 },{ 0,0,-1 },{ 0,-1,0 },{ 0,1,0 },{1,0,0},{-1,0,0} };
const long long INF = 0x7f7f7f7f7f7f7f7f;
const int inf = 0x3f3f3f3f;
const double pi = 3.14159265358979;
#define ll  long long;
#define PII  pair<int, int>;
const int mod = 1e9 + 7;
const int marown = 1000 + 5;
//if(x<0 || x>=r || y<0 || y>=c)

struct node
{
    int x, y, z,cnt;
};

int l, r, c,S,E,sx,sy,sz,ex,ey,ez;
vector<vector<string>> v(35,vector<string>(35));
int res=inf;
void bfs(vector<vector<vector<bool>>>& visited, queue<node>& que)
{    
    while(!que.empty())
    {
        if (v[que.front().z][que.front().x][que.front().y]==E)
        {
            res = min(res,que.front().cnt);
        }
        for (int t = 0; t < 6; t++)
        {                
            node tmp;
            tmp.x = que.front().x + dir[t][0];
            tmp.y = que.front().y + dir[t][1];
            tmp.z = que.front().z + dir[t][2];
            tmp.cnt=que.front().cnt + 1;
            if (tmp.z >= 0 && tmp.z < l && tmp.x >= 0 && tmp.x < r && tmp.y >= 0 && tmp.y < c && v[tmp.z][tmp.x][tmp.y] != # && visited[tmp.z][tmp.x][tmp.y] == false)
            {
                
                visited[tmp.z][tmp.x][tmp.y] = true;
                que.push(tmp);
            }
        }
        que.pop();
    }
}

int main()
{
    while (cin >> l >> r >> c && l != 0 && r != 0 && c != 0)
    {
        for (int k = 0; k < l; k++)
        {
            for(int i=0;i<r;i++)
            {
                cin >> v[k][i];
                for (int j = 0; j < c; j++)
                {
                    if (v[k][i][j] == S)
                    {
                        sx = i, sy = j,sz=k;
                        v[k][i][j] = S;
                    }
                    else if (v[k][i][j] == E)
                    {
                        ex = i, ey = j,ez=k;
                        v[k][i][j] = E;
                    }
                }
            }
        }
        queue<node> que;
        res = inf;
        vector<vector<vector<bool>>> visited(35,vector<vector<bool>>(35,vector<bool>(35,false)));
        node start;
        start.x = sx;
        start.y = sy;
        start.z = sz;
        start.cnt = 0;
        que.push(start);
        bfs(visited,que);
        if (res == inf)
            cout << "Trapped!" << endl;
        else
            cout << "Escaped in " << res<< " minute(s)." << endl;
    }
    return 0;
}

 

 

B - Dungeon Master POJ - 2251

标签:span   cti   order   function   input   with   script   ini   print   

原文地址:https://www.cnblogs.com/dealer/p/12547718.html

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