-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsolveDFS.py
57 lines (46 loc) · 2.04 KB
/
solveDFS.py
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
from solve import Solver
from maze import Maze
class SolverDFS(Solver):
MAX = 1000000000
def __init__(self, maze):
super().__init__(maze)
def solveDFS(self, visited, source_y, source_x):
visited[source_y][source_x] = True
if source_y == self.maze_dim - 1 and source_x == self.maze_dim - 1:
return [(source_y, source_x)]
else:
if source_x + 1 < self.maze_dim and not visited[source_y][source_x + 1] and not self.maze_matrix[source_y][source_x + 1][1]:
path = self.solveDFS(visited, source_y, source_x + 1)
if len(path) > 0:
path.append((source_y, source_x))
return path
if source_y + 1 < self.maze_dim and not visited[source_y + 1][source_x] and not self.maze_matrix[source_y + 1][source_x][0]:
path = self.solveDFS(visited, source_y + 1, source_x)
if len(path) > 0:
path.append((source_y, source_x))
return path
if source_x > 0 and not visited[source_y][source_x - 1] and not self.maze_matrix[source_y][source_x][1]:
path = self.solveDFS(visited, source_y, source_x - 1)
if len(path) > 0:
path.append((source_y, source_x))
return path
if source_y > 0 and not visited[source_y - 1][source_x] and not self.maze_matrix[source_y][source_x][0]:
path = self.solveDFS(visited, source_y - 1, source_x)
if len(path) > 0:
path.append((source_y, source_x))
return path
return []
def solve(self):
visited = []
for i in range(self.maze_dim):
visited.append([])
for j in range(self.maze_dim):
visited[i].append(False)
self.solution = self.solveDFS(visited, 0, 0)
if __name__ == '__main__':
maze = Maze(3)
maze.build()
solver = SolverDFS(maze)
solver.solve()
print(maze)
print(solver.get_solution())