Java 设置参观房间的迷宫求解回溯算法问题
我在寻找是否有人可以帮助我完成房间搜索算法Java 设置参观房间的迷宫求解回溯算法问题,java,algorithm,maze,backtracking,Java,Algorithm,Maze,Backtracking,我在寻找是否有人可以帮助我完成房间搜索算法 我正在尝试实现一个回溯算法来解决迷宫问题。我被困在了应该记住我已经去过的房间的地方。 迷宫由四个房间组成,每个房间有四个面——北、东、南和西。每个房间都通过创建一个到所需侧的门链接到下一个房间,即room1.createNorth(roomName),它在北面创建了一个新房间,并且一个新房间有南门链接回第一个房间,正如您在“我的房间”类中看到的那样 这是我的切碎房间类,代表迷宫中的每个房间。我删除了南、西和东方向,这与我处理北面的方法相同 public
我正在尝试实现一个回溯算法来解决迷宫问题。我被困在了应该记住我已经去过的房间的地方。
迷宫由四个房间组成,每个房间有四个面——北、东、南和西。每个房间都通过创建一个到所需侧的门链接到下一个房间,即
room1.createNorth(roomName)
,它在北面创建了一个新房间,并且一个新房间有南门链接回第一个房间,正如您在“我的房间”类中看到的那样
这是我的切碎房间类,代表迷宫中的每个房间。我删除了南、西和东方向,这与我处理北面的方法相同
public class Room {
private String name;
private Room north;
private Room east;
private Room west;
private Room south;
private boolean isExit = false;
private Maze maze;
/**
* @return name room
*/
public String getName() {
return this.name;
}
/**
* Sets room name
*
* @param name
*/
public void setName(String name) {
this.name = name;
}
/**
* Gets northern room if any
*
* @return pointer to northern room if any, otherwise <code>null</code>
*/
public Room getNorth() {
return this.north;
}
/**
* Sets the door to the next room to the north in that room and in the other
* room sets southern door as connecting back to that room
*
* @param otherRoom
*/
public void setNorth(Room otherRoom) {
this.north = otherRoom;
otherRoom.south = this;
}
/**
* creates a new room to the north and connects back to this room
*
* @param name
* of the room
* @return created room
*/
public Room createNorth(String name) {
Room otherRoom = null;
// create new room in that direction ONLY if there is no room yet
if (this.getNorth() == null) { // get northern direction, if it's null,
// then it's okay to create there
otherRoom = new Room(); // create!
this.setNorth(otherRoom); // set the door
otherRoom.setName(name); // set the name
} else { // there is a room in that direction, so don't create a new
// room and drop a warning
System.out.println("There is already a room in northern direction");
}
return otherRoom;
}
/**
* Asdf
*
* @return maze
*/
public Maze getMaze() {
return this.maze;
}
/**
* Set maze
*
* @param maze
*/
public void setMaze(Maze maze) {
this.maze = maze;
}
/**
* @param roomName path to this room must be found
*/
public void findPathTo(String roomName) {
Room soughtRoom = this.getMaze().getEntry();
while (!(soughtRoom.getName().equalsIgnoreCase(roomName))) {
// here should be also a method such as setRoomAsVisited()
if (this.getWest() != null) {
soughtRoom = this.getWest();
this.getMaze().getPaths().push(soughtRoom);
}
else if (this.getNorth() != null) {
soughtRoom = this.getNorth();
this.getMaze().getPaths().push(soughtRoom);
}
else if (this.getEast() != null) {
soughtRoom = this.getEast();
this.getMaze().getPaths().push(soughtRoom);
}
else if (this.getSouth() != null) {
soughtRoom = this.getSouth();
this.getMaze().getPaths().push(soughtRoom);
}
else {
if (this.getMaze().getPaths().isEmpty()) {
break; // no more path for backtracking, exit (no solution found)
}
// dead end, go back!
soughtRoom = this.getMaze().getPaths().pop();
}
System.out.println(this.getMaze().getPaths().toString());
}
}
@Override
public String toString() {
return "Room name is " + this.getName();
}
}
迷宫是这样的:S是起点
我的迷宫课
public class Maze {
Room room;
/**
* helper collection path stack for findPathTo() method
*/
private Stack<Room> paths = new Stack<Room>();
/**
* @return path for exit
*/
public Stack<Room> getPaths() {
return this.paths;
}
/**
* Singleton method for first room in the maze which is entry room
*
* @return room if no room is created then creates new, otherwise returns
* already created room
*/
public Room getEntry() {
if (this.room == null) {
this.room = new Room();
return this.room;
}
return this.room;
}
}
问题出在我的findPathTo(roomName)
方法中,该方法查找到房间的路径。
如果我进入房间D4,那么我的算法只会从“A4”向东移动一次到“B4”房间,在那里它会无限循环,堆栈只会随着房间“B4”而增长。例如,它为什么不向前移动到下一个房间“B3”或“C4”
编辑:这是工作代码
public void findPathTo(String roomName) {
Room soughtRoom = this.getMaze().getEntry();
while (!(soughtRoom.getName().equalsIgnoreCase(roomName))) {
if (soughtRoom.getWest() != null && soughtRoom.getWest().isVisited != true) {
this.getMaze().getPaths().push(soughtRoom);
soughtRoom = soughtRoom.getWest();
soughtRoom.isVisited = true;
}
else if (soughtRoom.getNorth() != null && soughtRoom.getNorth().isVisited != true) {
this.getMaze().getPaths().push(soughtRoom);
soughtRoom = soughtRoom.getNorth();
soughtRoom.isVisited = true;
}
else if (soughtRoom.getEast() != null && soughtRoom.getEast().isVisited != true) {
this.getMaze().getPaths().push(soughtRoom);
soughtRoom = soughtRoom.getEast();
soughtRoom.isVisited = true;
}
else if (soughtRoom.getSouth() != null && soughtRoom.getSouth().isVisited != true) {
this.getMaze().getPaths().push(soughtRoom);
soughtRoom = soughtRoom.getSouth();
soughtRoom.isVisited = true;
}
else {
if (this.getMaze().getPaths().isEmpty()) {
System.out.println("No solutions found :(");
break; // no more path for backtracking, exit (no solution found)
}
// dead end, go back!
soughtRoom = this.getMaze().getPaths().pop();
}
System.out.println("Path rooms: " + this.getMaze().getPaths().toString());
}
}
一种快速且高度未优化的方法: 对于每个访问过的房间,存储可能的方向(例如,创建
枚举方向
和集合
),并删除您来自的房间以及从上一个房间选择的方向。因此,从A4移到B4,您将从A4移到东部,从B4移到西部。如果您必须回溯,只需将堆栈展开,直到找到一个方向未知的房间(可能的方向列表不是空的),然后尝试下一个方向。如果此时堆栈变为空,则您尝试了所有可能的方法,但没有找到目标房间
正如我所说,这是一种高度未优化的方法,但它应该会起作用。一种快速且高度未优化的方法: 对于每个访问过的房间,存储可能的方向(例如,创建
枚举方向
和集合
),并删除您来自的房间以及从上一个房间选择的方向。因此,从A4移到B4,您将从A4移到东部,从B4移到西部。如果您必须回溯,只需将堆栈展开,直到找到一个方向未知的房间(可能的方向列表不是空的),然后尝试下一个方向。如果此时堆栈变为空,则您尝试了所有可能的方法,但没有找到目标房间
正如我所说,这是高度未优化的,但它应该会起作用。一些评论:
您的代码缺少正在使用的某些函数。迷宫类中没有getPaths()方法。如果你在网上发布代码,尽量确保它易于编译和测试。在您的例子中,我不得不猜测,getPaths()返回某种类型的堆栈,您试图在其上存储可能要探索的路径,但无法确定它实际上是做什么的
在发布之前,还要尽量简化代码。你的迷宫相当复杂,你必须画出来,看看你构建的迷宫是否与图片上的迷宫匹配。如果你能用一个简单得多的迷宫(可能是2个或3个房间)重现这个问题,试试看。此外,简化通常会给你很多提示,说明问题可能在哪里。当你简化的时候,问题会突然消失。这告诉你很多关于实际的错误
关于您的代码可能存在哪些问题的一些想法:
在每个方向,将soughtRoom设置为该方向的一个。我假设soughtRoom是您当前搜索的房间。然后你把那个房间推到烟囱上。但是,对于回溯,您需要在每个分支上存储将您带回以前状态的所有信息。如果先设置当前房间,然后按下,则先前状态的信息将丢失。换个角度试试,看看会发生什么。一些评论:
您的代码缺少正在使用的某些函数。迷宫类中没有getPaths()方法。如果你在网上发布代码,尽量确保它易于编译和测试。在您的例子中,我不得不猜测,getPaths()返回某种类型的堆栈,您试图在其上存储可能要探索的路径,但无法确定它实际上是做什么的
在发布之前,还要尽量简化代码。你的迷宫相当复杂,你必须画出来,看看你构建的迷宫是否与图片上的迷宫匹配。如果你能用一个简单得多的迷宫(可能是2个或3个房间)重现这个问题,试试看。此外,简化通常会给你很多提示,说明问题可能在哪里。当你简化的时候,问题会突然消失。这告诉你很多关于实际的错误
关于您的代码可能存在哪些问题的一些想法:
在每个方向,将soughtRoom设置为该方向的一个。我假设soughtRoom是您当前搜索的房间。然后你把那个房间推到烟囱上。但是,对于回溯,您需要在每个分支上存储将您带回以前状态的所有信息。如果先设置当前房间,然后按下,则先前状态的信息将丢失。反过来试试看会发生什么。有几种方法可以做到这一点 一种方法是在跟踪和回溯过程中,在设置/取消设置的每个房间对象中保留一个“isVisited”布尔标志。这意味着你只能单线程搜索你的迷宫(这可能没关系) 另一种方法是保留一个与之比较的已访问房间的列表(这里的优点是,如果您使用调用堆栈传递此列表,则只需“推”一个新房间到列表上并自动弹出它,应该相对容易) 您还可以使用房间的每搜索哈希表
public void findPathTo(String roomName) {
Room soughtRoom = this.getMaze().getEntry();
while (!(soughtRoom.getName().equalsIgnoreCase(roomName))) {
if (soughtRoom.getWest() != null && soughtRoom.getWest().isVisited != true) {
this.getMaze().getPaths().push(soughtRoom);
soughtRoom = soughtRoom.getWest();
soughtRoom.isVisited = true;
}
else if (soughtRoom.getNorth() != null && soughtRoom.getNorth().isVisited != true) {
this.getMaze().getPaths().push(soughtRoom);
soughtRoom = soughtRoom.getNorth();
soughtRoom.isVisited = true;
}
else if (soughtRoom.getEast() != null && soughtRoom.getEast().isVisited != true) {
this.getMaze().getPaths().push(soughtRoom);
soughtRoom = soughtRoom.getEast();
soughtRoom.isVisited = true;
}
else if (soughtRoom.getSouth() != null && soughtRoom.getSouth().isVisited != true) {
this.getMaze().getPaths().push(soughtRoom);
soughtRoom = soughtRoom.getSouth();
soughtRoom.isVisited = true;
}
else {
if (this.getMaze().getPaths().isEmpty()) {
System.out.println("No solutions found :(");
break; // no more path for backtracking, exit (no solution found)
}
// dead end, go back!
soughtRoom = this.getMaze().getPaths().pop();
}
System.out.println("Path rooms: " + this.getMaze().getPaths().toString());
}
}