This repository contains an innovative approach to solving mazes using Laplace's equation. Instead of traditional pathfinding algorithms, we use physical principles to model the maze as a potential field and then navigate the maze by following the gradient.
A maze can be thought of as a 2D domain where certain regions (paths) are accessible and others (walls) are not. From a physics perspective, imagine filling this domain with a conductive material. Walls are perfect insulators, while paths are conductive regions.
Now, if we impose a potential difference across this domain—by setting high potential at the start and low potential at the end—we induce a potential field across the maze. The distribution of this potential field will be governed by Laplace's equation in regions without any sources or sinks.
Laplace's equation is a second-order partial differential equation defined as:
Where
When we solve this equation with appropriate boundary conditions (start and end potentials and insulating walls), we obtain a continuous scalar field
Given the potential field, the path from the start to the end can be determined by following the gradient of the potential, which always points in the direction of the steepest increase in potential:
Moving in the direction of
This function solves the Laplace's equation iteratively for the given maze.
Math Details:
We initialize a potential field
This iteration continues until the field converges or the number of iterations reaches max_iter.
This function determines the path from the start to the end of the maze by following the gradient of the potential field.
Math Details:
At every point
Computes the gradient of the potential field.
Math Details:
For each cell
START = 0
END = 1This method provides an interesting approach to maze-solving by leveraging principles from physics. The generated potential field offers insight into how potential flows through the maze, and the gradient-driven path offers a means to navigate from start to end.

