如何在java中找到迷宫的其他解决方案?

如何在java中找到迷宫的其他解决方案?,java,arrays,oop,stack,Java,Arrays,Oop,Stack,我需要编写一个程序,在给定的txt文件中获取迷宫,并将解决方案路径打印到控制台。我写了这个程序,你们可以在下面看到,但我只能找到一个解决方案。如果迷宫中有不止一个解,我需要找到所有这些解。我不知道我应该采取什么方法来解决这个问题。你能给个主意吗 以下是我的作品: maze.txt(作为参数发送) 驾驶员等级: import java.io.*; import java.util.Arrays; public class Driver { public static void main(

我需要编写一个程序,在给定的txt文件中获取迷宫,并将解决方案路径打印到控制台。我写了这个程序,你们可以在下面看到,但我只能找到一个解决方案。如果迷宫中有不止一个解,我需要找到所有这些解。我不知道我应该采取什么方法来解决这个问题。你能给个主意吗

以下是我的作品:

maze.txt(作为参数发送)

驾驶员等级:

import java.io.*;
import java.util.Arrays;

public class Driver {
    public static void main(String[] args) {

        //Reading source file
        int rowNum = 0, colNum = 0;
        File mazeFile = new File(args[0]);

        try (BufferedReader br = new BufferedReader(new FileReader(mazeFile))) {
            System.out.println("Input of Readed File:\n");
            String line;
            while ((line = br.readLine()) != null) {
                colNum = line.length();
                rowNum++;
                System.out.println(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        //creating new maze array
        char[][] maze = new char[rowNum][colNum];
        System.out.println();
        System.out.print("ROW: "+rowNum+" COL: "+colNum);

        //Setting maze's elements
        try (BufferedReader br = new BufferedReader(new FileReader(mazeFile))) {
            int readed,rNum=0,cNum=0;
            while ((readed = br.read()) != -1) {
                if(readed == 10){

                }
                else if(rNum<rowNum && cNum < colNum){
                    maze[rNum][cNum] = (char)readed;
                    cNum++;
                }
                else if(cNum >= colNum){
                    rNum++;
                    cNum=0;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        //Printing created maze...
        System.out.println("\nCreated Maze: \n");

        for (int i = 0; i<rowNum ; i++) {
            for (int j = 0; j < colNum; j++) {
                System.out.print(maze[i][j]);
            }
            System.out.println();
        }

        System.out.println("\nSolution: \n");
        //Creating myStack object for making stack operations
        Stack myStack = new Stack(1000);

        //Creating mazeSolver object for solving maze
        MazeSolver mazeSolver = new MazeSolver(myStack,maze,1,1,colNum-2,rowNum-2,rowNum,colNum);
        mazeSolver.solve();

        //Printing inside of our stack.
        //myStack.showElements();

        //Creating answer array
        char[][] answer = maze;

        //Our path is drawn by re-reading the stored data in our stack structure.
        for (int i = rowNum-1; i >=0; i--) {
            for (int j = colNum-1; j >=0; j--) {
                int x[] = myStack.peek();
                if(i == x[0] && j == x[1]){
                    answer[i][j] = '#';
                }
            }
        }

        //Minor visual improvements ...
        for (int i = 0; i<rowNum ; i++) {
            for (int j = 0; j < colNum; j++) {
                if(answer[i][j] == '1' || answer[i][j] == '0')
                    answer[i][j] = '.';
            }
        }

        //Printing our answer
        for (int i = 0; i<rowNum ; i++) {
            for (int j = 0; j < colNum; j++) {
                System.out.print(maze[i][j]);
            }
            System.out.println();
        }
    }
}
public class MazeSolver {
    Stack workStack;
    char[][] maze;
    int startPointX;
    int startPointY;
    int endPointX;
    int endPointY;
    int numberOfRows;
    int numberOfCols;
    static final char Wall = '1';
    static final char Free = '0';
    static final char Success = '#';

    public MazeSolver(Stack workStack, char[][] maze,int startingPointX, int startingPointY, int endPointX, int endPointY, int RowNum, int ColNum) {
        this.workStack = workStack;
        this.maze = maze;
        this.startPointX = startingPointX;
        this.startPointY = startingPointY;
        this.endPointX = endPointX;
        this.endPointY = endPointY;
        this.numberOfRows = RowNum;
        this.numberOfCols = ColNum;
        workStack.push(startPointY,startingPointX);
    }

    boolean canMoveEast(){
        if((maze[startPointY][startPointX + 1] == Free) && (startPointX + 1 <= numberOfCols))
        {
            return true;
        }
        else
            return false;
    }

    boolean canMoveWest(){
        if((maze[startPointY][startPointX - 1] == Free) && (startPointX - 1 <= numberOfCols)){
            return true;
        }
        else
            return false;
    }

    boolean canMoveNorth(){
        if((maze[startPointY-1][startPointX] == Free) && (startPointY - 1 <= numberOfRows)){
            return true;
        }
        else
            return false;
    }

    boolean canMoveSouth(){
        if((maze[startPointY+1][startPointX] == Free) && (startPointY + 1 <= numberOfRows)){
            return true;
        }
        else
            return false;
    }

    boolean canMoveNorthEast(){
        if((maze[startPointY-1][startPointX+1] == Free) && (startPointY - 1 <= numberOfRows) && (startPointX + 1 <= numberOfCols)){
            return true;
        }
        else
            return false;
    }

    boolean canMoveNorthWest(){
        if((maze[startPointY-1][startPointX-1] == Free) && (startPointY - 1 <= numberOfRows) && (startPointX - 1 <= numberOfCols)){
            return true;
        }
        else
            return false;
    }
    boolean canMoveSouthEast(){
        if((maze[startPointY+1][startPointX+1] == Free) && (startPointY + 1 <= numberOfRows) && (startPointX + 1 <= numberOfCols)){
            return true;
        }
        else
            return false;
    }
    boolean canMoveSouthWest(){
        if((maze[startPointY+1][startPointX-1] == Free) && (startPointY + 1 <= numberOfRows) && (startPointX - 1 <= numberOfCols)){
            return true;
        }
        else
            return false;
    }

    boolean solve()
    {
        maze[startPointY][startPointX] = Success;

        //Checked if we reached our goal
        if((startPointY == endPointY) && (startPointX == endPointX)){
            return true;
        }

        if(canMoveEast()){
            workStack.push(startPointY,startPointX+1);
            startPointX++;
            solve();
        }
        else if(canMoveWest()){
            workStack.push(startPointY,startPointX-1);
            startPointX--;
            solve();
        }
        else if(canMoveNorth()){
            workStack.push(startPointY-1,startPointX);
            startPointY--;
            solve();
        }
        else if(canMoveSouth()){
            workStack.push(startPointY+1,startPointX);
            startPointY++;
            solve();
        }
        else if(canMoveNorthEast()){
            workStack.push(startPointY-1,startPointX+1);
            startPointY--;
            startPointX++;
            solve();
        }
        else if(canMoveNorthWest()){
            workStack.push(startPointY-1,startPointX-1);
            startPointY--;
            startPointX--;
            solve();
        }
        else if(canMoveSouthEast()){
            workStack.push(startPointY+1,startPointX+1);
            startPointY++;
            startPointX++;
            solve();
        }
        else if(canMoveSouthWest()){
            workStack.push(startPointY+1,startPointX-1);
            startPointY++;
            startPointX--;
            solve();
        }
        else if(true){
            try {
                maze[startPointY][startPointX] = Wall;
                int[] back = workStack.pop();
                startPointY = back[0];
                startPointX = back[1];
                solve();
            } catch (Exception e) {
                System.out.println("There is no solution!");
                System.exit(0);
            }
        }

        return false;
    }
}
我需要的输出:

Input of Readed File:

11111111111111111
10110011000111111
11001110111001111
10110001011100111
11101111011011001
11101001011011111
11011011011001011
10111100111110111
11011011011111101
11100111011000011
10011110100111101
10100110111111101
11111111111111111

ROW: 13 COL: 17
Created Maze: 

11111111111111111
10110011000111111
11001110111001111
10110001011100111
11101111011011001
11101001011011111
11011011011001011
10111100111110111
11011011011111101
11100111011000011
10011110100111101
10100110111111101
11111111111111111

Solution 1: 

.................
.#...............
..##...#.........
....###.#........
........#........
........#........
........#........
.......#.........
........#........
........#..####..
.........##....#.
...............#.
.................

Solution 2:

.................
.#...............
..##.............
....#............
...#.............
...#.............
..#..............
.#....##.........
..#..#..#........
...##...#..####..
.........##....#.
...............#.
.................

Process finished with exit code 0


您想要的结果是“各种解决方案可以通过(东北、西北)到(东南、西南)”,并且您需要使用堆栈进行解决? 如果是这样的话,我建议您使用两个堆栈,一个用于保存所有可能性(将所有toEast、toEast等存储在您可以去的地方),一个用于保存当前正在进行的操作(每个可能的解决方案都作为缓冲区)

只需添加将当前进程保存在缓冲区中的逻辑,并在原始代码上作为解决方案时打印路径。如果它不是一个解决方案,并且无法到达(东南、西南),则回溯并恢复缓冲区堆栈。对于这种逻辑,您将需要另一个堆栈保存位置,您最后一次从不同方向选择该位置

总之,

Stack1 => to save all possibilities
Stack2 => current paths. If not a solution, delete and restore
Stack3 => where you chose one direction from many. Need to traceback the path.

Stack2 copies Stack1 whenever you progress,
when reach the goal you print your Stack2 as a solution,
if not, pop until your latest decision informed by popping Stack3.

@到底什么是有缺陷的部分,先生?我写的程序是一个可以局部求解的程序,包括中间方向(东北、西北、东南、西南)。@DevilsHnd我通过回溯过程一个接一个地保留堆栈构造中的运动阶段,并通过获取堆栈构造的内容重写我的迷宫。但这一次,当行数和列数匹配时,它打印的是#,而不是1或0。对于剩余的位置,按“口述”。
Input of Readed File:

11111111111111111
10110011000111111
11001110111001111
10110001011100111
11101111011011001
11101001011011111
11011011011001011
10111100111110111
11011011011111101
11100111011000011
10011110100111101
10100110111111101
11111111111111111

ROW: 13 COL: 17
Created Maze: 

11111111111111111
10110011000111111
11001110111001111
10110001011100111
11101111011011001
11101001011011111
11011011011001011
10111100111110111
11011011011111101
11100111011000011
10011110100111101
10100110111111101
11111111111111111

Solution: 

.................
.#...............
..##...#.........
....###.#........
........#........
........#........
........#........
.......#.........
........#........
........#..####..
.........##....#.
...............#.
.................

Process finished with exit code 0

Input of Readed File:

11111111111111111
10110011000111111
11001110111001111
10110001011100111
11101111011011001
11101001011011111
11011011011001011
10111100111110111
11011011011111101
11100111011000011
10011110100111101
10100110111111101
11111111111111111

ROW: 13 COL: 17
Created Maze: 

11111111111111111
10110011000111111
11001110111001111
10110001011100111
11101111011011001
11101001011011111
11011011011001011
10111100111110111
11011011011111101
11100111011000011
10011110100111101
10100110111111101
11111111111111111

Solution 1: 

.................
.#...............
..##...#.........
....###.#........
........#........
........#........
........#........
.......#.........
........#........
........#..####..
.........##....#.
...............#.
.................

Solution 2:

.................
.#...............
..##.............
....#............
...#.............
...#.............
..#..............
.#....##.........
..#..#..#........
...##...#..####..
.........##....#.
...............#.
.................

Process finished with exit code 0

Stack1 => to save all possibilities
Stack2 => current paths. If not a solution, delete and restore
Stack3 => where you chose one direction from many. Need to traceback the path.

Stack2 copies Stack1 whenever you progress,
when reach the goal you print your Stack2 as a solution,
if not, pop until your latest decision informed by popping Stack3.