java解决迷宫问题

任务:随机生成一个迷宫图,0和1分别表示迷宫中的通路和障碍,设计一个程序,对任意设定的迷宫,求出一条从入口到出口的通路,或得出没有通路的结论。
要求:
(1)编写一个求解迷宫的非递归程序,并将求得的通路以三元组(i,j,d)的形式输出,其中: i,j指示迷宫中的一个坐标,d表示走到下一坐标的方向;
(2)编写递归形式的算法,求得迷宫中所有可能的通路;

迷宫问题的核心算法应该是回溯法,也可以说迷宫问题很好的体现了回溯法,运用深度优先搜索对所有节点以及其子节点进行遍历并标记每一步,如果没有解就返回上一个节点并撤销标记。

public class threeTuple {
     int i;
     int j;
     int k;
     threeTuple(int i, int j, int k){
         this.i = i;
         this.j = j;
         this.k = k;
     }
     public int getI(){
         return i;
     }

     public int getJ(){
         return j;
     }

     public int getK(){
         return k;
     }
}
public class Maze2 {

    private int[][] array = {
            {0, 0, 1, 1, 1, 1, 1, 0},
            {0, 1, 1, 0, 0, 0, 0, 0},
            {0, 0, 0, 0, 1, 1, 0, 1},
            {0, 1, 1, 1, 0, 1, 0, 1},
            {0, 0, 0, 1, 0, 0, 0, 1},
            {0, 1, 0, 0, 0, 1, 0, 1},
            {0, 1, 1, 1, 1, 0, 0, 1},
            {0, 0, 0, 0, 0, 1, 0, 1},
            {1, 1, 0, 1, 0, 0, 0, 0}

    };


    private int maxLine = 8;
    private int maxRow = 9;
    static int count = 0;
    static int step = 0;

    int tail = 0;
    static threeTuple [] sz = new threeTuple[100];

 /* 0表示向左
  * 1表示向右
  * 2表示向下
  * 3表示向上
  * */
    private void findPath(int i, int j) {


        //到达右下角出口
        if (i == maxRow - 1 && j == maxLine - 1) {

            //array[i][j] = 3;
            print();

            count++;
            return;
        }

        //向右走
        if (Move(i, j, i, j + 1)) {
            threeTuple sy = new threeTuple(i,j,1);
            sz[tail ++] = sy;
            array[i][j] = 3;
            findPath(i, j + 1);
            array[i][j] = 0;
            tail --;

        }
        //向左走
        if (Move(i, j, i, j - 1)) {
            threeTuple sy = new threeTuple(i,j,0);
            sz[tail ++] = sy;
            array[i][j] = 3;
            findPath(i, j - 1);
            array[i][j] = 0;
            tail --;

        }
        //向下走
        if (Move(i, j, i + 1, j)) {
            threeTuple sy = new threeTuple(i,j,2);
            sz[tail ++] = sy;
            array[i][j] = 3;
            findPath(i + 1, j);
            array[i][j] = 0;
            tail --;
        }
        //向上走
        if (Move(i, j, i - 1, j)) {
            threeTuple sy = new threeTuple(i,j,3);
            sz[tail ++] = sy;
            array[i][j] = 3;
            findPath(i - 1, j);
            array[i][j] = 0;
            tail --;
        }
    }

    private boolean Move(int i, int j, int I, int J) {

        if (I < 0 || J < 0 || I >= maxRow || J >= maxLine) {
            return false;
        }
        if (array[I][J] == 1) {
            return false;
        }
        if (array[I][J] == 3) {
            return false;
        }

        return true;
    }

    private void print() {

        for (int i = 0; i < maxRow; i++) {
            for (int j = 0; j < maxLine; j++) {
                System.out.print(array[i][j] + " ");   
            }
            System.out.println();
         }
        for(int i=0; i<tail; i++)
            System.out.println("("+sz[i].getI()+","+sz[i].getJ()+","+sz[i].getK()+")");

        System.out.println("一共"+(tail+1)+"步");
        System.out.println("**********************************");
    }
    public static void main(String[] args) {
        Maze2 mz = new Maze2();
        mz.findPath(0, 0);
        System.out.println("一共"+count+"个解");
        System.out.println();



    }
}
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
回溯算法可以很好地解决迷宫问题。下面是一个Java实现的示例代码: ```java import java.util.*; public class Maze { private int[][] maze; // 迷宫矩阵 private int rows; // 行数 private int cols; // 列数 private int startX; // 起点横坐标 private int startY; // 起点纵坐标 private int endX; // 终点横坐标 private int endY; // 终点纵坐标 private boolean[][] visited; // 记录走过的位置 public Maze(int[][] maze, int startX, int startY, int endX, int endY) { this.maze = maze; this.rows = maze.length; this.cols = maze[0].length; this.startX = startX; this.startY = startY; this.endX = endX; this.endY = endY; this.visited = new boolean[rows][cols]; } public void solve() { List<int[]> path = new ArrayList<>(); // 记录路径 boolean found = backtrack(startX, startY, path); if (found) { System.out.println("Solution found:"); for (int[] p : path) { System.out.print(Arrays.toString(p) + " "); } } else { System.out.println("No solution found."); } } private boolean backtrack(int x, int y, List<int[]> path) { if (x == endX && y == endY) { path.add(new int[]{x, y}); return true; } if (maze[x][y] == 1 || visited[x][y]) { return false; } path.add(new int[]{x, y}); visited[x][y] = true; if (x > 0 && backtrack(x - 1, y, path)) { return true; } if (x < rows - 1 && backtrack(x + 1, y, path)) { return true; } if (y > 0 && backtrack(x, y - 1, path)) { return true; } if (y < cols - 1 && backtrack(x, y + 1, path)) { return true; } path.remove(path.size() - 1); // 回溯 return false; } public static void main(String[] args) { int[][] maze = { {0, 1, 0, 0}, {0, 0, 0, 1}, {1, 0, 0, 0}, {1, 1, 0, 0}, {1, 1, 1, 0} }; Maze m = new Maze(maze, 0, 0, 4, 3); m.solve(); } } ``` 在这个示例代码中,我们使用了递归的回溯算法解决迷宫问题。当我们到达终点时,将路径添加到路径列表中并返回true。否则,我们按照上、下、左、右的顺序依次探索可行的路径。如果找到了一条可行路径,则返回true。如果所有的路径都无法到达终点,则回溯到上一步并返回false。最后,我们将找到的路径打印出来。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值