[POJ] 2251 Dungeon Master

来源:互联网 发布:智能合约 知乎 编辑:程序博客网 时间:2024/06/06 00:58

题目:
https://vjudge.net/contest/156770#problem/B

题意:
一个三维的地牢,依次给出地牢的高长宽,并给出每一层的具体内容。S起点,E终点,求最短的步数。

分析:
一眼看就是三维的DFS或BFS,然后不信邪,跑了一下DFS就TLE了,最后跑个BFS才过的。把以前的4个方向变成6个就可以了。

代码:

#include<stdio.h>  #include<cstring>  #include<algorithm>  #include<iostream>#include<vector>#include<set>#include<string>#include<sstream>#include<map>#include<list>#include<queue>using namespace std; const int MAX_N = 35 ; int l , r , c ; int sx,sy,sz,ex,ey,ez ;int ans ;int flag ; int max_n = 0x3f3f3f3f;char a[MAX_N][MAX_N][MAX_N];int book[MAX_N][MAX_N][MAX_N];int next_n[6][3] = {{0,0,1},{0,0,-1},{0,1,0},{0,-1,0},{1,0,0},{-1,0,0}};  class node{    public:        int z , x , y ,s ; };[]void bfs(){    queue<node> q ;     node head_n ,  temp_n ;     head_n.z = sz ;    head_n.x = sx ;     head_n.y= sy ;     head_n.s = 0 ;     q.push(head_n);    while(!q.empty()){        head_n = q.front() ;         q.pop() ;         for(int i = 0 ; i < 6 ; i++){            temp_n.z = head_n.z + next_n[i][0];            temp_n.x = head_n.x + next_n[i][1];            temp_n.y = head_n.y + next_n[i][2];            temp_n.s = head_n.s + 1 ;             if(temp_n.z<0||temp_n.z>l||temp_n.x<0||temp_n.x>r||temp_n.y<0||temp_n.y>c) continue ;             if((a[temp_n.z][temp_n.x][temp_n.y]=='.'||a[temp_n.z][temp_n.x][temp_n.y]=='E')&&book[temp_n.z][temp_n.x][temp_n.y]==0){                book[temp_n.z][temp_n.x][temp_n.y] = 1 ;                 q.push(temp_n);            }            if(temp_n.z==ez&&temp_n.x == ex&&temp_n.y== ey){                flag = 1 ;                break ;             }        }        if(flag){            break;        }    }    if(flag){        temp_n = q.back() ;         max_n = temp_n.s ;     }    return ; }void dfs(int z, int x ,int y,int step ){    if(z==ez&&x==ex&&y==ey){        flag = 1 ;         if(step<max_n) max_n = step ;         return ;     }    for(int i = 0 ; i < 6 ; i++){        int tz = z + next_n[i][0];        int tx = x + next_n[i][1];        int ty = y + next_n[i][2];        if(tz<0||tz>l||tx<0||tx>r||ty<0||ty>c) continue ;         if((a[tz][tx][ty]=='.'||a[tz][tx][ty]=='E')&&book[tz][tx][ty]==0){            //printf("tz=%d tx=%d ty=%d\n",tz,tx,ty);            book[tz][tx][ty] = 1 ;             dfs(tz,tx,ty,step+1);            book[tz][tx][ty] = 0 ;         }    }    return ; }int main(){    //freopen("in.txt","r",stdin);    //freopen("out.txt","w",stdout);    //ios::sync_with_stdio(false);    while(cin>>l>>r>>c&&(r||l||c)){        flag = 0 ;         max_n = 0x3f3f3f3f ;         ans = 0 ;         memset(a,0,sizeof(a));        memset(book,0,sizeof(book));        getchar();        for(int i = 0 ;i < l ; i ++){            for(int j = 0 ; j < r ; j++)scanf("%s",a[i][j]);        }        for(int i = 0 ; i < l ; i ++){            for(int j = 0 ; j < r ; j++){                for(int k = 0 ; k < c ; k++){                    if(a[i][j][k]=='S'){                        sz = i ;                         sx = j ;                         sy = k ;                     }                    else if(a[i][j][k]=='E'){                        ez = i ;                         ex = j ;                         ey = k ;                     }                }            }        }        //printf("sz=%d sx=%d sy=%d\n",sz,sx,sy);        //printf("ez=%d ex=%d ey=%d\n",ez,ex,ey);        book[sz][sx][sy] = 1 ;         //dfs(sz,sx,sy,0);        bfs();        if(flag){            printf("Escaped in %d minute(s).\n",max_n);        }        else{            printf("Trapped!\n");        }    }    return 0 ;   } 
0 0