I am having some truble with Recursive Backtracking, i have tried 2 variations, but both overflow the stack. Any ideas would be great
Code:public Point findPath(Point point){ String temp; String [] tempA; temp = point.toString(); tempA = Maze.tokenString(temp, ","); int y = Integer.parseInt(tempA[0]); int x = Integer.parseInt(tempA[1]); if(Maze.isEnd(point)){ return point; } Maze.mark(point); //north Point n = new Point(y-1,x); if(Maze.move(n)){ findPath(n); } //south Point s = new Point(y+1,x); if(Maze.move(s)){ findPath(s); } //east Point e = new Point(y,x+1); if(Maze.move(e)){ findPath(e); } //west Point w = new Point(y, x-1); if(Maze.move(w)){ findPath(w); } Maze.mark(point); findPath(point); return point; }Code:public boolean moveP(Point point, int d){ String temp; String [] tempA; temp = point.toString(); tempA = Maze.tokenString(temp, ","); int y = Integer.parseInt(tempA[0]); int x = Integer.parseInt(tempA[1]); switch(d){ case 1: n.set(y-1,x); if(Maze.move(n)){ return true; } break; case 2: e.set(y,x+1); if(Maze.move(e)){ return true; } break; case 3: s.set(y+1,x); if(Maze.move(s)){ return true; } break; case 4: w.set(y,x-1); if(Maze.move(w)){ return true; } break; default: } return false; } public Point findPath(Point point){ if(maze1.isEnd(point)){ return point; } maze1.mark(point); //north if((moveP(point,1))){ return findPath(n); } //east if((moveP(point,2))){ return findPath(e); } //south if((moveP(point,3))){ return findPath(s); } //west if((moveP(point,4))){ return findPath(w); } maze1.mark(point); return point; }




Reply With Quote