📜  迷宫中的老鼠的Java程序|回溯-2

📅  最后修改于: 2022-05-13 01:58:09.513000             🧑  作者: Mango

迷宫中的老鼠的Java程序|回溯-2

我们在第 1 组中讨论了回溯和骑士的旅行问题。让我们讨论迷宫中的老鼠作为另一个可以使用回溯解决的示例问题。

迷宫以 N*N 块的二进制矩阵形式给出,其中源块是最左上角的块,即 maze[0][0],目标块是最右下角的块,即 maze[N-1][N-1 ]。老鼠从源头出发,必须到达目的地。老鼠只能在两个方向上移动:向前和向下。

在迷宫矩阵中,0 表示该块是死路,1 表示该块可以在从源到目的地的路径中使用。请注意,这是典型迷宫问题的简单版本。例如,更复杂的版本可以是老鼠可以在 4 个方向上移动,而更复杂的版本可以是有限数量的移动。

以下是一个示例迷宫。

Gray blocks are dead ends (value = 0). 

以下是上述迷宫的二进制矩阵表示。

{1, 0, 0, 0}
                {1, 1, 0, 1}
                {0, 1, 0, 0}
                {1, 1, 1, 1}

以下是带有突出显示解决方案路径的迷宫。

以下是上述输入矩阵的解矩阵(程序的输出)。

{1, 0, 0, 0}
                {1, 1, 0, 0}
                {0, 1, 0, 0}
                {0, 1, 1, 1}
 All entries in solution path are marked as 1.
Java
/* Java program to solve Rat in a Maze problem using
   backtracking */
 
public class RatMaze {
    final int N = 4;
 
    /* A utility function to print solution matrix
       sol[N][N] */
    void printSolution(int sol[][])
    {
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++)
                System.out.print(" " + sol[i][j] + " ");
            System.out.println();
        }
    }
 
    /* A utility function to check if x, y is valid
        index for N*N maze */
    boolean isSafe(int maze[][], int x, int y)
    {
        // if (x, y outside maze) return false
        return (x >= 0 && x < N && y >= 0 && y < N && maze[x][y] == 1);
    }
 
    /* This function solves the Maze problem using
       Backtracking. It mainly uses solveMazeUtil()
       to solve the problem. It returns false if no
       path is possible, otherwise return true and
       prints the path in the form of 1s. Please note
       that there may be more than one solutions, this
       function prints one of the feasible solutions.*/
    boolean solveMaze(int maze[][])
    {
        int sol[][] = { { 0, 0, 0, 0 },
                        { 0, 0, 0, 0 },
                        { 0, 0, 0, 0 },
                        { 0, 0, 0, 0 } };
 
        if (solveMazeUtil(maze, 0, 0, sol) == false) {
            System.out.print("Solution doesn't exist");
            return false;
        }
 
        printSolution(sol);
        return true;
    }
 
    /* A recursive utility function to solve Maze
       problem */
    boolean solveMazeUtil(int maze[][], int x, int y,
                          int sol[][])
    {
        // if (x, y is goal) return true
        if (x == N - 1 && y == N - 1) {
            sol[x][y] = 1;
            return true;
        }
 
        // Check if maze[x][y] is valid
        if (isSafe(maze, x, y) == true) {
            // mark x, y as part of solution path
            sol[x][y] = 1;
 
            /* Move forward in x direction */
            if (solveMazeUtil(maze, x + 1, y, sol))
                return true;
 
            /* If moving in x direction doesn't give
               solution then  Move down in y direction */
            if (solveMazeUtil(maze, x, y + 1, sol))
                return true;
 
            /* If none of the above movements works then
               BACKTRACK: unmark x, y as part of solution
               path */
            sol[x][y] = 0;
            return false;
        }
 
        return false;
    }
 
    public static void main(String args[])
    {
        RatMaze rat = new RatMaze();
        int maze[][] = { { 1, 0, 0, 0 },
                         { 1, 1, 0, 1 },
                         { 0, 1, 0, 0 },
                         { 1, 1, 1, 1 } };
        rat.solveMaze(maze);
    }
}
// This code is contributed by Abhishek Shankhadhar


输出:
1  0  0  0 
1  1  0  0 
0  1  0  0 
0  1  1  1

请参阅有关迷宫中老鼠的完整文章 | Backtracking-2 了解更多详情!