Help with recursive puzzle solving

Updated the code to look cleaner, same problem still. Get some sort of error when I run it, but I can't see what it reads because the recursion happens too fast. The tabbing got mess up anyways :(.

I know methods have been posted and I've looked at them, but I still can't figure out what my problem is. It fails pretty soon in.

import java.util.LinkedList;

publicclass MazeSolver{

static String[][] maze;

static String wall;

staticint endX;

staticint endY;

static LinkedList pointList;

publicstaticvoid main(String[] args){

String[][] tempmaze ={

{"#","#","#","#","#","#","#","#","#","#","#","#"},

{"#","S","#"," ","#"," "," "," ","#"," "," ","E"},

{"#"," ","#"," ","#"," ","#","#","#"," ","#","#"},

{"#"," "," "," ","#"," "," "," ","#"," "," ","#"},

{"#"," ","#","#","#","#","#"," "," "," ","#","#"},

{"#"," "," "," "," "," ","#"," ","#"," "," ","#"},

{"#"," ","#","#","#"," ","#"," ","#","#"," ","#"},

{"#"," ","#"," "," "," ","#"," ","#"," "," ","#"},

{"#"," ","#"," ","#","#","#"," ","#","#","#","#"},

{"#","#","#"," ","#"," "," "," "," ","#"," ","#"},

{"#"," "," "," "," "," ","#"," "," "," "," ","#"},

{"#","#","#","#","#","#","#","#","#","#","#","#"},

};

//i made the temp maze so i could easily create the maze matrix

maze = tempmaze;

wall ="#";

endX = 11;

endY = 1;

pointList =new LinkedList();

//prints out the maze just for a visual

for(int x=0;x<maze.length;x++){

for(int y=0;y<maze[0].length;y++){

System.out.print(maze[x][y]);

}

System.out.println();

}

System.out.println();

//prints out whether it solves the maze or not.

System.out.println(solve(1,1,-1,-1));

}

publicstaticboolean solve(int curX,int curY,int prevX,int prevY){

// |-check if previous point-| |--check if its within bounds of the maze||--check if its a wall--|

if((curX==prevX && curY==prevY) || curX>maze.length-1 || curX<0 || curY>maze[0].length-1 || curY<0 || maze[curX][curY].equals(wall)){

returnfalse;

}

//catch the point that is the end and return true for the recursing if statement

elseif(curX==endX && curY==endY){

returntrue;

}

//keep brute forcing the maze until it reaches the end point

elseif(solve(curX,curY-1,curX,curY) || solve(curX+1,curY,curX,curY) || solve(curX,curY+1,curX,curY) || solve(curX-1,curY,curX,curY)){

int[] point ={curX, curY};

//keep adding the point to the front when the maze is solved because of LIFO stack

pointList.addFirst(point);

returntrue;

}

//if somehow the maze does not have an end point, or it is somehow unreachable, return false

else

returnfalse;

}

}

Message was edited by:

ending6o6

Message was edited by:

ending6o6

Message was edited by:

ending6o6

[9393 byte] By [ending6o6a] at [2007-11-26 18:31:13]
# 1
*BUMP* for edit.I found the error. It is a stackOverFlow, but I'm not sure how to fix this. Do I need to increase the amount of memory the JVM is able to use?
ending6o6a at 2007-7-9 6:05:21 > top of Java-index,Java Essentials,Java Programming...
# 2
What other posts/answers? You only have one post to your name.
prometheuzza at 2007-7-9 6:05:21 > top of Java-index,Java Essentials,Java Programming...
# 3

And may I also suggest using chars instead of Strings. That way you don't have to use all those equals(...) methods but can just compare them using ==

char[][] maze = {

"############".toCharArray(),

"#S# ## E".toCharArray(),

"# # # ### ##".toCharArray(),

"### #".toCharArray(),

"# #######".toCharArray(),

"## # #".toCharArray(),

"# ### # ## #".toCharArray(),

"# ## # #".toCharArray(),

"# # ### ####".toCharArray(),

"### ## #".toCharArray(),

"###".toCharArray(),

"############".toCharArray()

};

prometheuzza at 2007-7-9 6:05:21 > top of Java-index,Java Essentials,Java Programming...
# 4

Well I searched the forums for maze solving and helped me understand the recursive way. Those don't help with the problems I'm having though. The reason why I only have one topic for my user name is because usually my questions have already been asked in the forum, so I hvaen't needed an account for the forums until now.

ending6o6a at 2007-7-9 6:05:21 > top of Java-index,Java Essentials,Java Programming...
# 5

> I found the error. It is a stackOverFlow, but I'm

> not sure how to fix this. Do I need to increase the

> amount of memory the JVM is able to use?

A stack overflow can occur if you recurse too deeply, so there just isn't room on the stack to hold any more return addresses. You can't change the depth of the stack as far as I know (hopefully someone will correct me if I'm wrong there), so the only choice is to do fewer recursions.

Mr_Evila at 2007-7-9 6:05:21 > top of Java-index,Java Essentials,Java Programming...
# 6
Ok, so in theory this would work given a limitless stack possibility? Anyone have a way to possibly work around this? It does have to be done using recursion though.
ending6o6a at 2007-7-9 6:05:21 > top of Java-index,Java Essentials,Java Programming...
# 7
> Ok, so in theory this would work given a limitless> stack possibility?Not necessarily. The other possibility is that you have an infinite recursion going on. (Unless you count finding the solution in infinite time as "working".)
DrClapa at 2007-7-9 6:05:21 > top of Java-index,Java Essentials,Java Programming...
# 8
various solutions here, worth a read http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=71&t=000061
Michael_Dunna at 2007-7-9 6:05:21 > top of Java-index,Java Essentials,Java Programming...
# 9

Well, given that there are no loops in the maze, this should eventually find the solution to a reasonable size maze in a reasonable time. By reasonable I mean an average user sitting there waiting without falling asleep. Still, any other suggestions on how to get this to work around the stack overflow?

ending6o6a at 2007-7-9 6:05:21 > top of Java-index,Java Essentials,Java Programming...
# 10

> Well, given that there are no loops in the maze, this

> should eventually find the solution to a reasonable

> size maze in a reasonable time. By reasonable I mean

> an average user sitting there waiting without falling

> asleep. Still, any other suggestions on how to get

> this to work around the stack overflow?

Your algorithm is incorrect: the first if-statement always returns true.

Try to create some objects in your code like a Point class and use char's instead of String's.

prometheuzza at 2007-7-9 6:05:21 > top of Java-index,Java Essentials,Java Programming...
# 11

> Your algorithm is incorrect: the first if-statement

> always returns true.

> Try to create some objects in your code like a Point

> class and use char's instead of String's.

Something like this:import java.util.HashSet;

import java.util.Set;

import java.util.Stack;

import java.awt.Point;

public class MazeSolver {

final char WALL = '#';

final char END = 'E';

private boolean solve(char[][] maze, Stack<Point> path, Set<Point> visited) {

add all points from 'path' to 'visited'

IF visited has no more points

return false

ELSE

'current' = the top of 'path'

END IF

IF 'current' is the end

return true

END IF

'left' := the left of 'current'

'right' := the right of 'current'

... (all other directions)

IF 'left' != WALL && 'left' is not in 'visited'

add 'left' to 'path'

ESLE IF 'right' != WALL && 'left' is not in 'visited'

add 'left' to 'path'

... (all other directions)

ELSE

remove the last point from 'path'

END IF

make the recursive call

}

public static void main(String[] args) {

char[][] maze = {

"############".toCharArray(),

"#S# ## E".toCharArray(),

"# # # ### ##".toCharArray(),

"### #".toCharArray(),

"# #######".toCharArray(),

"## # #".toCharArray(),

"# ### # ## #".toCharArray(),

"# ## # #".toCharArray(),

"# # ### ####".toCharArray(),

"### ## #".toCharArray(),

"###".toCharArray(),

"############".toCharArray()

};

MazeSolver solver = new MazeSolver();

Point start = new Point(1,1);

Stack<Point> path = new Stack<Point>();

path.push(start);

System.out.println(solver.solve(maze, path, new HashSet<Point>()));

}

}

prometheuzza at 2007-7-9 6:05:21 > top of Java-index,Java Essentials,Java Programming...
# 12
> You can't change the depth of the stack as far as I know> (hopefully someone will correct me if I'm wrong there)-Xss, but in practice if you need to use this then your design is flawed.
YAT_Archivista at 2007-7-9 6:05:21 > top of Java-index,Java Essentials,Java Programming...