Uva oj 571-jugs (jar)

Source: Internet
Author: User

Time Limit: 3.000 seconds
Time Limit: 3.000 seconds

 

Background
Background

In the movie "Die Hard 3", Bruce Willis and Samuel L. jackson were confronted with the following puzzle. they were given a 3-gallon jug and a 5-gallon jug and were asked to fill the 5-gallon jug with exactly 4 gallons. this problem generalizes that puzzle.
In the film gentian huwei, Bruce wlis and Samuel Jackson face a challenge. They got two jars, 3 Galen and 5 Galen respectively, and asked to load 4 Galen exactly in the 5 Galen jar. This problem arises.

you have two jugs, A and B, and an infinite supply of water. there are three types of actions that you can use: (1) You can fill a jug, (2) you can empty a jug, and (3) you can pour from one jug to the other. pouring from one jug to the other stops when the first jug is empty or the second jug is full, whichever comes first. for example, if a has 5 gallons and B has 6 gallons and a capacity of 8, Then pouring from A to B leaves B full and 3 gallons in.
now you can have two jars A and B with unlimited water. You can only do three things: (1) Full jar, (2) Empty Jar, (3) pour water from one jar into another. When pouring water between two containers, it is necessary to wait until the inverted jar is emptied, or the poured jar is full. For example, if a has 5 galons of water, B has a capacity of 8 galons and 6 galons of water. After a is switched to B, 3 galons are left in.

A problem is given by a triple (Ca, CB, n), where Ca and CB are the capacities of the jugs A and B, respectively, and N is the goal. A solution is a sequence of steps that leaves EXACTLY n gallons in jug B. the possible steps are
This problem is given a triple (Ca, CB, n), where Ca and CB are the capacity of both A and B, and N is the target water volume. The solution involves a series of steps to obtain n galun water in tank B. The steps are as follows:

    • Fill
    • Fill B
    • Empty
    • Empty B
    • Pour a B
    • Pour B
    • Success

Where "Pour a B" means "Pour the contents of jug a into jug B", and "success" means that the goal has been accomplished.
"Pour a B" indicates that the water in tank A is poured into tank B. "Success" indicates that the target has been reached.

You may assume that the input you are given does have a solution.
You can think that all the given input has a solution.

 

Input
Input

Input to your program consists of a series of input lines each defining one puzzle. input for each puzzle is a single line of three positive integers: Ca, CB, and N. CA and CB are the capacities of jugs A and B, and N is the goal. you can assume 0 <ca ≤ CB and N ≤ CB ≤ 1000 and that A and B are relatively prime to one another.
InputProgramThe data of is composed of multiple rows. each row of data defines a question. Each row includes three integers: Ca, CB, and N. CA and CB are the capacity of tank A and tank B respectively, and N is the target water volume. You can think that 0 <ca ≤ CB and N ≤ CB ≤ 1000, A and B are mutually qualitative.

 

Output
Output

Output from your program will consist of a series of instructions from the list of the potential output lines which will result in either of the jugs containing EXACTLY n gallons of water. the last line of output for each puzzle shoshould be the line "success ". output lines start in column 1 and there shoshould be no empty lines nor any trailing spaces.
Your program should enter a set of step commands defined above so that N Galen's water can be loaded accurately at last, no matter which jar. The row at the top of the output line should be "success ". The output starts from the first column. If the first column cannot be empty, there cannot be spaces at the end of the row.

 

Sample Input
Input example

3 5 4
5 7 3

 

Sample output
Output example

Fill B
Pour B
Empty
Pour B
Fill B
Pour B
Success
Fill
Pour a B
Fill
Pour a B
Empty B
Pour a B
Success

 

Analysis
Analysis

This question only requires a solution, but does not require an optimal solution. The two containers must have different capacities. Therefore, an integer from 0 to the capacity of a large tank can be displayed. The principle is that if A and B are mutually qualitative, the minimum public factor is a × B, that is, there is no 1 <n <B, so that Na can be divisible by B. So r = Na mod B (mod is the modulo operation), then when n changes from 0 to B-1, R can take any value between 0 to B-1. This is easy to prove, but my proof process is very unprofessional. Please give your advice!

Now the problem is to use the AB two cans to pour water into each other to simulate this mathematical process. Set the small tank capacity to A, and the large tank capacity to B. Each time a is filled with A, it is poured into B. If B is full after the MTH, and the remaining water volume of A is R1, the following are available:

    • R1 = ma mod B

Then, empty B and load the remaining R1 in A to B. After n times of filling a into B, B is filled. When the remaining water volume of A is R2, there are:

    • R2 = (R1 + Na) mod B
    • = (MA mod B + Na) mod B
    • = (M + n) A mod B

We can see that the inverted method is to pour the water in a into B each time, but the water in A and B needs to be checked before this step. If a is empty, fill a. If B is full, clear B. In the end, you can obtain the required water volume in the tank B at a certain time.

Solution
Answer
 
 # include 
   
     # include 
    
      using namespace STD; int main (void) {// cyclically process each line of input data for (INT nacap, nbcap, ndest; CIN> nacap> nbcap> ndest ;) {// start the 
      algorithm  from A to B, and each output action for (INT NA = 0, NB = 0; NB! = Ndest; cout <"Pour a B \ n") {// If tank A is empty, it is filled with tank A and the action if (NA = 0) is output) {Na = nacap; cout <"fill a \ n" ;}// if tank B is full, empty tank B and output action if (NB = nbcap) {NB = 0; cout <"Empty B \ n";} // enter all the cans into the B can Nb + = Na; NA = 0; // If an excess of tank B is found, the excess part will be reversed to the IF (NB> nbcap) {Na = Nb-nbcap; NB = nbcap ;}} cout <"success" 
      
     
    
   

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.