zoukankan      html  css  js  c++  java
  • 广搜——三维迷宫

    [题目大意]:           给出一三维空间的地牢,要求求出由字符'S'到字符'E'的最短路径(注意只能走字符为'.'的点),移动方向可以是上,下,左,右,前,后,六个方向!每移动一次就耗费一分钟,要求输出最快的走出时间。

    [输入]: 第一行是三个整数,代表地牢的层数及每层的长与宽,以下各行是各层的地图

    [样例] 3 4 5           S....           .###.           .##..           ###.#
              #####           #####           ##.##             ##...

    #####           #####           #.###           ####E

    1 3 3           S##           #E#           ###

    0 0 0

    Sample Output

    Escaped in 11 minute(s).   Trapped!    [解题分析]:

    一个点到另外一个点的最小步数,直接用广搜,第一次搜到另外一个点时的步数即为最小步数!

    import java.util.*;
    
    class  point {//三层地牢中点的坐标
        int x;
        int y;
        int z;
        int step;//从入口到这点的时间
    
      public point(int x,int y,int z,int step){
        this.x=x;
        this.y=y;
        this.z=z;
        this.step=step;
      }
    
    
     public int  getX(){
       return this.x;
     }
     
     public int getY(){
       return this.y;
     }
    
    public int getZ(){
      return this.z;
     }
    
     public int getStep(){
       return this.step;
    }
           
    }
    
    public class Main {
      //移动方向可以是上,下,左,右,前,后,六个方向
     static int dir[][] = {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}, {-1, 0, 0}, {0, -1, 0}, {0, 0, -1}};
     char map[][][];//三维地图,有三层
     int R, L, C;
    
     public Main(char map[][][],int L,int R,int C){
        this.map=map;
        this.L=L;
        this.R=R;
        this.C=C;
    }
    
    int bfs(int x, int y, int z) {//广度优先搜索
        
        ArrayList< point> queue=new ArrayList< point>();//用ArrayList当作队列用
    
        queue.add(new point(x,y,z,0));
       
        while(queue.size()!=0) {
           
            for(int i = 0; i < 6; i++) {//从六个方向搜索
                //队头元素出队;
                 point head=queue.get(0);
                int a = head.x + dir[i][0];
                int b = head.y + dir[i][1];
                int c = head.z + dir[i][2];
    			
                if(a >= 0 && a < L && b >= 0 && b < R && c >= 0 && c < C && map[a][b][c] == 'E')
                    return head.step + 1;//找到出口
                if(a >= 0 && a < L && b >= 0 && b < R && c >= 0 && c < C && map[a][b][c] != '#') {
                      //元素入队;
                    queue.add(new point(a,b,c,head.step+1));//该位置可走
                    
                    map[a][b][c] = '#';//标记该处已经走过
                  
                }
            }
           queue.remove(0);
        }
        return -1;
    }
    
     
     public static void main(String[] args){
      Scanner in = new Scanner(System.in);
        int a, b, c;
        int L1,R1,C1;
        while(in.hasNext()) {
           L1=in.nextInt();
           R1=in.nextInt();
           C1=in.nextInt();
           if(L1==0&&R1==0&&C1==0) break;
            a = b = c = 0;
      
            char map1[][][]=new char[L1][R1][C1];
       
            for(int i = 0; i < L1; i++)
              for(int j = 0; j < R1; j++){
               map1[i][j]=in.next().toCharArray();
               for(int k=0;k< C1;k++){
                    if(map1[i][j][k] == 'S') {//找到入口位置;
                        a = i;
                        b = j;
                        c = k;
                    }
                }
             }
    
           
            Main m=new Main(map1,L1,R1,C1);
            
            int sum = m.bfs(a, b, c);
            if(sum != -1)
                System.out.printf("Escaped in %d minute(s).
    ", sum);
            else
                System.out.printf("Trapped!
    ");
        }
       }
    }
  • 相关阅读:
    【Java学习笔记十二】——初窥多线程
    【Java】使用swing实现简易计算器
    【Java学习技巧分享】——IDEA快捷键(超全!!!)
    【Java学习笔记九】——I/O流之字符流与数据流
    【Java学习笔记八】——I/O流之InputStream和OutputStream
    算法导论15章答案
    算法导论32章答案
    右击任务栏应用图标不显示菜单后的无奈美化
    verilog
    进制表示以及转换
  • 原文地址:https://www.cnblogs.com/xiaoying1245970347/p/3177255.html
Copyright © 2011-2022 走看看