poj 2251 Dungeon Master

来源:互联网 发布:video.js兼容ie8 编辑:程序博客网 时间:2024/06/04 19:21
Dungeon Master
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 26983 Accepted: 10579

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 5S.....###..##..###.#############.####...###########.#######E1 3 3S###E####0 0 0

Sample Output

Escaped in 11 minute(s).Trapped!

Source

Ulm Local 1997


题意:在一个三维的地牢里面,主人公想尽快的逃脱,他可以上下左右前后移动,每次移动耗时一分钟,问他最快能够在几分钟内逃脱,如果不能,输出“Trapped!”。


输入: 第一个是地牢的层数,第二,三个数表示一层地牢的规格,之后输入地牢的地图,“S”为起始点 "E"为出口,"#"不能走,"."为走廊。多case,直至0 0 0结束。


解法:经典的dfs,从2D扩展到3D,应对的方法是一样的,不同的就是在建图上多一个z坐标。




//Code:#include<iostream>#include<cstdio>#include<cstring>#include<queue>using namespace std;const int maxn = 35;bool mapn[maxn][maxn][maxn];bool visited[maxn][maxn][maxn];int z,x,y;int dir[][3]={0,0,-1,0,0,1,0,-1,0,0,1,0,1,0,0,-1,0,0};struct Node{    int x;    int y;    int z;    int step;    Node(int p1=0,int p2=0,int p3=0,int s=0):x(p1),y(p2),z(p3),step(s){}};bool operator == (const Node &a,const Node &b){    return a.x==b.x&&a.y==b.y&&a.z==b.z;}bool isValid(Node & v){    return v.x>=0 && v.y>=0 && v.z>=0 && v.x<x && v.y<y && v.z<z && mapn[v.x][v.y][v.z] && !visited[v.x][v.y][v.z];}int bfs(Node & Vs, Node & Ve){    queue<Node> qu;    qu.push(Vs);    visited[Vs.x][Vs.y][Vs.z]=true;    while(!qu.empty())    {        Node fa=qu.front();        qu.pop();        for(int i=0; i<6; i++)        {            Node P(dir[i][0]+fa.x, dir[i][1]+fa.y ,dir[i][2]+fa.z);            if(isValid(P))            {                P.step=fa.step+1;                qu.push(P);                visited[P.x][P.y][P.z]=true;            }            if(P==Ve)            {                printf("Escaped in %d minute(s).\n",P.step);                return 1;            }        }    }    return 0;}int main(){    //freopen("in.txt","r",stdin);    while(scanf("%d%d%d",&z,&x,&y),z+x+y)    {        getchar();        memset(mapn,false,sizeof(mapn));        memset(visited,false,sizeof(visited));        Node S_pos,E_pos;        char c;        for(int i=0;i<z;i++)        {            for(int j=0;j<x;j++)            {                for(int k=0;k<y;k++)                {                    scanf("%c",&c);                    if(c=='S')                    {                        S_pos.x=j;                        S_pos.y=k;                        S_pos.z=i;                        mapn[j][k][i]=true;                    }                    if(c=='.')                    {                        mapn[j][k][i]=true;                    }                    if(c=='E')                    {                        E_pos.x=j;                        E_pos.y=k;                        E_pos.z=i;                        mapn[j][k][i]=true;                    }                }                getchar();            }            getchar();        }        if(!bfs(S_pos,E_pos))            printf("Trapped!\n");    }    return 0;}


这道题目因为scanf("%c",&c),要注意输入,在合适的位置用上getchar()是必须的,还提一点我犯的错误:

一开始把代码写出来的时候,为了容易分辨,我把dir方向数组写出了这样:


int dir[][3]={(0,0,-1),(0,0,1),(0,-1,0),(0,1,0),(1,0,0),(-1,0,0)};


(0,0,-1)这样加上小括号会使它运算,根据逗号运算符的性质(0,0,-1)=-1,所以这个dir数组其实只初始化了6个值,这里就错了,要注意。


把小括号换成大括号可以,或者干脆不加括号。




0 0