zoukankan      html  css  js  c++  java
  • java实现迷宫算法--转

    沿着所有方向进行探测,有路径则走,没有路径则从栈中回退。

    回溯法是一种不断试探且及时纠正错误的搜索方法,下面的求解过程采用回溯法。从入口出发,按某一方向向前探索,若能走通(未走过的),即某处可以到达,则到达一个新点,否则试探下一个方向;若所有的方向均没有通路,则沿原路返回前一点,换下一个方向继续试探,直到所有可能的通路都搜索到,或找到一条通路,或无路可走又返回到入口点。这里可以用一个栈来实现,每走一步,将该位置压入栈中,若该点无路可走,则出栈返回上一位置。
    需要解决的四个问题:
    (1)表示迷宫的数据结构
      设迷宫为m行n列,利用数组maze[m][n]来表示一个迷宫,maze[i][j]=0或1,其中0表示通路,1表示不通。迷宫该数组四边都为1,代表迷宫四周都是墙。这样就可以保证每个点都有8个方向可以试探。
      入口为(1,1),出口为(6,8)
      1,1,1,1,1,1,1,1,1,1
    1,0,1,1,1,0,1,1,1,1
    1,1,0,1,0,1,1,1,1,1
    1,0,1,0,0,0,0,0,1,1
    1,0,1,1,1,0,1,1,1,1
    1,1,0,0,1,1,0,0,0,1
    1,0,1,1,0,0,1,1,0,1
    1,1,1,1,1,1,1,1,1,1
    (2)试探方向
      迷宫中间每个点都有8个方向可以试探。其增量数组可以用一个8*2的二维数组move表述,表示对当前点而言,它周围8个点的行和列的坐标偏移量.具体值如下:
           x  y
    0  1
    1 1
    1 0
    1 -1
    0 -1
    -1 -1
    -1 0
    -1 1
    在move数组中,x表示横坐标的增量,y表示纵坐标的增量。
      (3)栈中存放元素的设计
      栈中所存放的元素应该包含所到达的每点的坐标以及从该点沿哪个方向向下走的,可用一个类表示:
    class Step{
    int x,y,d;
    public Step(int x,int y,int d) {
    this.x = x;//横坐标
    this.y = y;//纵坐标
    this.d = d;//方向
    }
    }
    (4)防止重复到达某点而发生死循环
      使maze[i][j]置为-1,以便区别为达到的点,同样也可以防止走重复点的作用。

    源码如下:
    package com.test;
    
    import java.util.Stack;
    class Step{
        int x,y,d;
        public Step(int x,int y,int d) {
            this.x = x;//横坐标
            this.y = y;//纵坐标
            this.d = d;//方向
    }
    }
    
    public class MazeTest {
    
    public static void main(String[] args) {
    // 迷宫定义
    int[][] maze = {{1,1,1,1,1,1,1,1,1,1},
        {1,0,1,1,1,0,1,1,1,1},
        {1,1,0,1,0,1,1,1,1,1},
        {1,0,1,0,0,0,0,0,1,1},
        {1,0,1,1,1,0,1,1,1,1},
        {1,1,0,0,1,1,0,0,0,1},
        {1,0,1,1,0,0,1,1,0,1},
        {1,1,1,1,1,1,1,1,1,1}};
    int[][] move = {{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1},{-1,0},{-1,1}};
        Stack s = new Stack();
        Stack s1 = new Stack();
        int a = path(maze, move, s);
        while(!s.isEmpty()){
        Step step = s.pop();
        System.out.println(step.x+":"+step.y);
        }
    }
    public static int path(int[][] maze,int[][] move,Stack s){
        Step temp = new Step(1,1,-1); //起点
        s.push(temp);
        while(!s.isEmpty()){
            temp = s.pop();
            int x = temp.x;
            int y = temp.y;
            int d = temp.d+1;
        while(d<8){
            int i = x + move[d][0];
            int j = y + move[d][1];
            if(maze[i][j] == 0){ //该点可达
            temp = new Step(i,j,d); //到达新点
            s.push(temp);
            x = i;
            y = j;
        maze[x][y] = -1; //到达新点,标志已经到达
        if(x == 6 && y == 8){
            return 1; //到达出口,迷宫有路,返回1
        }else{
            d = 0; //重新初始化方向
        }
    }else{
        d++; //改变方向
        }
        }
    }
        return 0;
    }
    
    }                            

    代码2:

    import java.util.*;
     
    class Position{
        public Position(){
     
        }
     
        public Position(int row, int col){
            this.col = col;
            this.row = row;
        }
     
        public String toString(){
            return "(" + row + " ," + col + ")";
        }
     
        int row;
        int col;
    }
     
    class Maze{
        public Maze(){
            maze = new int[15][15];
            stack = new Stack<Position>();
            p = new boolean[15][15];
        }
     
        /*
         * 构造迷宫
         */
        public void init(){
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入迷宫的行数");
            row = scanner.nextInt();
            System.out.println("请输入迷宫的列数");
            col = scanner.nextInt();
            System.out.println("请输入" + row + "" + col + "列的迷宫");
            int temp = 0;
            for(int i = 0; i < row; ++i) {
                for(int j = 0; j < col; ++j) {
                    temp = scanner.nextInt();
                    maze[i][j] = temp;
                    p[i][j] = false;
                }
            }
        }
     
        /*
         * 回溯迷宫,查看是否有出路
         */
        public void findPath(){
            // 给原始迷宫的周围家一圈围墙
            int temp[][] = new int[row + 2][col + 2];
            for(int i = 0; i < row + 2; ++i) {
                for(int j = 0; j < col + 2; ++j) {
                    temp[0][j] = 1;
                    temp[row + 1][j] = 1;
                    temp[i][0] = temp[i][col + 1] = 1;
                }
            }
            // 将原始迷宫复制到新的迷宫中
            for(int i = 0; i < row; ++i) {
                for(int j = 0; j < col; ++j) {
                    temp[i + 1][j + 1] = maze[i][j];
                }
            }
            // 从左上角开始按照顺时针开始查询
     
            int i = 1;
            int j = 1;
            p[i][j] = true;
            stack.push(new Position(i, j));
            while (!stack.empty() && (!(i == (row) && (j == col)))) {
     
                if ((temp[i][j + 1] == 0) && (p[i][j + 1] == false)) {
                    p[i][j + 1] = true;
                    stack.push(new Position(i, j + 1));
                    j++;
                } else if ((temp[i + 1][j] == 0) && (p[i + 1][j] == false)) {
                    p[i + 1][j] = true;
                    stack.push(new Position(i + 1, j));
                    i++;
                } else if ((temp[i][j - 1] == 0) && (p[i][j - 1] == false)) {
                    p[i][j - 1] = true;
                    stack.push(new Position(i, j - 1));
                    j--;
                } else if ((temp[i - 1][j] == 0) && (p[i - 1][j] == false)) {
                    p[i - 1][j] = true;
                    stack.push(new Position(i - 1, j));
                    i--;
                } else {
                    stack.pop();
                    if(stack.empty()){
                        break;
                    }
                    i = stack.peek().row;
                    j = stack.peek().col;
                }
     
            }
     
            Stack<Position> newPos = new Stack<Position>();
            if (stack.empty()) {
                System.out.println("没有路径");
            } else {
                System.out.println("有路径");
                System.out.println("路径如下:");
                while (!stack.empty()) {
                    Position pos = new Position();
                    pos = stack.pop();
                    newPos.push(pos);
                }
            }
             
            /*
             * 图形化输出路径
             * */
             
            String resault[][]=new String[row+1][col+1];
            for(int k=0;k<row;++k){
                for(int t=0;t<col;++t){
                    resault[k][t]=(maze[k][t])+"";
                }
            }
            while (!newPos.empty()) {
                Position p1=newPos.pop();
                resault[p1.row-1][p1.col-1]="#";
             
            }
             
            for(int k=0;k<row;++k){
                for(int t=0;t<col;++t){
                    System.out.print(resault[k][t]+"	");
                }
                System.out.println();
            }
         
     
        }
     
        int maze[][];
        private int row = 9;
        private int col = 8;
        Stack<Position> stack;
        boolean p[][] = null;
    }
     
    class hello{
        public static void main(String[] args){
            Maze demo = new Maze();
            demo.init();
            demo.findPath();
        }
    }
  • 相关阅读:
    springboot 集成redission 以及分布式锁的使用
    springboot 使用线程池
    springboot 引入logback框架
    StringRedisTemplate 使用示例,工具类
    XHR post请求下载文件
    java List对象排序
    springboot中使用resttemplate调用服务是设置header中的accept-charset
    面试
    本地包打到项目中利用maven
    sql
  • 原文地址:https://www.cnblogs.com/yaowen/p/4479563.html
Copyright © 2011-2022 走看看