[英]Java Maze Solver
我在嘗試將一個暗示給我們的算法變成可用代碼時遇到了困難。 我們給出一個方向枚舉,其具有8個坐標(N,NE,NW,S,SE,SW.E,W)以及出口HERE。
這是暗示的算法:
getPathToExit(row, col):
if (row, col) is outside of the map:
return an empty list
else if (row, col) is an obstacle:
return an empty list
else if (row, col) is marked as visited or as deadend:
return an emtpy list
else if (row, col) is the exit:
//optional: mark exit as visited
return a list containing Direction.HERE
else:
//try to find a path from current square to exit:
mark current square as visited (that is, part of path)
for each neighbor of current square:
path = path from neighbor to exit
if path is not empty:
add (direction to neighbor) to start of path
return path
//after for loop: no path exists from this square to exit
mark current square as deadend
return empty list
這是我一直在研究的代碼:
public java.util.ArrayList<Direction> getPathToExit(){
for (int x=0; x<map.length; x++){
for (int y=0; y<map[x].length; y++){
if (map[x][y]=='S'){
this.startRow=x;
this.startCol=y;
}
}
}
System.out.println("start "+startRow+", "+startCol);
return getPathToExit(this.startRow, this.startCol);
}
private java.util.ArrayList<Direction> getPathToExit(int row, int col){
Direction [] dirs = Direction.values();
ArrayList<Direction> path = new ArrayList<Direction>();
getPathToExit(row, col);
if (row < 0 || col < 0 || row > map.length || col > map[row].length){
return null;
}
else if (map[row][col] != ' '){
return null;
}
else if (map[row][col] == 'E'){
path.add(Direction.HERE);
return path;
}
else {
for (int x=0; x<dirs.length-1; x++){
int nextRow = row + dirs[x].getRowModifier();
int nextCol = col + dirs[x].getColModifier();
path = getPathToExit(nextRow, nextCol);
}
}
return path;
}
這是枚舉類:
public enum Direction {
N, NE, E, SE, S, SW, W, NW, HERE;
/**
* Returns the X/column change on the screen that is associated with
* this direction: -1 for W, 0 for N/S, and +1 for E.
*/
public int getColModifier() {
int mod;
switch (this) {
case NW:
case W:
case SW:
mod = -1;
break;
case NE:
case E:
case SE:
mod = +1;
break;
default:
mod = 0;
break;
}
return mod;
}
/**
* Returns the Y/row change on the screen that is associated with
* this direction: -1 for N, 0 for E/W, and +1 for south.
*/
public int getRowModifier() {
int mod;
switch (this) {
case N:
case NE:
case NW:
mod = -1;
break;
case S:
case SE:
case SW:
mod = +1;
break;
default:
mod = 0;
break;
}
return mod;
}
/** As {@link #getColModifier()} */
public int getXModifier() {
return this.getColModifier();
}
/** As {@link #getRowModifier()} */
public int getYModifier() {
return this.getRowModifier();
}
/**
* Returns the direction that is the opposite of this one.
* For example, <code>Direction.NE.reverse() == Direction.SW</code>.
* (The opposite of HERE is still HERE though.)
*/
public Direction reverse() {
if (this == HERE) {
return this;
}else {
int reversed = (this.ordinal() + 4) % 8;
Direction[] dirs = Direction.values();
return dirs[reversed];
}
}
}
提前致謝。
代碼中有兩個問題:
(1)在主循環中:
for (int x=0; x<dirs.length-1; x++){
int nextRow = row + dirs[x].getRowModifier();
int nextCol = col + dirs[x].getColModifier();
path = getPathToExit(nextRow, nextCol);
}
您需要檢查遞歸調用: getPathToExit()
返回了非空列表。 如果有,你應該break
循環並將相關方向推到它的開始。 你已經找到了一條路 - 不是要繼續檢查其余部分!
(2)為了使您的算法完整(找到解決方案,如果存在),您需要維護一個visited
集,並避免重新訪問已訪問過的節點。
看看下面的例子:
-------
|S |x1|
-------
|x2|E |
-------
所有都是有效的正方形(沒有障礙物),S是開始,E是結束。
現在,假設方向的順序是right,left, ...
代碼(沒有visited
集)將執行以下操作:
go right (to x1).
go right - out of maze, go back.
go left (to S).
go right (to x1).
go right - out of maze, go back.
go left (to S)
....
你處在一個無限循環中! (一個已知的DFS退款)
StackOverflowError通常表明這是問題,調用堆棧從所有遞歸調用中已滿 - 並且拋出錯誤。
要修復它,您需要維護一個visited
集,並避免重新訪問已訪問過的節點。 有了這個設置,以及上面的迷宮(方向順序是right, left, down, ...
)將會發生的事情是:
go right (to x1)
go right - out of maze, go back.
go left (to S) - already visitted, go back.
go down (to E) - found target, return it.
更高級的替代方案是使用Iterative Deepening DFS - 這基本上意味着,您將路徑的長度限制為l
,並迭代地增加此l
。 我這次忽略了這個替代方案,它有點先進了。
作為旁注,您的算法是DFS的實現,它具有訪問集和有限圖(如果存在,則總是找到解決方案),但不是最優的(不保證找到最短路徑)。 要找到最短路徑,您可能希望使用BFS 。
另外 :我假設在方法的第三行中的遞歸調用是用於調試的剩余調用。 它應該不存在。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.