java迷宫中利用DFS进行最短路径递归搜索
`使用此代码时出现堆栈溢出错误。如果我们已经找到了最短路径,那么递归代码就会停止。迷宫包含字符“#”和“”。如果我找到了最短的路径,路径将标记为“.”。请帮忙,谢谢java迷宫中利用DFS进行最短路径递归搜索,java,recursion,path,microsoft-distributed-file-system,shortest,Java,Recursion,Path,Microsoft Distributed File System,Shortest,`使用此代码时出现堆栈溢出错误。如果我们已经找到了最短路径,那么递归代码就会停止。迷宫包含字符“#”和“”。如果我找到了最短的路径,路径将标记为“.”。请帮忙,谢谢 public static int getCoordinateY(String location){ //to get x coordinate String y = location.substring(2, 4); int coor = (y.charAt(0) - 'Q') * 10 + Character.g
public static int getCoordinateY(String location){ //to get x coordinate
String y = location.substring(2, 4);
int coor = (y.charAt(0) - 'Q') * 10 + Character.getNumericValue(y.charAt(1));`enter code here`
return coor;
}
public boolean canPass(int y,int x) { //you can keep going if you not found # and .
if(map[y][x] == '#' || map[y][x] == '.' ) {
return false;
}
return true;
}
public Character[][] cloneArray(Character[][] src) { //copy array
int length = src.length;
Character[][] target = new Character[length][src[0].length];
for (int i = 0; i < length; i++) {
System.arraycopy(src[i], 0, target[i], 0, src[i].length);
}
return target;
}
public void finish(int x,int y){ //goal
xgoal=x;
ygoal=y;
}
public int getDistance(){ //shortest distance from shortest path
return finalDistance;
}
public void shortestPathStart(int xStart,int yStart, int xEnd, int yEnd){
set('S',xStart,yStart); //start coordinate
finish(xEnd,yEnd);
shortestPathRec(xStart+1,yStart,0,map);//to right
shortestPathRec(xStart-1,yStart,0,map);// to left
shortestPathRec(xStart,yStart+1,0,map);//to up
shortestPathRec(xStart,yStart-1,0,map);// to down
map = result; //final map with '.'
set('F',xEnd,yEnd);
print();
}
public void shortestPathRec(int x,int y,int step,Character[][] map){
if(canPass(x,y)){
step++;
Character[][] temp = cloneArray(map);
temp[x][y] = '.'; //in the maze, '.' using for flags
if(x == xgoal && y == ygoal){//if already found the goal
hasDone = true;
finalDistance = step;
result = temp;
return;
}
if(hasDone==true && finalDistance<step){ //if shortest path is found other path should be in this condition
return;
}
shortestPathRec(x+1,y,step,temp);//calltherecursive again
shortestPathRec(x-1,y,step,temp);
shortestPathRec(x,y+1,step,temp);
shortestPathRec(x,y-1,step,temp);
}
}
public static int getCoordinateY(字符串位置){//获取x坐标
字符串y=位置。子字符串(2,4);
int coor=(y.charAt(0)-'Q')*10+Character.getNumericValue(y.charAt(1));`在此处输入代码`
返回库尔;
}
公共布尔canPass(inty,intx){//如果没有找到#和,可以继续运行。
if(map[y][x]='#'| | map[y][x]='.){
返回false;
}
返回true;
}
公共字符[][]cloneArray(字符[][]src){//复制数组
int length=src.length;
字符[][]目标=新字符[length][src[0].length];
for(int i=0;i 如果(hasDone==true&&finalDistance问题分析
您无法防止物理回溯:您移动到墙上,但没有在调用堆栈中进行备份(撤消最后一步),则转到下一个递归调用并执行相反的操作—执行第二个无用步骤,而不是备份到0个无用步骤。您的代码将在这两个方块之间无限移动,直到超出堆栈限制
修理
修改你的代码,这样你就永远不会走到你已经在这条路径上访问过的广场上。这很容易在线研究;Dijkstra的算法是最早的通用解决方案之一
回避
学习基本调试。除其他事项外,在每个例程的入口和出口插入print语句,打印输入参数和返回值。为了更加清晰,请保留一个深度计数器并适当缩进打印。如果堆栈溢出,则可能是递归太深。对于以下问题:这可能意味着您没有正确地检测和避免路径中的循环。请仔细查看您的canPass()
方法,它似乎有助于实现这一点,以及您的shortestPathRec()
方法。这可能是什么原因
并没有阻止你循环?非常感谢你的回答。我正在尝试回溯,但失败了。你能给我一个回溯的例子吗?我对Dijkstra的算法感到困惑。互联网上有成千上万的例子。你到底是如何和在哪里困惑的?另外,请记住堆栈溢出指南。在ot中她的东西,这是一个专门针对特定编程问题的网站。当这个网站出现“我不懂回溯”时,你需要研究,也许还需要一个当地的导师,不是这样。