-
Notifications
You must be signed in to change notification settings - Fork 123
/
Copy pathwaterjug.py
28 lines (23 loc) · 831 Bytes
/
waterjug.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
from collections import defaultdict
jug1=int(input("Enter amt in jug 1\n"))
jug2=int(input("Enter amt in jug 2\n"))
aim=int(input("Enter amt desiredin\n" ))
visited = defaultdict(lambda: False)
def waterJugSolver(amt1, amt2) :
if (amt1 == aim and amt2 ==0) or (amt2 == aim and amt1 == 0) :
print(amt1, amt2)
return True
if visited[(amt1, amt2)] == False:
print(amt1, amt2)
visited[ (amt1, amt2)] = True
return (waterJugSolver(0, amt2) or
waterJugSolver(amt1, 0) or
waterJugSolver(jug1, amt2) or
waterJugSolver(amt1, jug2) or
waterJugSolver(amt1, min(amt2, (jug1-amt1))) or
waterJugSolver(amt1 - min (amt1, (jug2-amt2)),
amt2 + min(amt1, (jug2-amt2))))
else:
return False
print("Steps: ")
waterJugSolver(0, 0)