Note that there are some explanatory texts on larger screens.

plurals
  1. PORecursive backtracking returning updated value
    primarykey
    data
    text
    <p>I'm creating a program designed to recursively navigate a maze. The code:</p> <pre><code>public static boolean traverse(int maze[][], coordinate start) { //recursion: traverse(maze, updated coordinates) if(maze[start.y+1][start.x] == 2 || maze[start.y-1][start.x] == 2 || maze[start.y][start.x+1] == 2 || maze[start.y][start.x - 1] == 2) { display(maze); System.out.println("DONE"); return true; } else { if(north(maze, start) == true) { maze[start.y-1][start.x] = 4; display(maze); coordinate temp = start; temp.y--; if (traverse(maze, temp) == false) { maze[start.y][start.x] = 3; } } if(west(maze, start) == true) { maze[start.y][start.x-1] = 4; display(maze); coordinate temp = start; temp.x--; if (traverse(maze, temp) == false) { maze[start.y][start.x] = 3; } } if(south(maze, start) == true) { maze[start.y+1][start.x] = 4; display(maze); coordinate temp = start; temp.y++; if (traverse(maze, temp) == false) { maze[start.y][start.x] = 3; } } if(east(maze, start) == true) { maze[start.y][start.x+1] = 4; display(maze); coordinate temp = start; temp.x++; if (traverse(maze, temp) == false) { maze[start.y][start.x] = 3; } } } return false; } </code></pre> <p>However whenever I reach a dead end, it doesn't backtrack. When I debug, it shows that my start values are fixated on staying at my dead end space when the program returns from the recursion or "backtracks".</p> <p>For example:</p> <pre><code>1 1 1 1 1 1 4 4 4 1 1 9 1 4 1 1 1 1 4 1 1 4 4 4 1 1 4 1 0 1 1 4 1 0 1 1 1 1 2 1 </code></pre> <p>9 is my starting point. 2 is my exit. 4 is my path. 1 represents the walls. When I reach a dead end (in this case row 7, column 2). My position be would equal to that dead end space throughout the rest of the program. Why's that?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload