#P1432. 倒水问题
倒水问题
Description
Assume two jugs and , with unlimited water supply. You can use three kinds of operations:
- Fill a jug.
- Empty a jug.
- Pour water from one jug into the other.
When pouring from one jug into the other, you must stop when either the first jug becomes empty or the second jug becomes full. For example, if jug has capacity gallons and jug has capacity gallons, and the amount of water is gallons, then when pouring from jug into jug , jug becomes full and jug has gallons left.
The problem has parameters: , , and , which are the capacities of jugs and and the target amount , respectively. The goal is to output a sequence of pouring steps so that the amount of water in jug is exactly .
If there are multiple valid sequences, output any one with the minimum number of operations.
Input Format
The first line contains the number of test cases .
The next lines each contain three numbers , , and , as described above.
Constraints: , , , and and are coprime.
Output Format
Print lines. For each test case, first output the number of operations (a solution is guaranteed to exist). Then output integers, each representing an operation:
- Operation 1:
fill Ameans fill to full. - Operation 2:
fill B. - Operation 3:
empty Ameans empty . - Operation 4:
empty B. - Operation 5:
pour B Ameans pour from into (until is full or is empty). - Operation 6:
pour A B.
2
3 5 4
5 7 3
6 2 5 3 5 2 5
6 1 6 1 6 4 6
1
26 29 11
22 1 6 1 6 4 6 1 6 4 6 1 6 4 6 1 6 4 6 1 6 4 6
Hint
SPJ is enabled.
If your sequence is better than the official answer, it will show UKE. In this case, please contact an administrator to update the testdata.
If your sequence is worse than the official answer, your score will be reduced accordingly.
Translated by ChatGPT 5
京公网安备 11011102002149号