首页 > 代码库 > poj 2251 Dungeon Master

poj 2251 Dungeon Master

#include <iostream>#include <stdio.h>#include <string.h>#include <queue>using namespace std;#define maxx 45char map[maxx][maxx][maxx];int vis[maxx][maxx][maxx];int dir[6][3]={{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}};//int dir[6][3]= {(1,0,0),(-1,0,0),(0,1,0),(0,-1,0),(0,0,1),(0,0,-1)};  ///  这里让我WA了  无限次~~~~~~~~~~~int l,r,c;struct node{    int x,y,z;    int step;};int bfs(int si,int sj,int sk){    queue<node> q;    while(!q.empty())        q.pop();    memset(vis,0,sizeof(vis));    node cur,next;    cur.z=si,cur.x=sj,cur.y=sk,cur.step=0;    vis[si][sj][sk]=1;    q.push(cur);    while(!q.empty()){        cur=q.front();        q.pop();        for(int i=0;i<6;i++){            next.z=cur.z+dir[i][0];            next.x=cur.x+dir[i][1];            next.y=cur.y+dir[i][2];            next.step=cur.step+1;            if(next.z<1 || next.z>l || next.x<1 || next.x>r || next.y<1 || next.y>c || map[next.z][next.x][next.y]==#)                continue;            if(map[next.z][next.x][next.y]==E)                return next.step;            if(!vis[next.z][next.x][next.y]){                vis[next.z][next.x][next.y]=1;                q.push(next);            }        }    }    return 0;}int main(){    int si,sj,sk;    while(scanf("%d%d%d",&l,&r,&c),l,r,c){        for(int i=1;i<=l;i++)            for(int j=1;j<=r;j++)                for(int k=1;k<=c;k++){                    cin>>map[i][j][k];                    if(map[i][j][k]==S){                        si=i;sj=j;sk=k;                    }                }        int ans=bfs(si,sj,sk);        if(ans==0)            printf("Trapped!\n");        else            printf("Escaped in %d minute(s).\n",ans);    }    return 0;}

Time Limit: 1000 MS Memory Limit: 65536 KB

64-bit integer IO format: %I64d , %I64u   Java class name: Main

[Submit] [Status] [Discuss]

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!

题意:三维空间 S起点 E终点 #不可以走 同样求最少步骤 所以依旧广搜
类似于二维哒~~~~~~~~

poj 2251 Dungeon Master