繁体   English   中英

在java中使用递归(2个方向)解决迷宫

[英]Solving maze using recursion (2 disrections) in java

我有点需要这个问题的帮助。 我想使用递归来解决 NxN 二进制矩阵。 问题是我认为我的递归实现在某种程度上不正确。 在这个问题中,我只允许向右和向下走。 我检查了 issafe() 方法,根据 1=true 和 0=false,一切似乎都返回 true 或 false。 如果我运行运行程序,则不显示任何内容。 任何帮助将非常感激。

public class Main {

    public static void main(String[] args) {

        int maze[][] = {{1, 0, 0, 0},
                        {1, 1, 0, 1},
                        {0, 1, 0, 0},
                        {1, 1, 1, 2}
        };
Maze rat = new Maze();
rat.solveMaze(maze, 0, 0);



    }

}

public class Maze {
    int maze[][];
    int mazeSize;
    int EXIT=2;

    public Maze() {

        mazeSize=4;
        maze = new int[mazeSize][mazeSize];
    }

    // check is its safe to traverse

    public Boolean isSafe(int x, int y, int maze[][]){
        if (x>=0 && x<mazeSize && y>=0 && y<mazeSize && maze[x][y]==1){
            return true;
        }
        else return false;
    }


    boolean solveMaze(int maze[][],int x,int y){
        int solmaze[][]=    {   {0, 0, 0, 0},
                                {0, 0, 0, 0},
                                {0, 0, 0, 0},
                                {0, 0, 0, 0}};

        if(maze[x][y]==EXIT){
            solmaze[x][y]=1;
            printmaze(solmaze);
            return true;
        }

        if(isSafe(x, y,maze) && maze[x][y]==1){
            solmaze[x][y]=1;
            return true;
        }

        if(isSafe(x, y,maze)==true && solveMaze(maze,x+1,y)==true){// down

            solmaze[x][y]=1;
        }
        if(isSafe(x, y,maze)==true && solveMaze(maze,x,y+1)==true){//right

            solmaze[x][y]=1;
        }
        solmaze[x][y]=0;
        return false;
    }

    void printmaze(int maze[][]){//print maze
        for(int i=0;i<maze.length;i++){
            for(int j=0;j<maze.length;j++){
                System.out.print(maze[i][j]);
            }
            System.out.println();
        }
    }

}

我相信这是您正在寻找的解决方案:

public class Main2 {

    public static void main(String[] args) {

        int maze[][] = {{1, 0, 0, 0},
                {1, 1, 0, 1},
                {0, 1, 0, 0},
                {1, 1, 1, 2}
        };
        Maze rat = new Maze();
        rat.solveAndPrintMaze(maze, 0, 0);
    }

}

public class Maze {
    int maze[][];
    int mazeSize;
    int EXIT=2;

    public Maze() {

        mazeSize=4;
        maze = new int[mazeSize][mazeSize];
    }

    // check is its safe to traverse
    public Boolean isSafe(int x, int y, int maze[][]){
        if (x>=0 && x<mazeSize && y>=0 && y<mazeSize && maze[x][y]>=1){
            return true;
        }
        else return false;
    }

    int solmaze[][]= {
            {0, 0, 0, 0},
            {0, 0, 0, 0},
            {0, 0, 0, 0},
            {0, 0, 0, 0}};

    boolean solveMaze(int maze[][],int x,int y){

        if(maze[x][y]==EXIT){
            solmaze[x][y]=1;
//          printmaze(solmaze);
            return true;
        }

//      if(isSafe(x, y,maze) && maze[x][y]==1){
//          solmaze[x][y]=1;
//          return true;
//      }

        if(isSafe(x+1, y,maze)==true && solveMaze(maze,x+1,y)==true){// down

            solmaze[x][y]=1;
            return true;
        }

        if(isSafe(x, y+1,maze)==true && solveMaze(maze,x,y+1)==true){//right

            solmaze[x][y]=1;
            return true;
        }

        solmaze[x][y]=0;
        return false;
    }

    void printmaze(int maze[][]){//print maze
        for(int i=0;i<maze.length;i++){
            for(int j=0;j<maze.length;j++){
                System.out.print(maze[i][j]);
            }
            System.out.println();
        }
    }

    void solveAndPrintMaze(int maze[][],int x,int y) {
        solveMaze(maze, x, y);
        printmaze(solmaze);
    }
}

在您第一次调用solveMaze ,第二个if为真((0,0) 是安全的,那里有一个 1),因此您无需打印任何内容就返回true

也许如果你解释了这是要做什么,人们可以帮助修复它(这很可能是通过删除它)。

您实际上并没有在这里尝试递归。 要以您尝试的方式启动递归,您必须从其内部调用您的 solveMaze 方法。

我错了。 下面斯科特给出了正确的答案。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM