Java 递归求解迷宫
我知道以前有人问过这个问题,但我就是看不到答案。我应该编写递归迷宫解决方案,以下是我到目前为止所做的工作:Java 递归求解迷宫,java,recursion,maze,Java,Recursion,Maze,我知道以前有人问过这个问题,但我就是看不到答案。我应该编写递归迷宫解决方案,以下是我到目前为止所做的工作: import java.awt.Color; import java.util.ArrayList; public class RecursiveMazeSolution implements MazeSolver { boolean[][] marked; ArrayList<Maze.Door> solutionPath = new ArrayList<>()
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;
}
}
导入java.awt.Color;
导入java.util.ArrayList;
公共类递归MazeSolution实现MazeSolver{
布尔[][]标记;
ArrayList solutionPath=新的ArrayList();
公共阵列列表解算迷宫(int startRow、int finishRow、int startCol、int finishCol、迷宫迷宫){
标记=新布尔值[maze.getRows()][maze.getColumns()];
返回迷宫(startRow、finishRow、startCol、finishCol、迷宫、标记);
}
公共阵列列表解算迷宫(int startRow、int finishRow、int startCol、int finishCol、迷宫迷宫、布尔[][]标记){
System.out.println(startRow+“”+startCol+“”+finishRow+“”+finishCol);
if(startRow<0 | | startCol<0 | | startRow>maze.getRows()-1 | | startCol>maze.getColumns()-1)返回null;
如果(标记为[startRow][startCol]){
System.out.println(“我在里面标记为if”);
返回null;
}
标记为[startRow][startCol]=true;
如果(startRow==finishRow&&startCol==finishCol){
添加(新迷宫门(startRow,startCol,迷宫编号方向,颜色.红色));
返回解路径;
}
if(solveMaze(startRow-1,finishRow,startCol,finishCol,maze,marked)!=null&&!maze.isClosed(startRow,startCol,maze.NORTH)){
添加(新迷宫门(startRow,startCol,Maze.NORTH,Color.RED));
返回解路径;
}
if(solveMaze(startRow+1,finishRow,startCol,finishCol,maze,marked)!=null&&!maze.isClosed(startRow,startCol,maze.SOUTH)){
添加(新的Maze.Door(startRow、startCol、Maze.SOUTH、Color.RED));
返回解路径;
}
if(solveMaze(startRow,finishRow,startCol-1,finishCol,maze,marked)!=null&&!maze.isClosed(startRow,startCol,maze.WEST)){
添加(新的Maze.Door(startRow、startCol、Maze.WEST、Color.RED));
返回解路径;
}
if(solveMaze(startRow,finishRow,startCol+1,finishCol,maze,marked)!=null&&!maze.isClosed(startRow,startCol,maze.EAST)){
添加(新的Maze.Door(startRow、startCol、Maze.EAST、Color.RED));
返回解路径;
}
返回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
import java.io.Serializable;
导入java.awt.Color;
公共类迷宫实现可序列化{
/**
*
*/
私有静态最终长serialVersionUID=-787488019846627488L;
/**
*房间的北墙
*/
公共静态最终int北=0;
/**
*房间的东墙
*/
公共静态最终int EAST=1;
/**
*房间的南墙
*/
公共静态最终int南=2;
/**
*房间的西墙
*/
公共静态最终int WEST=3;
/**
*房间里没有方向
*/
公共静态最终int NO_方向=4;
私有静态字符串[]墙={“北”、“东”、“南”、“西”};
私人房间[][]间;
/**
*建造师
*@param rows是迷宫中的行数
*@param columns是迷宫中的列数
*/
公共迷宫(int行、int列){
房间=新房间[行][列];
对于(int i=0;i
我认为我找到了解决方案,但我的程序只是递归运行了一段时间,然后没有找到迷宫的解决方案。此外,如果我让我的程序忽略“墙”,它可以找到一个解决方案(在多次递归调用之后),但它不应该忽略墙。有人能告诉我我做错了什么吗?快速浏览一下您的代码,我认为问题在于您没有在
solveMaze
中取消已访问方块的标记。当你进入该功能时,你会标记一个方块,表示你不能再次访问它。但是你需要把它标记为自由ag
if( !maze.isClosed(startRow, startCol, Maze.NORTH) &&
solveMaze(startRow-1, finishRow, startCol, finishCol,maze, marked) != null )