首页 文章

在java中使用递归(2个异常)解决迷宫

提问于
浏览
-2

我在这个问题上需要帮助 . 我想使用递归来求解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();
        }
    }

}

3 回答

  • 0

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

    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);
        }
    }
    
  • 0

    在您第一次调用 solveMaze 时,第二个 if 为真((0,0)是安全的并且存在1),因此您返回 true 而没有打印任何内容 .

    也许如果你解释了它的目的,可以帮助修复它(可能是通过删除它) .

  • 1

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

    我错了 . 斯科特在下面给出了正确答案 .

相关问题