-
Notifications
You must be signed in to change notification settings - Fork 19
/
bfs.py
24 lines (20 loc) · 817 Bytes
/
bfs.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
from collections import deque
from solver import Solver
class BFS(Solver):
def __init__(self, initial_state):
super(BFS, self).__init__(initial_state)
self.frontier = deque()
def solve(self):
self.frontier.append(self.initial_state)
while self.frontier:
board = self.frontier.popleft()
self.explored_nodes.add(tuple(board.state))
if board.goal_test():
self.set_solution(board)
break
for neighbor in board.neighbors():
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