Classic water jug problem simulation. A Water Jug Problem: You are given two jugs, a 4lit one and a 3lit one, a pump which has. SWI-Prolog version 7.2.0. Prolog program of water jug problem. Domains x,y=integer. Predicates f(x,y). F(X,Y):-X+Y=4,Y0,X4,!, Y1=Y-4+X, write('n 4 ',Y1), f(4,Y1);X+Y=4,Y0,Y3,!, Y1=Y-4+X, write('n 4 ',Y1), f(4,Y1).
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).
(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:
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.
#define pii pair<int, int> using namespace std; void BFS( int a, int b, int target) // Map is used to store the states, every // indicate either that state is visited map<pii, int > m; 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 continue ; // doesn't met jug constraints u.first < 0 || u.second < 0)) // the solution path m[{ u.first, u.second }] = 1; // if we reach solution state, put ans=1 isSolvable = true ; if (u.second != 0) // fill final state } if (u.first != 0) // fill final state } // print the solution path for ( int i = 0; i < sz; i++) << ', ' << path[i].second << ')n' ; } // if we have not reached final state // states to reach solution state q.push({ a, u.second }); // fill Jug1 for ( int ap = 0; ap <= max(a, b); ap++) { // pour amount ap from Jug2 to Jug1 int d = u.second - ap; // check if this state is possible or not q.push({ c, d }); // Pour amount ap from Jug 1 to Jug2 d = u.second + ap; // check if this state is possible or not q.push({ c, d }); q.push({ 0, b }); // Empty Jug1 if (!isSolvable) } // Driver code { cout << 'Path from initial state ' BFS(Jug1, Jug2, target); } |
Output:
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 [email protected]. 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.
Recommended Posts:
Active7 months ago
It is a water jug problem. The larger bucket holds 5, the smaller bucket holds 3. I want to get 4 in the larger bucket.
The problem is that when I run I cannot get any answer, it produces an error. It doesn't seem like an obvious error, the algorithm is simple and direct.
Could anyone help me to find what is wrong with it?
false10k99 gold badges7676 silver badges155155 bronze badges
leaflyflyleaflyfly
1 Answer
You're not using 'is' for the assignments.
Your
puzzle0
predicate has two unbound variables: X
& Y
.And your
path4
predicates are faulty.Try these:
Below is my solution to this problem:
I get these solutions:
Obviously the second to last, being the shortest, is the best.
EnigmativityEnigmativity81.9k99 gold badges6868 silver badges137137 bronze badges