java - Search Algorithm Causing StackOverFlowError -
i using known depth first search solve maze using own stack created class assignment. reason believe getting error because program never meeting finishspot
condition. have used debugger , looks infinitely stuck @ point before finishspot
. logic seems correct except until point maze solver tries finish maze, need meeting last condition causing program crash.
this sample maze 's' start , 'f' finish , '*' wall.
***** *s* * * * * * f* *****
this main uses linkedstack class can post if needed.
//creates stack , determines start , 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 point searching char. private static point findpoint( char [][] maze, char c ) { ( int = 0; < maze.length; i++ ) { ( int j = 0; j < maze[i].length; j++ ) { if ( maze[i][j] == c ) { return new point(i, j); } } } return null; } //search algorithm looks 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 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 condition help!) if( currentspot == finishspot ){ printmaze(maze); hassolution = true; } currentspot = stack.pop(); findpath( maze, currentspot, finishspot, stack ); } return hassolution; }
following conditions never true @ same time in code
while( currentspot != finishspot && !stack.isempty() ){ ... if( currentspot == finishspot ){
Comments
Post a Comment