美文网首页
[kuangbin带你飞]专题一 简单搜索 B

[kuangbin带你飞]专题一 简单搜索 B

作者: jenye_ | 来源:发表于2018-06-12 01:37 被阅读0次

    [B - Dungeon Master]
    [POJ - 2251]
    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!
    
    

    • 这题碰到了三个问题:

      • 第一次提交runtime error:发现忘记判断边缘了,访问visited越界了
        • 总结了一下类似搜索题判断是否能访问4个要素:
          注意判断边缘情况
          1. 超过上限
          2. 低于下限

          判断是否可以通过

          1. 判断地图是否能够通过
          2. 判断是否访问过
    • 第二次提交wa了:发现忘记重置,使用memset重置visited,

    • 第三次提交ce.发现头文件用<memory.h>不行,换了<cstring>后A了

    • 在nowpos和要进队的nextpos输出是很好的调试方法


    #include<iostream>
    #include<cstdio> 
    #include<algorithm>
    #include<queue>
    #include<cstring>
    using namespace std;
    int mov[6][3]={{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}};
    int visited[40][40][40];
    char map[40][40][40];
    struct pos{
        int l;
        int r;
        int c;
        int cnt;
    };
    int L,R,C;
    
    void setpos(pos &node,int l,int r,int c,int cnt){
        node.l = l;
        node.r = r;
        node.c = c;
        node.cnt = cnt;
        return; 
    }
    
    void setvisited(pos node,int value){
        visited[node.l][node.r][node.c] = value;
        return;
    }
    
    int check(pos node){
        //注意判断边缘情况 
        // 1.超过上限 2.低于下限
        //判断是否可以通过 
        //3.判断地图是否能够通过 
        //4.判断是否访问过 
        
        if(
            node.c>=C||node.l>=L||node.r>=R||
            node.c<0||node.l<0||node.r<0||
            map[node.l][node.r][node.c]=='#'||visited[node.l][node.r][node.c]){
            return 0;
        }
        return 1;
    }
    
    int bfs(pos st){
        queue<pos> Q;
        Q.push(st);
        setvisited(st,1);
        pos nowpos;
        pos nextpos;
        while(!Q.empty()){
            nowpos = Q.front();Q.pop();
            if(map[nowpos.l][nowpos.r][nowpos.c]=='E'){
                return nowpos.cnt;
            }
            //try  diff derections 
    //      cout<<nowpos.l<<" "<<nowpos.r<<" "<<nowpos.c<<endl;
            for(int i = 0 ;i< 6 ;i++){
                
                setpos(nextpos,nowpos.l+mov[i][0],nowpos.r+mov[i][1],nowpos.c+mov[i][2],nowpos.cnt+1);
    //          cout<<"try"<<nextpos.l<<" "<<nextpos.r<<" "<<nextpos.c<<endl;
                if(check(nextpos)){
                    setvisited(nextpos,1);
                    Q.push(nextpos);
                }
            }
            
        }
        return -1;
        
        
    } 
    
    int main(){
        pos st;
        int time;
        while(cin>>L>>R>>C&&L!=0||R!=0||C!=0){
            //地图输入 
            memset(visited,0,sizeof(visited));
            for(int i =0;i<L;i++){
                for(int j=0;j<R;j++){
                    for(int s=0;s<C;s++){
                        cin>>map[i][j][s];
                        if(map[i][j][s]=='S'){
                            setpos(st,i,j,s,0);
                        }
                    }
                }
            }
            time = bfs(st);
            if(time>0){
                printf("Escaped in %d minute(s).\n",time);
            }else{
                printf("Trapped!\n");
            }
        }
        
        return 0;
    }
    
    

    相关文章

      网友评论

          本文标题:[kuangbin带你飞]专题一 简单搜索 B

          本文链接:https://www.haomeiwen.com/subject/qzwkeftx.html