I'm trying to solve the famous "water jug puzzle" (like in the movie Die Hard with a Vengeance).
I solve easily the puzzle with supports of 2 jugs in C#.
But now I would like to achieve the same with N number of jugs (2, 3, 4, 5, ...). It completely changes the way the algorithm works.
My current solution works on the following principle :
Then, I repeat the operation by reversing the order of the jugs and I take the fastest solution between the two.
This system works great with 2 jugs but cannot work with more jugs.
Reminder of the rules: You have N jugs of water, each with a specific capacity. It is possible to fill a jug completely from a faucet, empty a jug completely into a sink, and transfer the contents of one jug to another (subject to available capacity of target and current fill of source).
The goal is to get ONE jug with a given amount of water (the goal) and find the fastest way to do it.
For example: Two jugs of 3 and 5 liters represented as an array of int [3, 5]. The goal is to have a jug with 4 (represented by an int). The function takes these two elements as parameters and displays the fastest path (if there is one).
The way to get there is (in 6 steps in this case): (0,5) (3,2) (0,2) (2,0) (2,5) (3,4)
As explained before, I solved this simple version with 2 jugs easily. But how to do it with N jugs? Has anyone ever done something similar? Or have an idea?
The only precise operations are
These operations perform, respectively:
amount[A] = capacity[A];
amount[A] = 0;
temp = min(available(B), amount[A]); amount[B] += temp; amount[A] -= temp;
With this special convenience function:
available(i) => capacity[i] - amount[i]
Perform a breadth first search from among these relatively few available operations until the desired amount of water exists in one of the jugs.
There are N choices of indices for operation 1 and 2 (FILL or EMPTY), and there are (N * N-1) choices for operation 3 (POUR), in other words choose a random A and a random B that is not equal to A.
A useful optimization is to prevent enqueuing search states that have already been deemed reachable with fewer steps. A state here is at first glance represented by the values in the amount array, but can also be represented by the number of jugs of each unique capacity and amount, because the jugs themselves are not unique except for their capacity and current amount.