main:-
enter(monkey,room).
enter(monkey,room):-
write("Monkey enters the room"),nl,
checkheight(monkey,banana).
checkheight(monkey,banana):-
write("Monkey cannot reach the banana"),nl,
move(monkey,left).
move(monkey,left):-
write("Monkey moves towards the chair"),nl,
push(monkey,chair).
push(monkey,chair):-
write("Monkey pushes the chair towards banana"),nl,
place(monkey,chair).
place(monkey,chair):-
write("Monkey places the chair below banana"),nl,
climb(monkey,chair).
climb(monkey,chair):-
write("monkey climbs the chair"),nl,
function(check).
function(check):-
write('Enter height of the room'),
read(H1),
write('enter height of the chair'),
read(H2),
write('enter height of monkey'),
read(H3),
B is H2 + H3, check(H1,B). gets(monkey,stick):-
write("Monkey Move Towards The Stick"),nl,
write("Monkey Gets The Stick and Climbs Again"),nl,
write('Enter Height of Room: '),
read(N1),
write('Enter Height of Chair: '),
read(N2),
write('Enter Height of Monkey: '),
read(N3),
write('Enter Height of Stick: '),
read(N4),
SUM is N2+N3+N4,
check(N1,SUM). check(A,B):-
(
A>B-> write("Monkey Still Cannot Reach The
Banana"),nl, gets(monkey,stick);
A<B->
write("Monkey Ate The Banana");
A=B-> write("Monkey Ate The
Banana")
).
from collections import defaultdict
visited = defaultdict(lambda: False)
jug1=int(input("Enter Capacity of Jug 1 : "))
jug2=int(input("Enter Capacity of Jug 2 : "))
aim=int(input("Enter Goal : "))
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)),
amt2 - min(amt2, (jug1-amt1))) or
waterJugSolver(amt1 - min(amt1, (jug2-amt2)),
amt2 + min(amt1, (jug2-amt2))))
else:
return False
print("Steps: ")
waterJugSolver(0,0)
0 Comments