Experiment 5
Experiment 5
Theory :- The Water Jug Problem involves measuring a specific amount of water
using two jugs of different capacities. The allowed operations include filling a jug
to its full capacity, completely emptying it, or transferring water from one jug to
the other until either the first jug is empty or the second is full. The goal is to
reach a state where at least one jug contains the desired volume of water. This
problem is commonly solved using systematic search techniques, such
as Breadth-First Search (BFS), to explore all possible states and find the
optimal sequence of actions. It is a classic puzzle often associated with the Die
Hard movie scenario, where precise measurements must be achieved with
limited resources.
Code :-
% Main predicate to solve the water jug problem
% Usage: water_jug(4, 3, 2, Solution).
% Returns Solution as a list of states from initial to goal
water_jug(Jug1Cap, Jug2Cap, Target, Solution) :-
% Basic input validation
Jug1Cap > 0,
Jug2Cap > 0,
Target >= 0,
Target =< max(Jug1Cap, Jug2Cap),
% BFS implementation
bfs([[State|Path]|_], _, _, Target, _, [State|Path]) :-
% Check if current state meets target
(State = (Target, _); State = (_, Target)).