You are given an m liter jug and a n liter jug. Both the jugs are initially empty. The jugs don’t have markings to allow measuring smaller quantities. You have to use the jugs to measure d liters of water where d is less than n.
(X, Y) corresponds to a state where X refers to the amount of water in Jug1 and Y refers to the amount of water in Jug2
Determine the path from e initial state (xi, yi) to the final state (xf, yf), where (xi, yi) is (0, 0) which indicates both Jugs are initially empty and (xf, yf) indicates a state which could be (0, d) or (d, 0).
The operations you can perform are:
- Empty a jug (X, 0)->(0, 0) Empty Jug 1.
- Fill a Jug, (0, 0)->(X, 0) Fill Jug 1
- Pour water from one jug to the other until one of the jugs is either empty or full, (X, Y) -> (X-d, Y+d)
Examples:
Input : 4 3 2
Output : {( 0,0),(0,3),(3,0),(3,3),(4,2),(0,2)}
We have discussed the optimal solution in The Two Water Jug Puzzle. In this post, a BFS based solution is discussed.
Here, we keep exploring all the different valid cases of the states of water in the jug simultaneously until and unless we reach the required target water.
As provided in the problem statement, at any given state we can do either of the following operations:
- Fill a jug
- Empty a jug
- Transfer water from one jug to another until either of them gets completely filled or empty.
Examples:
Input: X = 4, Y = 3, Z = 2
Output: {(0, 0), (0, 3), (3, 0), (3, 3), (4, 2), (0, 2)}
Explanation:
Step 1:- First we will fill the 4 litre jug completely with water.
Step 2:- Then optimal approach would be to empty water from 4-litre jug into 3-litre (leaving 1L water in 4L jug and 3L completely full). Hence we got 1L water.
Step 3:- Now, Empty water from 3L.
Step 4:- Pour the water from 4L jug into 3L jug Now 4L container is completely empty and 1L water in present in 3L litre jug.
Step 5:- Fill the 4L jug with water completely again.
Step 6:- On transferring water from 4L jug to 3L jug, we will get 2L water in 4L jug which was our required quantity.
Input: X = 3, Y = 5, Z = 4
Output: 6
Explanation:
Step 1:- First we will fill the 5-litres jug to its maximum capacity.
Step 2:- Then optimal approach would be to transfer 3-litres from 5-litres jug to 3-litres jugs.
Step 3:- Now, Empty the 3-litres jug.
Step 4:- Transfer 2L from 5L jug to 3-L jug.
Step 5:- Now, Fill 5-litres jug to its maximum capacity.
Step 6:- On Pouring water from 5L jug to 3L jug until it’s full we will get 4L water in 5-litre jug which was our required quantity.
Running of the algorithm:
We start at an initial state in the queue where both the jugs are empty. We then continue to explore all the possible intermediate states derived from the current jug state using the operations provided.
We also, maintain a visited matrix of states so that we avoid revisiting the same state of jugs again and again.
|
Case 1 | Fill it | Empty it | ? |
Case 2 | Empty it | Fill it | ? |
Case 3 | Fill it | Fill it | Redundant case |
Case 4 | Empty it | Empty it | Already visited (Initial State) |
Case 5 | Unchanged | Fill it | ? |
Case 6 | Fill it | Unchanged | ? |
Case 7 | Unchanged | Empty | ? |
Case 8 | Empty | Unchanged | ? |
Case 9 | Transfer water from this | Transfer water into this | ? |
Case 10 | Transfer water into this | Transfer water from this | ? |
From the table above, we can observe that the state where both the jugs are filled is redundant as we won’t be able to continue ahead / do anything with this state in any possible way.
So, we proceed, keeping in mind all the valid state cases (as shown in the table above) and we do a BFS on them.
In the BFS, we first skip the states which was already visited or if the amount of water in either of the jugs exceeded the jug quantity.
If we continue further, then we first mark the current state as visited and check if in this state, if we have obtained the target quantity of water in either of the jugs, we can empty the other jug and return the current state’s entire path.
But, if we have not yet found the target quantity, we then derive the intermediate states from the current state of jugs i.e. we derive the valid cases, mentioned in the table above (go through the code once if you have some confusion).
We keep repeating all the above steps until we have found our target or there are no more states left to proceed with.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
typedef pair< int , int > pii;
void printpath(map<pii, pii> mp, pii u)
{
if (u.first == 0 && u.second == 0) {
cout << 0 << " " << 0 << endl;
return ;
}
printpath(mp, mp[u]);
cout << u.first << " " << u.second << endl;
}
void BFS( int a, int b, int target)
{
map<pii, int > m;
bool isSolvable = false ;
map<pii, pii> mp;
queue<pii> q;
q.push(make_pair(0, 0));
while (!q.empty()) {
auto u = q.front();
q.pop();
if (m[u] == 1)
continue ;
if ((u.first > a || u.second > b || u.first < 0
|| u.second < 0))
continue ;
m[{ u.first, u.second }] = 1;
if (u.first == target || u.second == target) {
isSolvable = true ;
printpath(mp, u);
if (u.first == target) {
if (u.second != 0)
cout << u.first << " " << 0 << endl;
}
else {
if (u.first != 0)
cout << 0 << " " << u.second << endl;
}
return ;
}
if (m[{ u.first, b }] != 1) {
q.push({ u.first, b });
mp[{ u.first, b }] = u;
}
if (m[{ a, u.second }] != 1) {
q.push({ a, u.second });
mp[{ a, u.second }] = u;
}
int d = b - u.second;
if (u.first >= d) {
int c = u.first - d;
if (m[{ c, b }] != 1) {
q.push({ c, b });
mp[{ c, b }] = u;
}
}
else {
int c = u.first + u.second;
if (m[{ 0, c }] != 1) {
q.push({ 0, c });
mp[{ 0, c }] = u;
}
}
d = a - u.first;
if (u.second >= d) {
int c = u.second - d;
if (m[{ a, c }] != 1) {
q.push({ a, c });
mp[{ a, c }] = u;
}
}
else {
int c = u.first + u.second;
if (m[{ c, 0 }] != 1) {
q.push({ c, 0 });
mp[{ c, 0 }] = u;
}
}
if (m[{ u.first, 0 }] != 1) {
q.push({ u.first, 0 });
mp[{ u.first, 0 }] = u;
}
if (m[{ 0, u.second }] != 1) {
q.push({ 0, u.second });
mp[{ 0, u.second }] = u;
}
}
if (!isSolvable)
cout << "No solution" ;
}
int main()
{
int Jug1 = 4, Jug2 = 3, target = 2;
cout << "Path from initial state "
"to solution state ::\n" ;
BFS(Jug1, Jug2, target);
return 0;
}
|
Java
import java.util.*;
class Pair {
int j1, j2;
List<Pair> path;
Pair( int j1, int j2)
{
this .j1 = j1;
this .j2 = j2;
path = new ArrayList<>();
}
Pair( int j1, int j2, List<Pair> _path)
{
this .j1 = j1;
this .j2 = j2;
path = new ArrayList<>();
path.addAll(_path);
path.add( new Pair( this .j1, this .j2));
}
}
public class GFG {
public static void main(String[] args)
throws java.lang.Exception
{
int jug1 = 4 ;
int jug2 = 3 ;
int target = 2 ;
getPathIfPossible(jug1, jug2, target);
}
private static void
getPathIfPossible( int jug1, int jug2, int target)
{
boolean [][] visited
= new boolean [jug1 + 1 ][jug2 + 1 ];
Queue<Pair> queue = new LinkedList<>();
Pair initialState = new Pair( 0 , 0 );
initialState.path.add( new Pair( 0 , 0 ));
queue.offer(initialState);
while (!queue.isEmpty()) {
Pair curr = queue.poll();
if (curr.j1 > jug1 || curr.j2 > jug2
|| visited[curr.j1][curr.j2])
continue ;
visited[curr.j1][curr.j2] = true ;
if (curr.j1 == target || curr.j2 == target) {
if (curr.j1 == target) {
curr.path.add( new Pair(curr.j1, 0 ));
}
else {
curr.path.add( new Pair( 0 , curr.j2));
}
int n = curr.path.size();
System.out.println(
"Path of states of jugs followed is :" );
for ( int i = 0 ; i < n; i++)
System.out.println(
curr.path.get(i).j1 + " , "
+ curr.path.get(i).j2);
return ;
}
queue.offer( new Pair(jug1, 0 , curr.path));
queue.offer( new Pair( 0 , jug2, curr.path));
queue.offer( new Pair(jug1, curr.j2, curr.path));
queue.offer( new Pair(curr.j1, jug2, curr.path));
queue.offer( new Pair( 0 , curr.j2, curr.path));
queue.offer( new Pair(curr.j1, 0 , curr.path));
int emptyJug = jug2 - curr.j2;
int amountTransferred
= Math.min(curr.j1, emptyJug);
int j2 = curr.j2 + amountTransferred;
int j1 = curr.j1 - amountTransferred;
queue.offer( new Pair(j1, j2, curr.path));
emptyJug = jug1 - curr.j1;
amountTransferred = Math.min(curr.j2, emptyJug);
j2 = curr.j2 - amountTransferred;
j1 = curr.j1 + amountTransferred;
queue.offer( new Pair(j1, j2, curr.path));
}
System.out.println( "Not Possible to obtain target" );
}
}
|
Python3
from collections import deque
def BFS(a, b, target):
m = {}
isSolvable = False
path = []
q = deque()
q.append(( 0 , 0 ))
while ( len (q) > 0 ):
u = q.popleft()
if ((u[ 0 ], u[ 1 ]) in m):
continue
if ((u[ 0 ] > a or u[ 1 ] > b or
u[ 0 ] < 0 or u[ 1 ] < 0 )):
continue
path.append([u[ 0 ], u[ 1 ]])
m[(u[ 0 ], u[ 1 ])] = 1
if (u[ 0 ] = = target or u[ 1 ] = = target):
isSolvable = True
if (u[ 0 ] = = target):
if (u[ 1 ] ! = 0 ):
path.append([u[ 0 ], 0 ])
else :
if (u[ 0 ] ! = 0 ):
path.append([ 0 , u[ 1 ]])
sz = len (path)
for i in range (sz):
print ( "(" , path[i][ 0 ], "," ,
path[i][ 1 ], ")" )
break
q.append([u[ 0 ], b])
q.append([a, u[ 1 ]])
for ap in range ( max (a, b) + 1 ):
c = u[ 0 ] + ap
d = u[ 1 ] - ap
if (c = = a or (d = = 0 and d > = 0 )):
q.append([c, d])
c = u[ 0 ] - ap
d = u[ 1 ] + ap
if ((c = = 0 and c > = 0 ) or d = = b):
q.append([c, d])
q.append([a, 0 ])
q.append([ 0 , b])
if ( not isSolvable):
print ( "No solution" )
if __name__ = = '__main__' :
Jug1, Jug2, target = 4 , 3 , 2
print ( "Path from initial state "
"to solution state ::" )
BFS(Jug1, Jug2, target)
|
C#
using System;
using System.Collections.Generic;
class GFG {
static void BFS( int a, int b, int target)
{
Dictionary<Tuple< int , int >, int > m
= new Dictionary<Tuple< int , int >, int >();
bool isSolvable = false ;
List<Tuple< int , int > > path
= new List<Tuple< int , int > >();
List<Tuple< int , int > > q
= new List<Tuple< int , int > >();
q.Add( new Tuple< int , int >(0, 0));
while (q.Count > 0) {
Tuple< int , int > u = q[0];
q.RemoveAt(0);
if (m.ContainsKey(u) && m[u] == 1)
continue ;
if ((u.Item1 > a || u.Item2 > b || u.Item1 < 0
|| u.Item2 < 0))
continue ;
path.Add(u);
m[u] = 1;
if (u.Item1 == target || u.Item2 == target) {
isSolvable = true ;
if (u.Item1 == target) {
if (u.Item2 != 0)
path.Add( new Tuple< int , int >(
u.Item1, 0));
}
else {
if (u.Item1 != 0)
path.Add( new Tuple< int , int >(
0, u.Item2));
}
int sz = path.Count;
for ( int i = 0; i < sz; i++)
Console.WriteLine( "(" + path[i].Item1
+ ", " + path[i].Item2
+ ")" );
break ;
}
q.Add( new Tuple< int , int >(u.Item1, b));
q.Add( new Tuple< int , int >(a, u.Item2));
for ( int ap = 0; ap <= Math.Max(a, b); ap++) {
int c = u.Item1 + ap;
int d = u.Item2 - ap;
if (c == a || (d == 0 && d >= 0))
q.Add( new Tuple< int , int >(c, d));
c = u.Item1 - ap;
d = u.Item2 + ap;
if ((c == 0 && c >= 0) || d == b)
q.Add( new Tuple< int , int >(c, d));
}
q.Add( new Tuple< int , int >(a, 0));
q.Add( new Tuple< int , int >(0, b));
}
if (!isSolvable)
Console.WriteLine( "No solution" );
}
static void Main()
{
int Jug1 = 4, Jug2 = 3, target = 2;
Console.WriteLine( "Path from initial state "
+ "to solution state ::" );
BFS(Jug1, Jug2, target);
}
}
|
Javascript
<script>
function BFS(a,b,target)
{
let m = new Array(1000);
for (let i=0;i<1000;i++){
m[i]= new Array(1000);
for (let j=0;j<1000;j++)
{
m[i][j]=-1;
}
}
let isSolvable = false ;
let path = [];
let q = [];
q.push([0,0]);
while (q.length!=0) {
let u = q[0];
q.shift();
if ((u[0] > a || u[1] > b ||
u[0] < 0 || u[1] < 0))
continue ;
if (m[u[0]][u[1]] > -1)
continue ;
path.push([u[0],u[1]]);
m[u[0]][u[1]] = 1;
if (u[0] == target || u[1] == target) {
isSolvable = true ;
if (u[0] == target) {
if (u[1] != 0)
path.push([u[0],0]);
}
else {
if (u[0] != 0)
path.push([0,u[1]]);
}
let sz = path.length;
for (let i = 0; i < sz; i++)
document.write( "(" + path[i][0]
+ ", " + path[i][1] + ")<br>" );
break ;
}
q.push([u[0],b]);
q.push([a,u[1]]);
for (let ap = 0; ap <= Math.max(a, b); ap++) {
let c = u[0] + ap;
let d = u[1] - ap;
if (c == a || (d == 0 && d >= 0))
q.push([c,d]);
c = u[0] - ap;
d = u[1] + ap;
if ((c == 0 && c >= 0) || d == b)
q.push([c,d]);
}
q.push([a,0]);
q.push([0,b]);
}
if (!isSolvable)
document.write( "No solution" );
}
let Jug1 = 4, Jug2 = 3, target = 2;
document.write( "Path from initial state " +
"to solution state ::<br>" );
BFS(Jug1, Jug2, target);
</script>
|
OutputPath of states of jugs followed is :
0 , 0
0 , 3
3 , 0
3 , 3
4 , 2
0 , 2
Time Complexity: O(n*m).
Space Complexity: O(n*m). Where n and m are the quantity of jug1 and jug2, respectively. This article has been improved by Sparsh Sharma.