Maze-solving algorithm: Difference between revisions

Content deleted Content added
No edit summary
Tags: Mobile edit Mobile web edit Advanced mobile edit
Tags: Mobile edit Mobile web edit Advanced mobile edit
Line 58:
This is in effect a depth-first search expressed in term of grid points. The omniscient view prevents entering loops by memoization. Here is a sample code in [[Java (programming language)|Java]]:
<syntaxhighlight lang="java">
intboolean[][] maze = new int[width][height]; // The maze
boolean[][] wasHere = new boolean[width][height];
boolean[][] correctPath = new boolean[width][height]; // The solution to the maze
Line 65:
 
public void solveMaze() {
maze = generateMaze(); // Create Maze (1false = path, 2true = wall)
for (int row = 0; row < maze.length; row++)
// Sets boolean Arrays to default values
Line 79:
public boolean recursiveSolve(int x, int y) {
if (x == endX && y == endY) return true; // If you reached the end
if (maze[x][y] == 2 || wasHere[x][y]) return false;
// If you are on a wall or already were here
wasHere[x][y] = true;