• poj2251——bfs


    POJ 2251  bfs

    Dungeon Master
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 18245   Accepted: 7075

    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!

    题意:在三维空间上找最短路,其实就是bfs,和二维没有区别,水题
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    #include<algorithm>
    #include<queue>
    
    using namespace std;
    
    const int maxn=50;
    const int INF=(1<<28);
    
    int X,Y,Z;
    char ch[maxn][maxn][maxn];
    bool vis[maxn][maxn][maxn];
    int sx,sy,sz;
    int ans;
    int dx[]={-1,1,0,0,0,0};
    int dy[]={0,0,-1,1,0,0};
    int dz[]={0,0,0,0,-1,1};
    
    struct node
    {
        int x,y,z;
        int dist;
    };
    
    bool bfs()
    {
        queue<node> q;
        q.push({sx,sy,sz,0});
        vis[sx][sy][sz]=1;
        while(!q.empty()){
            node now=q.front();
            q.pop();
            for(int i=0;i<6;i++){
                int nx=now.x+dx[i];
                int ny=now.y+dy[i];
                int nz=now.z+dz[i];
                int nd=now.dist+1;
                if(ch[nx][ny][nz]=='E'){
                    ans=nd;
                    return true;
                }
                if(ch[nx][ny][nz]=='#') continue;
                if(vis[nx][ny][nz]) continue;
                vis[nx][ny][nz]=1;
                q.push({nx,ny,nz,nd});
            }
        }
        return false;
    }
    
    int main()
    {
        while(cin>>X>>Y>>Z,X||Y||Z){
            memset(ch,'#',sizeof(ch));
            for(int i=1;i<=X;i++){
                for(int j=1;j<=Y;j++){
                    for(int k=1;k<=Z;k++){
                        cin>>ch[i][j][k];
                        if(ch[i][j][k]=='S'){
                            sx=i;
                            sy=j;
                            sz=k;
                        }
                    }
                }
            }
            ans=0;
            memset(vis,0,sizeof(vis));
            if(bfs()) printf("Escaped in %d minute(s).
    ",ans);
            else printf("Trapped!
    ");
        }
        return 0;
    }
    poj2251_bfs
    没有AC不了的题,只有不努力的ACMER!
  • 相关阅读:
    macbook466加了两条1333金士顿正常
    spring 使用 groovy 的 utf8 问题
    jQuery Pagination Plugin Demo
    ssh 二级跳 转
    实战 Groovy: 用 curry 过的闭包进行函数式编程
    无刷新分页 jquery.pagination.js 张龙豪 博客园
    用fgets()函数从屏幕上输入一字符串_BenRuanChinaUnix博客
    What Is My IP Shows Your IP Address
    Chapter 24. Dynamic language support
    什么是SQA?
  • 原文地址:https://www.cnblogs.com/--560/p/4334129.html
Copyright © 2020-2023  润新知