You are given a m litre jug and a n litre 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 litres of water where d is less than n.

(X, Y) corresponds to a state where X refers to amount of water in Jug1 and Y refers to amount of water in Jug2

Determine the path from initial state (xi, yi) to 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, Y)->(0, Y) 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), (4, 0), (4, 3), (3, 0), (1, 3), (3, 3), (4, 2), (0, 2)}

We have discussed one solution in The Two Water Jug Puzzle

In this post a BFS based solution is discussed.

We run breadth first search on the states and these states will be created after applying allowed operations and we also use visited map of pair to keep track of states that should be visited only once in the search.This solution can also be achieved using depth first search.

`#include <bits/stdc++.h> ` `#define pii pair<int, int> ` `#define mp make_pair ` `using` `namespace` `std; ` ` ` `void` `BFS(` `int` `a, ` `int` `b, ` `int` `target) ` `{ ` ` ` `// Map is used to store the states, every ` ` ` `// state is hashed to binary value to ` ` ` `// indicate either that state is visited ` ` ` `// before or not ` ` ` `map<pii, ` `int` `> m; ` ` ` `bool` `isSolvable = ` `false` `; ` ` ` `vector<pii> path; ` ` ` ` ` `queue<pii> q; ` `// queue to maintain states ` ` ` `q.push({ 0, 0 }); ` `// Initialing with initial state ` ` ` ` ` `while` `(!q.empty()) { ` ` ` ` ` `pii u = q.front(); ` `// current state ` ` ` ` ` `q.pop(); ` `// pop off used state ` ` ` ` ` `// if this state is already visited ` ` ` `if` `(m[{ u.first, u.second }] == 1) ` ` ` `continue` `; ` ` ` ` ` `// doesn't met jug constraints ` ` ` `if` `((u.first > a || u.second > b || ` ` ` `u.first < 0 || u.second < 0)) ` ` ` `continue` `; ` ` ` ` ` `// filling the vector for constructing ` ` ` `// the solution path ` ` ` `path.push_back({ u.first, u.second }); ` ` ` ` ` `// marking current state as visited ` ` ` `m[{ u.first, u.second }] = 1; ` ` ` ` ` `// if we reach solution state, put ans=1 ` ` ` `if` `(u.first == target || u.second == target) { ` ` ` `isSolvable = ` `true` `; ` ` ` `if` `(u.first == target) { ` ` ` `if` `(u.second != 0) ` ` ` ` ` `// fill final state ` ` ` `path.push_back({ u.first, 0 }); ` ` ` `} ` ` ` `else` `{ ` ` ` `if` `(u.first != 0) ` ` ` ` ` `// fill final state ` ` ` `path.push_back({ 0, u.second }); ` ` ` `} ` ` ` ` ` `// print the solution path ` ` ` `int` `sz = path.size(); ` ` ` `for` `(` `int` `i = 0; i < sz; i++) ` ` ` `cout << ` `"("` `<< path[i].first ` ` ` `<< ` `", "` `<< path[i].second << ` `")\n"` `; ` ` ` `break` `; ` ` ` `} ` ` ` ` ` `// if we have not reached final state ` ` ` `// then, start developing intermediate ` ` ` `// states to reach solution state ` ` ` `q.push({ u.first, b }); ` `// fill Jug2 ` ` ` `q.push({ a, u.second }); ` `// fill Jug1 ` ` ` ` ` `for` `(` `int` `ap = 0; ap <= max(a, b); ap++) { ` ` ` ` ` `// pour amount ap from Jug2 to Jug1 ` ` ` `int` `c = u.first + ap; ` ` ` `int` `d = u.second - ap; ` ` ` ` ` `// check if this state is possible or not ` ` ` `if` `(c == a || (d == 0 && d >= 0)) ` ` ` `q.push({ c, d }); ` ` ` ` ` `// Pour amount ap from Jug 1 to Jug2 ` ` ` `c = u.first - ap; ` ` ` `d = u.second + ap; ` ` ` ` ` `// check if this state is possible or not ` ` ` `if` `((c == 0 && c >= 0) || d == b) ` ` ` `q.push({ c, d }); ` ` ` `} ` ` ` ` ` `q.push({ a, 0 }); ` `// Empty Jug2 ` ` ` `q.push({ 0, b }); ` `// Empty Jug1 ` ` ` `} ` ` ` ` ` `// No, solution exists if ans=0 ` ` ` `if` `(!isSolvable) ` ` ` `cout << ` `"No solution"` `; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `Jug1 = 4, Jug2 = 3, target = 2; ` ` ` `cout << ` `"Path from initial state "` ` ` `"to solution state :\n"` `; ` ` ` `BFS(Jug1, Jug2, target); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

Output:

Path from initial state to solution state :: (0, 0) (0, 3) (4, 0) (4, 3) (3, 0) (1, 3) (3, 3) (4, 2) (0, 2)

This article is contributed by **Abhishek rajput**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Implementing Water Supply Problem using Breadth First Search
- Water Connection Problem
- Check if a given directed graph is strongly connected | Set 2 (Kosaraju using BFS)
- Print all paths from a given source to a destination using BFS
- Traversing directory in Java using BFS
- BFS using STL for competitive coding
- Diameter of n-ary tree using BFS
- Level of Each node in a Tree from source node (using BFS)
- BFS using vectors & queue as per the algorithm of CLRS
- Detect cycle in an undirected graph using BFS
- Finding the path from one vertex to rest using BFS
- Islands in a graph using BFS
- Count number of ways to reach destination in a Maze using BFS
- Implementation of BFS using adjacency matrix
- Find integral points with minimum distance from given set of integers using BFS
- Detect Cycle in a Directed Graph using BFS
- Count the number of nodes at given level in a tree using BFS.
- Program to print all the non-reachable nodes | Using BFS
- Minimum number of Water to Land conversion to make two islands connected in a Grid
- BFS vs DFS for Binary Tree