我正在尝试编写一个算法来查找并显示迷宫中的路径。
迷宫轮廓从输入文件中读取并存储在二维数组中。
我需要在点 (1,1) 开始迷宫并找到穿过迷宫的任何路径。
我有一个 findpath() 方法,我可以用我写的东西找到出口,但是在找到出口后,我需要通过弹出堆栈来显示我到达那里的路径(这是我目前无法做到的,是我需要什么帮助)。找到目标后,堆栈应弹出完成,并且仅包含找到目标所采用的路径。(此时我只会更改这些房间内的值,但我知道该怎么做)
请查看我的推送发生在哪里,并就应该推送和弹出的顺序提供一些指导,以便满足上述标准。
非常感谢任何帮助。谢谢你。
这是当前输出: o 最初是一个空白空间,但当检测为目标时,它已更改
********************
* * *
** ** ***** ** *****
* * * * * *
* * * * * * *
* * * *
************* ** *
* O
********************
代码:
public void findPath() {
Room start = rooms[1][1];
push(start);
while(!isEmpty()) {
current = pop();
System.out.println("The value stored in current is" + current.getValue()+ "");
if (current == null)
System.out.println("current is null");
//This is finding the goal the walls will contain a *
else if(current.getValue() == ' ' && current.getRight() == null || current.getValue() == ' ' && current.getLeft() == null || current.getValue() == ' ' && current.getUp() == null || current.getValue() == ' ' && current.getRight() == null){
current.setValue('O');
for(int i = 0; i < tos; i++ ){
pop();
}
System.out.println(" I found the end here is the path:" + current.getPrevious().getValue()+ " jjj");
} else if(current.getBlocked() == false && current.getVisited() == false) {
System.out.println("pushing currents neighbors left, right....etc" + "current is at" + current.getCord());
current.setVisited(true);
if(current.getRight() != null){
current.getRight().setPrevious(current);
push(current.getRight());
System.out.println("Inside push 1" +current.getRight().getCord());
} else {
System.out.println("Inside push right is null");
}
if(current.getLeft() != null) {
current.getLeft().setPrevious(current);
push(current.getLeft());
System.out.println("Inside push 2 " + current.getLeft().getCord());
} else {
System.out.println("Inside push left is null");
}
if(current.getUp() != null) {
current.getUp().setPrevious(current);
push(current.getUp());
System.out.println("Inside push 3" + current.getUp().getCord());
} else {
System.out.println("Inside push up is null");
}
if(current.getDown() != null) {
current.getDown().setPrevious(current);
push(current.getDown());
System.out.println("inside push 4" + current.getDown().getCord());
}
} else {
System.out.println("Inside push down is null");
}
}
for(int i = 0; i < rows ; i++) {
for(int j = 0; j < columns ; j++) {
System.out.print(rooms[i][j].getValue());
}
System.out.println();
}
}