-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
85 lines (76 loc) · 2.09 KB
/
test.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
class State:
def __init__(self ,x ,y):
self. x =x
self. y =y
def isgoal(self):
return self.y==1
def tostring(self):
return str((self.x ,self.y))
def getlegalactions(self):
result =[]
if self. x >0:
result.append("Empty5")
if self. y >0:
result.append("Empty2")
if self. x<=3 and self.y >0:
result.append("2to5")
if self. x>=2 and self.y==0:
result.append("5to2")
if self. x==1 and self.y <2:
result.append("5to2part")
return result
def step(state ,action):
if action=="Empty5":
return State(0 ,state.y)
if action=="Empty2":
return State(state.x ,0)
if action=="2to5":
return State(state. x +state.y ,0)
if action=="5to2":
return State(state. x -2 ,2)
if action=="5to2part":
return State(0 ,state. y +state.x)
return state
# print(initialstate.getlegalactions())
# print(initialstate.isgoal())
# state=step(initialstate,"Empty5")
# print(state.x,state.y)
# print(state.tostring())
def BFS(state):
# pdb.set_trace()
todo =[(state ,[])]
finished =False
while not finished:
cur,path =todo[0]
del todo[0]
legalactions =cur.getlegalactions()
for action in legalactions:
newstate =step(cur ,action)
todo.append((newstate ,path +[action]))
if newstate.isgoal():
finished =True
return path +[action]
initialstate =State(5 ,0)
path =BFS(initialstate)
print(path)
path = [] # the solution
states = [] # the seen states
def DFS(state):
states.append(state.tostring())
if state.isgoal():
return True
for action in state.getlegalactions():
newstate = step(state, action)
if newstate.tostring() not in states:
pass
else:
continue
path.append(action)
if DFS(newstate):
return True
else:
path.remove(action)
return False
initialstate=State(5,0)
DFS(initialstate)
print(path)