General Solution to the Problem of using cup water

Source: Internet
Author: User

Some friends in the garden mentioned this problem. I believe you have all seen this problem, and sometimes you will encounter it during interviews.
There are many ways to solve the problem. Some people rely on guesses, poor lifting, and reverse pushing. In the case of a lot of water pouring times, it will be quite troublesome.
Earlier in the year, I came up with a general and simple method for solving such questions.
Take a 5-litre, 6-litre, 6-litre cup, and 3-litre water as an example. The two cups are in 22 States according to the amount of water they are loaded, the process of water pouring is the process of switching between these States. The two cups are empty and are in the initial state. The water in one of the two cups is 3 liters, which is the final state, therefore, this problem can be solved using a finite state machine. For the sake of intuition, these statuses are distributed in a two-dimensional table based on the water volume of the two cups, and the following figure is obtained:


Figure 1

In the figure, the horizontal coordinates indicate the water volume of one cup (A), the vertical coordinates indicate the water volume of another cup (B), the gray boxes indicate the State that does not exist, and the yellow boxes indicate the initial state, the blue box indicates the final state.
The following describes how to switch between these statuses. First, you can fill a cup with water. In the figure, it indicates that the gray area is changed from the zero state to the full state on the opposite side. Filling the B cup is from top to bottom, 2, fill the cup a with three values from left to right.


Figure 2 Figure 3

If you leave a cup empty, the direction is the opposite, as shown in the figure below.

If you pour water from a cup to another cup, you can use the 45-degree line from the original state to the lower left or upper right. The target State indicates the amount to be poured and the remaining amount to the original cup. 4 indicates that the water in the cup containing 4l water is poured into the B cup. After the B cup is filled with 4l water, A is blank. Figure 5 indicates that the cup is full, and the B cup is 1l. After the cup is poured into the B cup, the B cup is full, and the cup has 2L left. Figure 6 indicates that a cup is 1l and B is full. After B is poured into a cup, A is full and B is empty. These are permitted status changes. The principle of change is that the total amount of water remains unchanged only in the 45 degrees direction in the lower left to the upper right.


Figure 4 Figure 5 Figure 6

Now, several allowed changes are obtained to see if the initial state can reach the final state along such a line.
There are two options at the beginning. First, try to the right. Figure 7 is displayed.

 


Figure 7

There are only two options: Bottom left or bottom, and full down. The next operation can only empty a cup, which makes no sense. Therefore, there is only one option, bottom left, to (1, 5), obtain figure 8.


Figure 8

The following two options are available: Obviously, it is meaningless to go back and to the right, so there is only one option, that is, to go up to (1, 0) and get figure 9.


Figure 9

Similarly, if the destination to the right has passed, the left bottom is shown in figure 10.


Figure 10

In the end, Figure 11 is displayed. Reached the final state.


Figure 11

The solution here is:
1. aplus full (6, 0)
2. A inverted B (1, 5)
3. Empty B (1, 0)
4. A inverted B (0, 1)
5. Fill a (6, 1)
6. A inverted B (2, 5)
7. Empty B (2, 0)
8. A inverted B (0, 2)
9. aplus full (6, 2)
10. A inverted B (3, 5) (get 3l)
If B is first filled with B at the beginning, the result is similar.
If a status is reached, the next step cannot be found, or all the next steps are returned to the previously reached status, there is no solution.

In the case of two cups, there is basically no branch to be selected except the first step.
In the case of multiple cups, the status is represented by a multi-dimensional array, and the available branches also increase with the increase of dimensions to form a search tree. You can further explore it on your own.

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.