gpt4 book ai didi

Java 用递归解决迷宫问题

转载 作者:搜寻专家 更新时间:2023-10-31 19:31:21 24 4
gpt4 key购买 nike

我有一个任务,我应该能够显示从入口到导出的迷宫路径,我已经让它工作到一定程度,但是当迷宫变得更加复杂,有死胡同时,诸如程序进入无限递归。如果您能给我任何帮助,为我指明正确的方向,我们将不胜感激。

可以在 Room 类中找到 Mu 当前理论。

这里是 Room 类,其中存储了连接迷宫的每个房间的引用,有点像在 6 个方向(北、南、东、西、上、下)上链接的链表。

import java.util.ArrayList;

public class OurRoom
{
private OurRoom exits[];
private String name;
private static ArrayList<OurRoom> list;

public OurRoom()
{
this(null);
}

public OurRoom(String name)
{
this.name = name;
this.list = new ArrayList<OurRoom>();
exits = new OurRoom[Direction.values().length];
for(OurRoom exit : exits)
{
exit = null;
}
}


public void connectTo(OurRoom theOtherRoom, Direction direction)
{
exits[direction.ordinal()] = theOtherRoom;
theOtherRoom.exits[direction.getOpposite().ordinal()] = this;
}

public OurRoom getExit(Direction direction)
{
return exits[direction.ordinal()];
}

public boolean lookExit(Direction direction)
{
return exits[direction.ordinal()] != null;
}

public String getName() {
return name;
}

public OurRoom solveRecursively(OurRoom exit) {
list.add(this);
if(this == exit) {
return this;
}else {
OurRoom temp = null;
if(lookExit(Direction.east)) {
temp = exits[Direction.east.ordinal()].solveRecursively(exit);
}
else if(lookExit(Direction.up)) {
temp = exits[Direction.up.ordinal()].solveRecursively(exit);
}
else if(lookExit(Direction.south)) {
temp = exits[Direction.south.ordinal()].solveRecursively(exit);
}
else if(lookExit(Direction.down)) {
temp = exits[Direction.down.ordinal()].solveRecursively(exit);
}
else if(lookExit(Direction.west)) {
temp = exits[Direction.west.ordinal()].solveRecursively(exit);
}
else if(lookExit(Direction.north)) {
temp = exits[Direction.north.ordinal()].solveRecursively(exit);
}
return temp;
}
}

public ArrayList<OurRoom> getList() {
return list;
}

}

这是方向枚举

public enum Direction
{
north, south, east, west, up, down;

public Direction getOpposite()
{
switch(this)
{
case north:
return south;
case south:
return north;
case east:
return west;
case west:
return east;
case up:
return down;
case down:
return up;
default:
return this;
}
}
}

这是迷宫构建的示例。

import java.util.ArrayList;
import java.util.Iterator;

public class OurMaze
{
private OurRoom entrance, exit;

public OurMaze()
{
this(1);
}

public OurMaze(int mazeNumber)
{
entrance = null;
exit = null;
switch(mazeNumber)
{
case 0:
break;
case 1:
this.buildMaze1();
break;
default:
}
}

public OurRoom getEntrance()
{
return entrance;
}

public OurRoom getExit()
{
return exit;
}

public Iterator<OurRoom> findPathRecursively() {
entrance.solveRecursively(exit);
ArrayList<OurRoom> list = entrance.getList();
return list.iterator();
}

private void buildMaze1()
{
OurRoom room1, room2;

room1 = new OurRoom("Room 1");
room2 = new OurRoom("Room 2");
room1.connectTo(room2, Direction.north);

entrance = room1;
exit = room2;
}

public static void main(String[] args) {
OurMaze maze = new OurMaze(1);
}
}

最佳答案

您只需要保留二维数组,其值指示单元格是否被访问:您不想两次访问同一个单元格。

除此之外,它只是广度优先搜索(如果您不想要最短路径,深度优先搜索也可以)。

一些链接
http://en.wikipedia.org/wiki/Flood_fill
http://en.wikipedia.org/wiki/Breadth-first_search
http://en.wikipedia.org/wiki/Depth-first_search

示例搜索例程。

    void dfs(int i, int j) {
if cell(i, j) is outside of maze or blocked {
return
}
if visited[i][j] {
return
}
visited[i][j] = true;
dfs(i + 1, j);
dfs(i - 1, j);
dfs(i, j + 1);
dfs(i, j - 1);
}

如果像 visited 一样,对于每个单元格,您都可以找到路径本身,您可以从中找到它。所以,打印看起来像这样(只是一个伪代码)。

var end = exit_point;
while (end != start_point) {
print end;
end = came_from[end];
}

编辑
上面的代码适用于二维迷宫,我刚刚注意到你有三维版本。但是在上面的例子中很容易引入第三个坐标。
如果有任何困难,请告诉我。

关于Java 用递归解决迷宫问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3225126/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com