我一直在try 制作一个可以递归解迷宫的程序.但是我的算法中的递归有一个问题.该算法只能求解一个位置的解.

控制台上有:

Note "char indicators" for the 2D array:

  • "*"=墙壁
  • "#"=开始
  • "$"=结束
  • ''=可能的路径/不是墙
  • "@"=路径
  • "~"=deadend

期望输出:

Presolved maze:
* * * * * * * * * * 
* # * * * * * * * * 
*   *           * * 
*   *       *   * * 
*       *   *   * * 
* * *       *   * * 
* *     *   *   * * 
* *     *   *     * 
* * * * * * *     * 
*               $ * 
* * * * * * * * * * 

Maze solution:

* * * * * * * * * * 
* # * * * * * * * * 
* @ * @ @ @ @ @ * * 
* @ * @ ~ ~ * @ * * 
* @ @ @ * ~ * @ * * 
* * * ~ ~ ~ * @ * * 
* * ~ ~ * ~ * @ * * 
* * ~ ~ * ~ * @ ~ * 
* * * * * * * @ ~ * 
* ~ ~ ~ ~ ~ ~ @ $ * 
* * * * * * * * * *

我得到的输出:

Presolved maze:
* * * * * * * * * * 
* # * * * * * * * * 
*   *           * * 
*   *       *   * * 
*       *   *   * * 
* * *       *   * * 
* *     *   *   * * 
* *     *   *     * 
* * * * * * *     * 
*               $ * 
* * * * * * * * * * 

Maze solution:

* * * * * * * * * * 
* # * * * * * * * * 
* @ *           * * 
*   *       *   * * 
*       *   *   * * 
* * *       *   * * 
* *     *   *   * * 
* *     *   *     * 
* * * * * * *     * 
*               $ * 
* * * * * * * * * *

我try 过其他解决方案,比如在我的一般情况下删除递归中的递归,但这只是倒退了我的算法可以做的小进步(即找到一个位置).我想知道我还能做些什么来解决这个问题?其他方法也可以,但"算法"的工作方式与我希望的不一样.

import java.util.*;
public class maze {
    public static void main(String[] args) {
        allocateMaze();
    }
    
    public static void allocateMaze() {
        char[][] mazeArr = {{'*','*','*','*','*','*','*','*','*','*'},//0
                            {'*','#','*','*','*','*','*','*','*','*'},//1
                            {'*',' ','*',' ',' ',' ',' ',' ','*','*'},//2
                            {'*',' ','*',' ',' ',' ','*',' ','*','*'},//3
                            {'*',' ',' ',' ','*',' ','*',' ','*','*'},//4
                            {'*','*','*',' ',' ',' ','*',' ','*','*'},//5
                            {'*','*',' ',' ','*',' ','*',' ','*','*'},//6
                            {'*','*',' ',' ','*',' ','*',' ',' ','*'},//7
                            {'*','*','*','*','*','*','*',' ',' ','*'},//8
                            {'*',' ',' ',' ',' ',' ',' ',' ','$','*'},//9
                            {'*','*','*','*','*','*','*','*','*','*'}};//10
        
        //setting row and col to 0 for display method
        int row = 0;
        int col = 0;
        System.out.println("Presolved maze:");
        displayMaze(mazeArr, row, col); //displays presolved maze
        row = 1;
        col = 1;
        boolean isSolved = false;
        System.out.println("\nMaze solution:");
        algorithm(mazeArr, row, col, isSolved); //create variable for solved maze, sends maze allocation to method that solves the maze
        System.out.println();
        row = 0;
        col = 0;
        displayMaze(mazeArr, row, col); //displays traced + solved maze
    }

    public static void displayMaze(char[][] mazeArr, int row, int col) {
        if (row < mazeArr.length) { //iterate through all (11) rows
            if (col < mazeArr[row].length) { //iterate through all (11) columns
                System.out.print(mazeArr[row][col] + " "); //displays the current index in the array
                displayMaze(mazeArr, row, col+1); //repeats this method by iterating through the columns first
                return;
            }
            System.out.println(); //enters the line after each row for display purposes
            displayMaze(mazeArr, row+1, col=0); //repeats this method by iterating through the rows
        }
    }
    
    public static char[][] algorithm(char[][] mazeArr, int row, int col, boolean isSolved){
        boolean isPath = false; // assume there is no path

        if (mazeArr[row][col] == '$' && isSolved == true) { // IF MAZE IS COMPLETELY SOLVED
            return mazeArr;

        } else { // IF MAZE IS NOT COMPLETELY SOLVED
            
            if (isSolved == false && isPath == false) { // start searching thru the maze, assume there is no path
                // THERE IS NO DEAD END
                
                if (mazeArr[row - 1][col] == ' ') { // if north has a path
                    mazeArr[row - 1][col] = '@'; // block off path to not go back here again
                    isPath = true; // there is a path
                    isSolved = false; // assume maze is still not solved
                    
                    return algorithm(mazeArr, row--, col, isSolved); // repeat process going to next north spot
                }
                
                if (mazeArr[row][col + 1] == ' ') { // if east has a path
                    mazeArr[row][col + 1] = '@'; // block off path to not go back here again
                    isPath = true; // there is a path
                    isSolved = false; // assume maze is still not solved

                    return algorithm(mazeArr, row, col++, isSolved); // repeat process going to next east spot
                }
                
                if (mazeArr[row + 1][col] == ' ') { // if south has a path
                    mazeArr[row + 1][col] = '@'; // block off path to not go back here again
                    isPath = true; // there is a path
                    isSolved = false; // assume maze is still not solved
                    
                    return algorithm(mazeArr, row++, col, isSolved); // repeat process going to next south spot
                }
                
                if (mazeArr[row][col - 1] == ' ') { // if west has a path
                    mazeArr[row][col - 1] = '@'; // block off path to not go back here again
                    isPath = true; // there is a path
                    isSolved = false; // assume maze is still not solved
                    
                    return algorithm(mazeArr, row, col--, isSolved); // repeat process going to next west spot
                }
                
                if (mazeArr[row][col] == '$') { // if you have reached the end of the maze
                    isSolved = true; // maze has been solved
                    
                    
                    return algorithm(mazeArr, row, col, isSolved); // algorithm will recognize
                }

            } else { // finds alternate path if there's a dead end
                if (mazeArr[row][col] != '#') {
                mazeArr[row][col] = '~'; //indicates that this index is a dead end
                }
                
                if (isPath == false) {
                    if (mazeArr[row - 1][col] == '@' && mazeArr[row - 1][col] != '#') { // if north was a path
                        isPath = true; 
                        isSolved = false;
                        
                        return algorithm(mazeArr, row--, col, isSolved); //returns to initial position before hitting dead end
                    }
                }
                
                if (isPath == false) {
                    if (mazeArr[row][col+1] == '@' && mazeArr[row][col+1] != '#') { // if east was a path
                        isPath = true; 
                        isSolved = false;
                        
                        return algorithm(mazeArr, row, col++, isSolved); //returns to initial position before hitting dead end
                    }
                }
                
                if (isPath == false) {
                    if (mazeArr[row + 1][col] == '@' && mazeArr[row + 1][col] != '#') { // if south was a path
                        isPath = true; 
                        isSolved = false;
                        
                        return algorithm(mazeArr, row++, col, isSolved); //returns to initial position before hitting dead end
                    }
                }
                
                if (isPath == false) {
                    if (mazeArr[row][col-1] == '@' && mazeArr[row][col-1] != '#') { // if west was a path
                        isPath = true; 
                        
                        return algorithm(mazeArr, row, col--, isSolved); //returns to initial position before hitting dead end
                    }
                }
                
                if (isPath == false) { // if there's no way out, that means there is no solution
                    System.out.println("No Solution");              
                    return mazeArr;
                }
                
            }
            return mazeArr;
        }
    }
}

推荐答案

当你回忆起algorithm()时,你需要把++--放在rowcol之前.当其中一个在变量之后时,变量会增加,但不会增加输出的值:

public class Test {
    public static void main(String[] args) {
        int x = 5;
        int y = x++;
        System.out.println(x + " " + y);
    }
}

给予

6 5

高于x++会增加x,但之前y被设置为x.要修正它,将增量放在变量之前,比如++row:

public class Test {
    public static void main(String[] args) {
        int x = 5;
        int y = ++x;
        System.out.println(x + " " + y);
    }
}

输出:

6 6

Java相关问答推荐

如何在返回bigint []值的子查询中使用any?

多个Java线程和TreeMap.put()的非预期行为

@ IdClass with @ Inheritance(策略= InheritanceType. SINGLE_TABLE)

Java inline Double条件和值解装箱崩溃

对于几乎不涉及逻辑的请求,您是否应该使用命令模式?

从Spring5迁移到Spring6:无法在雅加达包中找到类

Spring Data JPA慢慢地创建了太多非活动会话

Jenv-相同的Java版本,但带有前缀

Log4j与jdk21兼容吗?

是否为计划任务补偿系统睡眠?

无法使用Freemarker从XML中读取重复的标记值

从12小时开始的日期模式

为什么mvn编译生命周期阶段不只是编译已更改的java文件?

如何设置默认序列生成器分配大小

如果c不为null,Arrays.sort(T[]a,Comparator<;?super T>;c)是否会引发ClassCastException?

Android上的SQLite:Android.database.SQLite.SQLiteReadOnlyDatabaseException:try 写入只读数据库(代码1032 SQLite_readonly_DBMOVED)

如何使用stream.allMatch()为空流返回false?

JavaFX中ListView中的问题

Maven创建带有特定类的Spring Boot jar和普通jar

如何在 IntelliJ 中获取 Visual Studio Code 的 Dark Modern Java colored颜色 语法