A sample maze:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XS  X X   X   X   X X X   X     X       X
X X X XXX X X XXX X X XXX X X XXXXXXX XXX
X X         X X X   X   X   X     X   X X
X X XXXXXXXXX X XXX X XXX XXXXXXXXXXX X X
X X   X     X               X   X     X X
XXX XXXXX XXXXX X XXX X X XXX XXX X X X X
X       X   X   X X   X X         X X X X
XXX XXX XXX XXX XXXXX XXX XXX X XXX X X X
X   X   X   X       X X   X   X   X X X X
XXXXX XXXXX XXX XXX XXXXX XXX X XXXXXXX X
X   X     X       X X X   X   X         X
XXX X XXXXX XXX XXX X XXXXX X XXXXX X XXX
X   X X X   X   X X     X X X X   X X X X
X XXX X X XXXXX X X X XXX XXX X X X X X X
X     X X   X     X X X X     X X X X   X
X XXX X X XXXXXXX XXXXX XXXXX XXX XXX X X
X X X X X X X X X   X   X           X X X
X X XXX X X X X X XXX XXXXXXXXX XXXXXXXXX
X X     X X X X X X X     X   X     X   X
XXXXX XXXXX X X X X XXX X XXX XXXXXXX XXX
X X       X X   X     X X   X     X   X X
X XXX X XXX X X XXXXX X XXXXX XXX X X X X
X     X X     X       X       X   X X   X
X X XXXXXXXXXXXXX X XXXXXXX XXXXX X XXX X
X X X       X X   X       X     X     X X
XXX X XXXXXXX XXXXX XXX X XXXXX XXX XXXXX
X X   X   X   X   X X   X X X   X X X   X
X XXX X X X XXX XXXXX XXXXX XXXXX X XXX X
X   X   X X     X     X X   X       X   X
XXX X XXXXXXXXX X XXXXX XXX X X XXX X X X
X     X X X       X     X   X X X     X X
XXXXX X X X XXXXXXX X X XXX XXXXX XXX XXX
X X             X   X X     X X   X   X X
X X XXXXXXX X XXXXXXXXXXX X X X XXX XXX X
X         X X             X X   X       X
XXXXX XXXXX XXX XXXXXXXXXXX X X X XXXXX X
X X   X X X X       X         X X X     X
X X X X X XXX XXXXX XXX X XXXXXXX X X X X
X   X   X       X   X   X     X   X X XEX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

The maze solving script

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import sys
import random
import colorama
from colorama import Fore, Back, Style

PATH, WALL, START, END, SEEN, SOLUTION = ' #SE.@'
BORING_THINGS = [WALL, SEEN]

def main():
    try:
        path = sys.argv[1]
    except IndexError:
        print "Give me a maze!"
        sys.exit(0)

    maze = open(path, 'r').read()

    ms = MazeSolver(maze)
    solved_maze = ms.solve()
    ms.print_maze(solved_maze)

class MazeSolver:
    def __init__(self, maze):
        self.maze = [list(row) for row in maze.splitlines()]

    def _find_start(self):
        for y, row in enumerate(self.maze):
            if START in row:
                x = row.index(START)
                return x, y

    def _explore(self, x, y):
        current_cell = self.maze[y][x]
        if current_cell in BORING_THINGS:
            return False
        if current_cell == PATH:
            self.maze[y][x] = SEEN
        if current_cell == END:
            return True

        directions = [(x-1,y), (x+1,y), (x,y-1), (x,y+1)]
        random.shuffle(directions)
        for direction in directions:
            x, y = direction
            if self._explore(x, y):
                self.maze[y][x] = SOLUTION
                return True

        # elif (self._explore(x-1,y) or self._explore(x+1,y) or
              # self._explore(x,y-1) or self._explore(x,y+1)):
            # self.maze[y][x] = SOLUTION
            # return True

        return False

    def _print_symbol(self, symbol):
        if symbol == SOLUTION:
            sys.stdout.write(Back.GREEN+' '+Style.RESET_ALL)
        elif symbol == SEEN:
            sys.stdout.write(Back.RED+' '+Style.RESET_ALL)
        else:
            sys.stdout.write(symbol)

    def print_maze(self, maze):
        colorama.init()
        for row in maze:
            for symbol in row:
                self._print_symbol(symbol)
            sys.stdout.write('\n')

    def solve(self):
        x, y = self._find_start()       
        self._explore(x, y)

        return self.maze

if __name__ == '__main__':
    main()