Java 导致堆栈溢出错误的搜索算法
我正在使用所谓的深度优先搜索来解决一个迷宫,使用我自己为任务创建的堆栈类。我认为我出现此错误的原因是因为我的程序从未满足Java 导致堆栈溢出错误的搜索算法,java,stack,depth-first-search,Java,Stack,Depth First Search,我正在使用所谓的深度优先搜索来解决一个迷宫,使用我自己为任务创建的堆栈类。我认为我出现此错误的原因是因为我的程序从未满足finishSpot条件。我使用了调试器,它看起来像是无限地卡在finishSpot之前的点上。我的逻辑似乎基本正确,除非我的迷宫解算器试图完成迷宫,所以我只需要帮助满足导致程序崩溃的最后一个条件 这是一个示例迷宫,其中“s”表示开始,“f”表示结束,“*”表示墙 ***** *s* * * * * * f* ***** 这是我的Main,它使用我的LinkedStack类
finishSpot
条件。我使用了调试器,它看起来像是无限地卡在finishSpot
之前的点上。我的逻辑似乎基本正确,除非我的迷宫解算器试图完成迷宫,所以我只需要帮助满足导致程序崩溃的最后一个条件
这是一个示例迷宫,其中“s”表示开始,“f”表示结束,“*”表示墙
*****
*s* *
* * *
* f*
*****
这是我的Main,它使用我的LinkedStack类,如果需要,我可以发布它
//Creates a Stack and determines the start and endpoints.
public static void solveDFS( char [][] maze ){
LinkedStack stack = new LinkedStack();
Point currentSpot = findPoint( maze,'s' );
Point finishSpot = findPoint( maze, 'f' );
findPath( maze,currentSpot, finishSpot,stack );
}
//Finds a point by searching for a char.
private static Point findPoint( char [][] maze, char c ) {
for ( int i = 0; i < maze.length; i++ ) {
for ( int j = 0; j < maze[i].length; j++ ) {
if ( maze[i][j] == c ) {
return new Point(i, j);
}
}
}
return null;
}
//Search algorithm looks for all neighbor locations
private static boolean findPath( char [][] maze, Point currentSpot, Point finishSpot, LinkedStack stack ){
boolean hasSolution = false;
stack.push(currentSpot);
while( currentSpot != finishSpot && !stack.isEmpty() ){
// Checks Right
if( currentSpot.x < maze.length ){
if( maze[currentSpot.x + 1][currentSpot.y] == ' '){
stack.push(new Point( currentSpot.x + 1, currentSpot.y ));
}
}
// Checks Left
if( currentSpot.x > 0 ){
if( maze[currentSpot.x - 1][currentSpot.y] == ' '){
stack.push(new Point( currentSpot.x - 1, currentSpot.y ));
}
}
// Checks Up
if( currentSpot.y > 0 ){
if( maze[currentSpot.x][currentSpot.y - 1] == ' ' ){
stack.push(new Point( currentSpot.x, currentSpot.y - 1));
}
}
// Checks Down
if( currentSpot.y < maze[currentSpot.x].length ){
if( maze[currentSpot.x][currentSpot.y + 1] == ' '){
stack.push(new Point( currentSpot.x, currentSpot.y + 1));
}
}
// Checks Finish (My program never meets this condition help!)
if( currentSpot == finishSpot ){
printMaze(maze);
hasSolution = true;
}
currentSpot = stack.pop();
findPath( maze, currentSpot, finishSpot, stack );
}
return hasSolution;
}
//创建堆栈并确定起点和终点。
公共静态void solveDFS(字符[][]迷宫){
LinkedStack堆栈=新LinkedStack();
点currentSpot=findPoint(迷宫,'s');
点finishSpot=findPoint(迷宫,'f');
findPath(迷宫、currentSpot、finishSpot、堆栈);
}
//通过搜索字符来查找点。
专用静态点findPoint(字符[][]迷宫,字符c){
对于(int i=0;i0){
如果(迷宫[currentSpot.x-1][currentSpot.y]=''){
堆栈推送(新点(currentSpot.x-1,currentSpot.y));
}
}
//检查
如果(currentSpot.y>0){
如果(迷宫[currentSpot.x][currentSpot.y-1]=''){
堆栈推送(新点(currentSpot.x,currentSpot.y-1));
}
}
//检查
if(currentSpot.y
以下条件在您的代码中永远不会同时为真
while( currentSpot != finishSpot && !stack.isEmpty() ){
...
if( currentSpot == finishSpot ){
我认为你不能像那样比较
点
变量。
尝试重写方法equals
如果(currentSpot==finishSpot)
读取。。。我认为你不应该在这里使用==
。使用if(currentSpot.equals(finishSpot))
替代Point
类中的equals
和hashCode
。为什么要使用堆栈而不是递归?@mangusta我需要这样做是为了分配。