-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathdfs.py
22 lines (19 loc) · 783 Bytes
/
dfs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
from solver import Solver
class DFS(Solver):
def __init__(self, initial_state):
super(DFS, self).__init__(initial_state)
self.frontier = []
def solve(self):
self.frontier.append(self.initial_state)
while self.frontier:
board = self.frontier.pop()
self.explored_nodes.add(tuple(board.state))
if board.goal_test():
self.set_solution(board)
break
for neighbor in board.neighbors()[::-1]:
if tuple(neighbor.state) not in self.explored_nodes:
self.frontier.append(neighbor)
self.explored_nodes.add(tuple(neighbor.state))
self.max_depth = max(self.max_depth, neighbor.depth)
return