-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWater_Jug_BFS.py
More file actions
61 lines (52 loc) · 1.54 KB
/
Copy pathWater_Jug_BFS.py
File metadata and controls
61 lines (52 loc) · 1.54 KB
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
def BFS(a,b,ta=0,tb=0):
hm={}
flag,e2=False,False
path,queue=[],[]
queue.append((0,0))
t,target=0 if ta else -1,ta if ta else tb
while queue:
#print(queue)
cs=queue[0]#current state
queue.pop(0)
if hm.get(cs,0)==1:
continue
if cs[0]>a or cs[-1]>b or cs[0]<0 or cs[-1]<0:
continue
path.append(cs)
hm[cs]=1
if cs[t]==target:
flag=True
for p in path[:-1]:
print(p,end='->')
print(path[-1])
if not e2:
print('Or')
e2=1
hm={}
flag=False
path,queue=[],[]
queue.append((0,0))
continue
break
if not e2:
queue.append((cs[0],b))
queue.append((a,cs[-1]))
else:
queue.append((a,cs[-1]))
queue.append((cs[0],b))
for ap in range(max(a,b)+1):
c,d=cs[0]+ap,cs[-1]-ap
if c==a or (d==0 and d>=0):
queue.append((c,d))
c,d=cs[0]-ap,cs[-1]+ap
if (c==0 and c>=0) or d==b:
queue.append((c,d))
if not e2:
queue.append((0,cs[-1]))
queue.append((cs[0],0))
else:
queue.append((cs[0],0))
queue.append((0,cs[-1]))
if not flag:
print('No Soluton')
BFS(4,3,2)