Solve all 4x4 mazes simultaneously with least moves
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
If "simultaneously" means you must apply the same move sequence to every 4x4 maze at once, then the problem is not just running BFS on each maze separately. It becomes a single shortest-path search over a combined state space where one action, such as U or R, updates every maze position at the same time.
Model the Problem as a Product State
For one maze, a state is just the current cell. For k mazes solved together, a state is a tuple of k positions:
(p1, p2, ..., pk)
From that combined state, one move is chosen from:
- up
- down
- left
- right
The same chosen move is applied to every maze. If a move is blocked by a wall in one maze, that maze simply stays in place while the others move if they can.
The goal state is reached when every maze position is at its own exit cell.
Why Breadth-First Search Is the Right Tool
Each move has the same cost, so ordinary BFS gives the shortest sequence of simultaneous moves. The algorithm is:
- start from the tuple of starting positions
- try the four possible global moves
- enqueue unseen combined states
- stop when all mazes are solved
That gives the least number of moves by construction.
A Runnable Python Example
The code below uses simple adjacency maps rather than drawing maze walls directly. Each maze is a dictionary from a cell to the cells reachable by U, D, L, or R.
This BFS returns the shortest shared move sequence if one exists.
Why Solving Each Maze Separately Is Not Enough
If you solve each maze independently and then try to combine the answers, you usually lose optimality or feasibility. One maze may want U at a step where another needs R. The simultaneous version is constrained by one shared control stream, so the coupled search is the real problem.
That is why the product-state BFS is the correct model.
Complexity on 4x4 Mazes
Each individual maze has at most 16 positions. For k mazes, the worst-case combined state space is 16^k. That grows quickly, but 4x4 mazes are small enough that BFS can still be practical for a modest number of mazes, especially with pruning and symmetry reduction.
If you truly mean "all possible 4x4 mazes," the search space becomes enormous and you will need stronger compression or dynamic programming ideas. But for a fixed collection of mazes, product-state BFS is the standard exact method.
Common Pitfalls
- Running BFS on each maze independently even though the move sequence must be shared.
- Forgetting that a blocked move should usually leave that maze in place rather than invalidate the whole step.
- Treating this as a shortest-path problem in one maze instead of a shortest-path problem in the product graph.
- Underestimating the
16^kgrowth of the combined state space. - Not storing visited combined states, which makes the search explode unnecessarily.
Summary
- If one move sequence controls all mazes, the correct state is the tuple of all positions.
- Use BFS on that combined state graph to get the least number of simultaneous moves.
- Independent per-maze shortest paths do not solve the coupled problem.
- For a modest number of 4x4 mazes, product-state BFS is exact and practical.
- The main challenge is managing state-space growth as the number of mazes increases.
Related reading
- Solving a graph issue with Python
- Solving a puzzle using search algorithms
- Solving linear equations represented as a string
- Solving N-Queens Problem... How far can we go?
- Solving Range Minimum Queries using Binary Indexed Trees Fenwick Trees
- Sort a vector in which the n first elements have been already sorted?
- Solving string reduction challenge
- Solving The 8 Puzzle With A Algorithm

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.