This problem is found here: http://learningandtheadolescentmind.org/resources_02_bucket.html
I called it three "Ice Bucket Challenge" for fun. ;) Here is the problem description.
There are an 8-liter bucket filled with ice water and empty 3-liter and
5-liter buckets. You solve the puzzle by using the three buckets to
divide the 8 liters of water into two equal parts of 4 liters. Try to solve the
puzzle in the fewest number of moves.
Although to find a fewest number of moves is not easy, but I can design depth first search to find the necessary moves to distribute the waters.
Here is the code
The idea here is to for each possible case, we find the all possible cases which can be derived from it. For example, from 8, 0, 0, it could be 5, 0, 3 or 3, 5, 0. And we use a list keep track of the cases we already visited. Think of all possible cases as a graph, we essentially do a depth first search until we find the case contains 4.
A potential solution to find the fewest moves is to use the breath first search. We can use a Queue to contain the list of cases we already visited, then generate the queue of next level based on that, also keep track of the parent case for each case in the queue until we find the case.
It's also called waterjug problem, a classical AI problem. One thing we need to know is that it is necessary to calculate gcd of 3 numbers to see if it is divisible by 4 to ensure it has an answer.
ReplyDeletegcd(3,5,8) is 1 is not divisible by 4. i guess you mean the other way around.
DeleteCorrect
Delete