poj2251_kuagnbin带你飞专题一

Posted 多一份不为什么的坚持

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了poj2251_kuagnbin带你飞专题一相关的知识,希望对你有一定的参考价值。

Dungeon Master
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 32684   Accepted: 12529

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!

Source

解题思路:只是把二维的变成三维的了,本质上不变,就增加了两个方向。
但我还是卡了好久,我刚开始只记录了上一步的位置,不往上一步的位置走,结果一直超时。
后来发现只要走过的地方就把vis数组置为1,以后就不能走到这个位置了。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>

using namespace std;

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

int l,r,c;
char maze[40][40][40];
int vis[40][40][40];
node start;
node endd;
int directx[7]={0,0,0,0,1,-1};
int directy[7]={-1,1,0,0,0,0};
int directz[7]={0,0,-1,1,0,0};

int findd(){
    queue<node> que;
    memset(vis,0,sizeof(vis));
    start.step=0;
    que.push(start);
    while(!que.empty()){
        node now=que.front();
        que.pop();
        for(int i=0;i<6;i++){
            node next=now;
            next.x+=directx[i];
            next.y+=directy[i];
            next.z+=directz[i];
            int xx=next.x;
            int yy=next.y;
            int zz=next.z;
            if(maze[xx][yy][zz]==#||vis[xx][yy][zz]||!(xx>=0&&xx<l&&yy>=0&&yy<r&&yy>=0&&zz>=0&&zz<c)){
                continue;
            }
            if(maze[xx][yy][zz]==E){
                next.step+=1;
                return next.step;
            }
            if(maze[xx][yy][zz]==.){
                next.step+=1;
                vis[xx][yy][zz]=1;
                que.push(next);
            }
        }
    }
    return 0;
}

int main()
{
    while(scanf("%d %d %d",&l,&r,&c)&&(l+r+c!=0)){
        getchar();
        for(int i=0;i<l;i++,getchar()){
            for(int j=0;j<r;j++,getchar()){
                for(int k=0;k<c;k++){
                    scanf("%c",&maze[i][j][k]);
                    if(maze[i][j][k]==S){
                        start.x=i;
                        start.y=j;
                        start.z=k;
                    }
                }
            }
        }
        int ans=findd();
        if(ans){
            printf("Escaped in %d minute(s).\n",ans);
        }else{
            printf("Trapped!\n");
        }

    }


    return 0;
}

 

以上是关于poj2251_kuagnbin带你飞专题一的主要内容,如果未能解决你的问题,请参考以下文章

poj1426_kuagnbin带你飞专题一

Dungeon Master POJ - 2251 [kuangbin带你飞]专题一 简单搜索

[ An Ac a Day ^_^ ] [kuangbin带你飞]专题四 最短路练习 POJ 2387 Til the Cows Come Home

[ An Ac a Day ^_^ ][kuangbin带你飞]专题四 最短路练习 POJ 2240 Arbitrage spfa求负环

kuangbin带你飞专题POJ - 3126 Prime Path

kuangbin带你飞专题POJ - 3126 Prime Path