Newer
Older
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
import queue
from collections import deque
def BreadthFirstSearch(state, successor, isgoal):
"""accepts start state, Please do not change this function"""
toDo = queue.Queue()
toDo.put([state])
explored = {state}
while not toDo.empty():
path = toDo.get()
current = path[-1]
if isgoal(current):
return path
for next_state in successor(current):
if next_state not in explored:
explored.add(next_state)
path2 = path + [next_state]
toDo.put(path2)
return "Error Path not found"
def DepthFirstSearch(state, successor, isgoal):
"""accepts start state, Please do not change this function"""
toDo = deque()
toDo.append([state])
explored = {state}
while not toDo:
path = toDo.pop()
current = path[-1]
if isgoal(current):
return path
for next_state in successor(current):
if next_state not in explored:
explored.add(next_state)
path2 = path + [next_state]
toDo.append(path2)
return "Error Path not found"