0

我知道以前有人问过这个问题,但我根本看不到答案。我应该编写递归迷宫解决方案,这是我到目前为止所做的:

import java.awt.Color;
import java.util.ArrayList;

public class RecursiveMazeSolution implements MazeSolver {
boolean[][] marked;
ArrayList<Maze.Door> solutionPath = new ArrayList<>();

public ArrayList<Maze.Door> solveMaze(int startRow, int finishRow, int startCol, int finishCol, Maze maze) {
    marked = new boolean[maze.getRows()][maze.getColumns()];
    return solveMaze(startRow, finishRow, startCol, finishCol, maze, marked);
}

public ArrayList<Maze.Door> solveMaze(int startRow, int finishRow, int startCol, int finishCol, Maze maze, boolean[][] marked) {
    System.out.println(startRow + " " + startCol + " " + finishRow + " " + finishCol);
    if(startRow < 0 || startCol < 0 || startRow > maze.getRows() - 1|| startCol > maze.getColumns() - 1) return null;
    if(marked[startRow][startCol]) {
        System.out.println("I'm inside marked if");
        return null;
    }

    marked[startRow][startCol] = true;
    if(startRow == finishRow && startCol == finishCol) {
        solutionPath.add(new Maze.Door(startRow, startCol, Maze.NO_DIRECTION, Color.RED));
        return solutionPath;
    }

    if(solveMaze(startRow - 1, finishRow, startCol, finishCol,maze, marked) != null && !maze.isClosed(startRow, startCol, Maze.NORTH)) {
        solutionPath.add(new Maze.Door(startRow, startCol, Maze.NORTH, Color.RED));
        return solutionPath;
    }
    if(solveMaze(startRow + 1, finishRow, startCol, finishCol,maze, marked) != null && !maze.isClosed(startRow, startCol, Maze.SOUTH)){
        solutionPath.add(new Maze.Door(startRow, startCol, Maze.SOUTH, Color.RED));
        return solutionPath;
    }
    if(solveMaze(startRow, finishRow, startCol - 1, finishCol,maze, marked) != null && !maze.isClosed(startRow, startCol, Maze.WEST)){
        solutionPath.add(new Maze.Door(startRow, startCol, Maze.WEST, Color.RED));
        return solutionPath;
    }
    if(solveMaze(startRow, finishRow, startCol + 1, finishCol,maze, marked) != null && !maze.isClosed(startRow, startCol, Maze.EAST)){
        solutionPath.add(new Maze.Door(startRow, startCol, Maze.EAST, Color.RED));
        return solutionPath;
    }

    return null;
}


}

这是提供给我的迷宫课程:

import java.io.Serializable;
import java.awt.Color;

public class Maze implements Serializable {

/**
 * 
 */
private static final long serialVersionUID = -787488019846627488L;
/**
 * the north wall of a room
 */
public static final int NORTH = 0;
/**
 * the east wall of a room
 */
public static final int EAST = 1;
/**
 * the south wall of a room
 */
public static final int SOUTH = 2;
/**
 * the west wall of a room
 */
public static final int WEST = 3;
/**
 * No direction from a room
 */
public static final int NO_DIRECTION = 4;
private static String[] walls = {"North", "East", "South", "West"};
private Room[][] rooms;

/**
 * Constructor
 * @param rows is the number of rows in the maze
 * @param columns is the number of columns in the maze
 */
public Maze(int rows, int columns) {
    rooms = new Room[rows][columns];
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < columns; j++) {
            rooms[i][j] = new Room();
        } // end for
    } // end for
}

/**
 * rows accessor
 * @return the number of rows in the maze
 */
public int getRows() {
    return rooms.length;
}

/**
 * columns accessor
 * @return the number of columns in the maze
 */
public int getColumns() {
    return rooms[0].length;
}

/**
 * Checks to see if a wall is closed
 * @param row the row number
 * @param column the column number
 * @param wall the wall number
 * @return true if wall is closed; false if it is open
 */
public boolean isClosed(int row, int column, int wall) {
    return rooms[row][column].closed[wall];
}

/**
 * Opens the wall
 * @param row the row number
 * @param column the column number
 * @param wall the wall number
 */
public void setOpen(int row, int column, int wall) {
    rooms[row][column].closed[wall] = false;
}

/**
 * Closes the wall
 * @param row the row number
 * @param column the column number
 * @param wall the wall number
 */
public void setClosed(int row, int column, int wall) {
    rooms[row][column].closed[wall] = true;
}

public static class Door {

    int row;
    int column;
    int wall;
    Color color;

    public Door(int row, int column, int wall, Color color) {
        this.row = row;
        this.column = column;
        this.wall = wall;
        this.color = color;
    } // end constructor

    public boolean equals(Object x) {
        if ( x == null ) return false;
        if (!(x.getClass().equals(this.getClass()))) {
            return false;
        }
        Door door = (Door) x;
        return row == door.row && column == door.column && wall == door.wall && color.equals(door.color);
    } // end equal

    public int hashCode() {
        return row + column + wall + color.hashCode();
    }

    public String toString() {
        return row + " " + column + " " + walls[wall] + "\n";
    } // end toString()
} // end Door

private class Room implements Serializable {

    boolean[] closed;

    Room() {
        closed = new boolean[4];
        for (int i = 0; i < 4; i++) {
            closed[i] = true;
        } // end for
    }
} // end Room
} // end Maze

我认为我找到了解决方案的正确方法,但是我的程序只是递归地运行了一段时间,然后找不到迷宫的解决方案。此外,如果我让我的程序忽略“墙壁”,它可以找到解决方案(经过大量递归调用),但它不应该忽略墙壁。谁能告诉我我做错了什么?

4

1 回答 1

1

基于对您的代码的快速浏览,我认为问题在于您没有取消标记solveMaze. 当您进入该功能时,您会标记一个正方形,表示您不能再次访问它。但是您需要在返回之前再次将其标记为免费。

在考虑了更多之后,似乎它通常不应该是一个问题,因为您会在确定没有通过该正方形的解决方案路径之后取消标记该正方形。

然后我意识到你在递归调用之后正在做墙壁测试。这意味着您正在寻找解决方案,然后放弃解决方案,因为途中有一堵墙。同时,您将所有方块标记为已访问,并且无处可寻有效解决方案。

您需要在递归之前测试墙壁,如果有墙壁,则不要进行递归。短路评估在这里就足够了(通过重新排序if语句中的术语):

if( !maze.isClosed(startRow, startCol, Maze.NORTH) &&
    solveMaze(startRow-1, finishRow, startCol, finishCol,maze, marked) != null )

顺便说一句,不需要marked作为参数传递。是班级成员。

于 2013-03-06T09:12:21.190 回答