Deep Maze Solver

Published: (December 31, 2025 at 02:56 AM EST)
2 min read
Source: Dev.to

Source: Dev.to

Introduction

A few days ago I saw a X Post explaining that diffusion models could be used to solve algorithmic tasks like solving mazes. Intrigued, I decided to replicate this work on a smaller scale and began building a neural network that can solve mazes.

Problem

The goal is to build a convolutional neural network (CNN) that can solve mazes using supervised learning.
Given a maze image, the CNN must find the path from the top‑left corner to the bottom‑right corner.
We will use PyTorch for this task.

Dataset

Because maze generation and solving are deterministic, we can create an infinite dataset on the fly—no manual labeling required!

  • Maze generation: Recursive Division algorithm
  • Maze solving: Depth‑first Search

We will generate mazes of sizes from 5×5 to 127×127 (where n×n denotes the number of blocks horizontally and vertically). Each maze is resized to 128×128 pixels using nearest‑neighbor interpolation. The resizing may introduce slight distortion, but the maze structure remains unchanged.

Example

A sample 57×57 maze and its solution:

Sample maze and solution

Pre‑processing

  • Input: The raw maze image.
  • Label: The solved maze showing only the path (borders and walls are ignored).

The same maze with only the solution path:

Sample maze and path

Both the input and label are resized to 128×128 pixels:

Sample dataset

The Model

We will use a standard U‑Net architecture—nothing fancy—to map the input maze to its solution path.

Training

Hyperparameters

ParameterValue
Batch size16
OptimizerAdam
Loss functionBinary Cross‑Entropy
Learning rate1e‑4
Training set size500 000 samples
Validation set size100 000 samples

Because mazes are generated randomly, an “epoch” (i.e., a full pass through the training set) does not have the usual meaning; each epoch simply corresponds to generating a new batch of mazes.

Results

Training / Validation Loss

The graph below shows training loss computed every 1 000 samples. Validation loss was evaluated on 200 samples after each 1 000‑sample training segment.

Loss graph

Sample Outputs

15 × 15 maze

Output 15x15

57 × 57 maze

Output 57x57

127 × 127 maze

Output 127x127

The model performs well on small and medium mazes, but its accuracy drops on the largest mazes. Further improvements (e.g., deeper networks, attention mechanisms, or diffusion‑based approaches) could address this limitation.

Techniques Companies Predominantly Use Right Now

(Big model, Big data, Big‑time training, Big hardware, Big money)

Conclusion

It has been very interesting working on this project. Neural networks are being used to solve far more complex problems, but it is fascinating to see them applied to algorithmic challenges like these.

  • Here is the link to the GitHub repo if you are curious about what happens behind the scenes.
  • Also, shout‑out to this wonderful repository for providing the U‑Net code.
Back to Blog

Related posts

Read more »