递归走迷宫java_Java用递归问题解决迷宫

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

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

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

import java.util.ArrayList;

public class OurRoom

{

private OurRoom exits[];

private String name;

private static ArrayList list;

public OurRoom()

{

this(null);

}

public OurRoom(String name)

{

this.name = name;

this.list = new ArrayList();

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 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 findPathRecursively() {

entrance.solveRecursively(exit);

ArrayList 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);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值