Results 1 to 4 of 4
Thread: Recursion error
- 03-11-2012, 01:00 AM #1
Member
- Join Date
- Mar 2012
- Location
- Vestal, NY
- Posts
- 36
- Rep Power
- 0
Recursion error
I'm writing a simple recursion program that is supposed to find a path through a "maze".
Here is the output:Java Code://CLASS MAZESEARCH (driver) public class MazeSearch { public static void main(String[] args) { Maze labyrinth = new Maze(); System.out.println(labyrinth); if(labyrinth.traverse(0, 0)) { System.out.println("The maze was successfully solved!"); } else { System.out.println("There is no possible path."); } System.out.println(labyrinth); } } //CLASS MAZE public class Maze { private final int TRIED = 3; private final int PATH = 7; private int[][] grid = { {1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1}, {1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1}, {0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0}, {1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1}, {1, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1}, {1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1}, {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}}; public boolean traverse(int row, int column) { boolean done = false; if(valid(row, column)) { grid[row][column] = TRIED; if(row == grid.length - 1 && column == grid[0].length - 1) { done = true; } else { done = traverse(row + 1, column + 1); if(!done) { done = traverse(row, column + 1); } if(!done) { done = traverse(row - 1, column); } if(!done) { done = traverse(row, column - 1); } } if(done) { grid[row][column] = PATH; } } return done; } private boolean valid(int row, int column) { boolean result = false; if(row >= 0 && row < grid.length && column >= 0 && column < grid[row].length) { if(grid[row][column] == 1) { result = true; } } return result; } public String toString() { String result = "\n"; for(int row = 0; row < grid.length; row++) { for(int column = 0; column < grid[row].length; column++) { result += grid[row][column] + ""; } result += "\n"; } return result; } }
"1110110001111
1011101111001
0000101010100
1110111010111
1010000111001
1011111101111
1000000000000
1111111111111
There is no possible path.
3330330003333
1033303333003
0000303030300
1110333030333
1010000333003
1011111103333
1000000000000
1111111111111"
Theres the problem in red...why is it not seeing that 1 below the 3 on the left?
- 03-11-2012, 01:33 AM #2
Re: Recursion error
Add some printlns to the code to print out what it is doing so you can see where the execution goes and what the values of variables are as it executeswhy is it not seeing that 1 below the 3 on the left?
-
Re: Recursion error
I'm not really sure I understand your code but it just seems like this part:
is meant to search up,down,left and right from a given index in the 2d array...Java Code:done = traverse(row + 1, column + 1); if(!done) { done = traverse(row, column + 1); } if(!done) { done = traverse(row - 1, column); } if(!done) { done = traverse(row, column - 1); }
if thats so, then the first statement is wrong and should look like this:
so that you are checking 'down' and not 'down-right'Java Code:done = traverse(row + 1, column);
looking at your output again it seems like all of the 1's that were converted to 3's were accessible through the down-right path up until where it had stopped.Last edited by ozzyman; 03-11-2012 at 01:57 AM.
- 03-11-2012, 02:09 AM #4
Member
- Join Date
- Mar 2012
- Location
- Vestal, NY
- Posts
- 36
- Rep Power
- 0
Similar Threads
-
recursion help
By Yakg in forum New To JavaReplies: 6Last Post: 02-18-2011, 01:10 PM -
stack overflow error, i want to call a method within itself not using recursion tho
By bigboi26 in forum New To JavaReplies: 1Last Post: 03-17-2010, 05:25 AM -
recursion and tail-recursion differences
By OptimusPrime in forum New To JavaReplies: 2Last Post: 12-28-2009, 06:26 PM -
Recursion with int and string error message
By nicolek808 in forum New To JavaReplies: 7Last Post: 09-14-2009, 09:30 AM -
recursion
By ravian in forum New To JavaReplies: 2Last Post: 12-03-2007, 05:15 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks