zoukankan      html  css  js  c++  java
  • 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!
  • 相关阅读:
    Eclipse 软件 Java 解决:出现的editor does not contain a main type错误框 问题
    Oracle SQL高级编程——分析函数(窗口函数)全面讲解
    瑞联科技:Pwp3框架 调用存储过程返还数据集合 到前端界面展示
    spring jdbctemplate调用存储过程,返回list对象
    利用jquery操作Radio方法小结
    Oracle临时表
    JdbcUtil
    整理oracle 树形查询
    Js:消息弹出框、获取时间区间、时间格式、easyui datebox 自定义校验、表单数据转化json、控制两个日期不能只填一个
    Java对象之间的深度复制拷贝
  • 原文地址:https://www.cnblogs.com/--560/p/4334129.html
Copyright © 2011-2022 走看看