0
我想寫一個算法來查找並在迷宮中顯示路徑。你能否提供一些僞代碼來顯示迷宮的路徑?
迷宮輪廓從輸入文件讀取並存儲在二維數組中。
我需要在點(1,1)處開始迷宮並在迷宮中找到任何路徑。
我有一個findpath()方法,我能夠找到我已經寫入的出口,但在找到出口後,我需要顯示我通過彈出堆棧來達到那裏的路徑(我目前無法做和我需要幫助)。一旦找到目標,堆棧應該彈出完成,並將只包含找到目標所採取的路徑。 (在這一點上,我只是改變這些房間內的價值,但我知道如何做到這一點)
請檢查我的推動發生在哪裏,並提供一些指導,以什麼順序的東西應該推動和彈出,以便滿足以上標準。
任何幫助,非常感謝。謝謝。
這裏是電流輸出:鄰最初是空的空間,但在作爲目標檢測又改爲
********************
* * *
** ** ***** ** *****
* * * * * *
* * * * * * *
* * * *
************* ** *
* 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();
}
}
一種方法是沿着正確的牆壁找到最終的結果,我認爲這適用於大多數迷宮,否則像A *或D * Lite這樣的尋路算法應該可以幫助您。有很多你可以學習的實現。 – danielbeard 2012-04-24 03:52:25
這段代碼的當前輸出是什麼? – shan 2012-04-24 04:02:01
[GRAPH PROBLEM:找到算法來確定矩形迷宮中從一個點到另一個點的最短路徑?](http://stackoverflow.com/questions/2634647/graph-problem-find-an-algorithm- to-determine-the-short-path-from-one-point-t) – Bohemian 2012-04-24 04:05:35