Algorithm series (ii) three bucket water problem

Source: Internet
Author: User

There is a 8-liter bucket filled with water, in addition to a volume of 3 liters of empty barrels and a volume of 5 liters of empty barrels, how to use these two empty barrels equal to 8 liters of water? The additional condition is that three buckets have no volume scale and can not use other auxiliary containers.

This is a classic topic, the average person can give the answer in a minute, but many people may not notice that the answer to this question is not unique. Let's take a look at one of the most common answers, which is the fastest known operation, with a total of 7 inverted water actions:

Pour 5 liters of water from a 8-liter barrel into a barrel with a volume of 5 liters.

Pour 3 liters of water from a 5-liter barrel into a barrel with a volume of 3 liters.

Pour 3 liters of water from a 3-liter barrel into a barrel with a volume of 8 liters.

Pour 2 liters of water from a 5-liter barrel into a barrel with a volume of 3 liters.

Pour 5 liters of water from a 8-liter barrel into a barrel with a volume of 5 liters.

Pour 1 liters of water from a 5-liter barrel into a barrel with a volume of 3 liters.

Pour 3 liters of water from a 5-liter barrel into a barrel with a volume of 8 liters.

< end >

Here's a slightly more complicated answer, which requires a 8-time inverted water action:

Pour 3 liters of water from a 8-liter barrel into a barrel with a volume of 3 liters.

Pour 3 liters of water from a 3-liter barrel into a barrel with a volume of 5 liters.

Pour 3 liters of water from a 8-liter barrel into a barrel with a volume of 3 liters.

Pour 2 liters of water from a 3-liter barrel into a barrel with a volume of 5 liters.

Pour 5 liters of water from a 5-liter barrel into a barrel with a volume of 8 liters.

Pour 1 liters of water from a 3-liter barrel into a barrel with a volume of 5 liters.

Pour 3 liters of water from a 8-liter barrel into a barrel with a volume of 3 liters.

Pour 3 liters of water from a 3-liter barrel into a barrel with a volume of 5 liters.

< end >

How many kinds of answers do you have? Here first sell a thought, patiently read this article you will know.

The idea of solving problems

If the way of thinking of personnel, so the key to solve this problem is how to set up a certain 1 liters of water or can accommodate 1 liters of water space, the three bucket volume is 3, 5 and 8, with these three numbers to do addition and subtraction operations, can get many groups of answers, such as:

3– (5-3) = 1

This strategy corresponds to the first workaround mentioned above, while the other set of operations:

(3 + 3)-5 = 1

Corresponds to the second solution mentioned above.

But the computer does not understand the importance of this "1", it is difficult to follow the human way of thinking to deduce the answer step-by-step, so using computers to solve this problem, usually choose to use the "exhaustive method." Why use the exhaustive method? Because this is not a typical problem of solving the optimal solution, although it can imply a method to solve the least number of inverted water, but in essence, the most efficient method for solving the problem of optimal solution is not applicable to this problem. If we can solve all the legal solutions of space, and then find the optimal solution by comparison, it is also a method for solving the optimal solution. However, as far as the subject is concerned, I do not care about the quickest way to find out the whole method of dividing water may be more in line with the requirements.

If we put the volume of water in three buckets in a single moment as a state, the initial state of the problem is 8 liters of water, the solution state (final state) is 8 liter bucket 4 liters of water, 5 liters bucket 4 liters of water. The essence of the exhaustive method is to search all possible states from the initial state, according to the rules of a state change, and to find an answer whenever a path of change from the initial state to the final state is found. The result of this state change search is usually to get a state search tree, the root node is the initial state, the leaf node may be the final state, or some can not be converted to the final state of the middle State, the state tree has a number of final state of the leaf node, there are many answers. Based on the above analysis results, the key to solve the problem of the algorithm is three points: first, the state model of the algorithm is established, secondly, the search algorithm of the state tree (implied state transition rule) is set up, and finally, some means to improve the efficiency of the algorithm, such as applying the "pruning" condition to avoid duplicate state search, and avoiding the state Loop generation causes the search algorithm to loop infinitely between several states.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.