- This project is a simple maze solver based on Breadth-First Search (BFS).
- It reads a maze from a text file, finds the shortest path from
S(Start) toG(Goal), and outputs the result to a new file. - Walls are represented by
#, open paths by., start byS, and goal byG. - The solved path is visualized using directional arrows:
^: move up>: move rightv: move down<: move left
The maze is provided as a plain text file (in.txt) with the following rules:
#: wall.: free cellS: start position (must be unique)G: goal position (must be unique)- All rows must have the same length.
########################################
#S#.......#.......#.........#..........#
#.#.#####.#.#####.#.#######.#.########.#
#...#...#...#...#...#.....#...#........#
#####.#.#######.#.###.###.#####.######.#
#.....#.........#.....#.#.......#......#
#.#####.#######.#######.#.#######.####.#
#.#.....#.......#.......#.#.......#....#
#.#.#####.#######.#######.#.#######.####
#.#.#...#.#.......#.....#.#.#.....#....#
#.#.#.#.#.#.#######.###.#.#.#.###.####.#
#.#.#.#.#.#.#.......#...#.#.#.#.#......#
#.###.#.#.#.#.#######.###.#.#.#.########
#.....#.#.#.#.......#.#...#.#.#........#
#######.#.#.#######.#.#.###.#.########.#
#.......#.#.......#.#.#.#...#.#........#
#.#######.#######.#.#.#.#.###.#.########
#.#.......#.......#.#.#.#.#...#.#......#
#.#.#######.#######.#.#.#.#.###.#.####.#
#.#.#.......#.......#.#.#.#.#...#.#....#
#.#.#.#######.#######.#.###.#.###.#.####
#.#.#.#.......#.......#.#...#.#...#....#
#.#.#.#.#######.#######.#.###.#.#######.
#.#.#.#.#.......#.......#.#...#........#
#.#.###.#.#######.#######.#.##########.#
#.#.....#.#.......#.......#.#..........#
#.#######.#.#######.#######.#.##########
#.........#.........#.......#..........#
###########.#########.#######.########.#
#...........#.........#.......#........#
#.###########.#######.#.#######.######.#
#.#...........#.......#.#.......#......#
#.#.###########.#######.#.#######.####.#
#.#.#...........#.......#.#.......#....#
#.#.#.###########.#######.#.#######.####
#.#.#.#...........#.......#.#.......#..#
#.#.#.#.###########.#######.#.#######..#
#...#...#...........#.......#.........G#
########################################
- The result is written to
out.txt. - The shortest path from
StoGis marked using directional arrows. SandGremain unchanged in the output.
########################################
#S#>>>>>>v#>>>>>>v#.........#..........#
#v#^#####v#^#####v#.#######.#.########.#
#>>^#...#>>^#...#v..#>>>>v#...#........#
#####.#.#######.#v###^###v#####.######.#
#.....#.........#>>>>^#.#v......#......#
#.#####.#######.#######.#v#######.####.#
#.#.....#.......#.......#v#.......#....#
#.#.#####.#######.#######v#.#######.####
#.#.#...#.#.......#.....#v#.#.....#....#
#.#.#.#.#.#.#######.###.#v#.#.###.####.#
#.#.#.#.#.#.#.......#...#v#.#.#.#......#
#.###.#.#.#.#.#######.###v#.#.#.########
#.....#.#.#.#.......#.#v<<#.#.#........#
#######.#.#.#######.#.#v###.#.########.#
#.......#.#.......#.#.#v#...#.#........#
#.#######.#######.#.#.#v#.###.#.########
#.#.......#.......#.#.#v#.#...#.#......#
#.#.#######.#######.#.#v#.#.###.#.####.#
#.#.#.......#.......#.#v#.#.#...#.#....#
#.#.#.#######.#######.#v###.#.###.#.####
#.#.#.#.......#.......#v#...#.#...#....#
#.#.#.#.#######.#######v#.###.#.#######.
#.#.#.#.#.......#v<<<<<<#.#...#........#
#.#.###.#.#######v#######.#.##########.#
#.#.....#.#v<<<<<<#.......#.#..........#
#.#######.#v#######.#######.#.##########
#.........#v........#.......#>>>>>>>>>v#
###########v#########.#######^########v#
#v<<<<<<<<<<#>>>>>>>>v#>>>>>>^#.......v#
#v###########^#######v#^#######.######v#
#v#>>>>>>>>>>^#v<<<<<<#^#.......#.....v#
#v#^###########v#######^#.#######.####v#
#v#^#v<<<<<<<<<<#>>>>>>^#.#.......#v<<<#
#v#^#v###########^#######.#.#######v####
#v#^#v#>>>>>>>>>>^#.......#.#v<<<<<<#..#
#v#^#v#^###########.#######.#v#######..#
#>>^#>>^#...........#.......#>>>>>>>>>G#
########################################
- Uses Breadth-First Search (BFS) to compute the shortest distance from
Sto every reachable cell. - Once
Gis reached, the algorithm reconstructs the path by moving backward along decreasing distance values. - Each step is replaced with an arrow indicating the direction of movement toward
G.
Require Python 3.
Clone this repository:
git clone https://github.com/pantsman-jp/maze-solver
- Prepare an input file named
in.txtin the project directory. - Run the solver:
python main.py
- The solved maze will be written to
out.txt.
Copyright © 2026 pantsman