UVA 571 Jugs

Source: Internet
Author: User
Tags greatest common divisor

Original question:
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 is problem generalizes that puzzle.

You have both jugs, A and B, and an infinite supply of water. There is three types of actions that you can use: (1) can fill a jug, (2) You can empty a jug, and (3) can pour F Rom one jug to the other. Pouring from one jug to the other stops when the first jug is empty or the second jug are full, whichever comes first. For example, if A have 5 gallons and B have 6 gallons and A capacity of 8, then pouring from A to B leaves B full and 3 gall ONS in A.

A problem is given by a triple (ca,cb,n), where Ca and Cb was the capacities of the jugs A and B, respectively, and N is T He goal. A solution is a sequence of steps this leaves exactly N gallons in jug B. The possible steps is

Fill A
Fill B
Empty A
Empty B
Pour A B
Pour B A
Success
Where pour a B "meanspour the contents of jug A into jug B", and "success" means that the goal have been accomplished.

Assume that the input is given does has a solution.

Input

Input to your program consists of a series of the input lines each defining one puzzle. Input for each puzzle was a single line of three positive INTEGERS:CA, CB, and N. Ca and CB are the capacities of jugs a nd B, and N is the goal. You can assume 0 < ca< CB and N < CB <1000 and that A and B is relatively prime to one another.
Output

Output from your program would consist of a series of instructions from the list of the potential output lines which would r Esult in either of the jugs containing exactly N gallons of water. The last line of output for each puzzle should is the line "success". Output lines start in column 1 and there should is no empty lines nor any trailing spaces.
Sample Input

3 5 4
5 7 3
Sample Output

Fill B
Pour B A
Empty A
Pour B A
Fill B
Pour B A
Success
Fill A
Pour A B
Fill A
Pour A B
Empty B
Pour A B
Success
Effect:
Give you two pots, a, a, and a target water c. Now let you pour out the target water with a, B and infinite water. Where a, b coprime.

#include <iostream> #include <algorithm> #include <map> #include <string> #include <cstring > #include <sstream> #include <cstdio> #include <vector> #include <cmath> #include <stack > #include <queue> #include <iomanip> #include <set> #include <fstream> #include <climits
> Using namespace std;


FStream input,output;
    int main () {Ios::sync_with_stdio (false);
    int Ja,jb,aim;
    int A, B; while (Cin>>ja>>jb>>aim) {if (Ja==aim) {cout<< "Fill A" <<endl
            ;
            cout<< "Success" <<endl;
        Continue
            } if (Jb==aim) {cout<< "Fill B" <<endl;
            cout<< "Success" <<endl;
        Continue
        } a=b=0;
                while (B!=aim) {if (a==0) {cout<< "fill a" <<endl;
            A=ja;} if (B&LT;JB) {cout<< "pour A B" <<endl;
                    if (B+JA&GT;JB) {a=ja-(jb-b);
                B=JB;
                    } else {b+=a;
                a=0;
                }} if (B==JB) {cout<< "Empty B" <<endl;
                b=0;
                cout<< "pour A B" <<endl;
                B=a;
            a=0;
    }} cout<< "Success" <<endl;
}//Input.close ();
    Output.close ();
return 0;




 }

Answer:
A very classical problem, just read the question when I thought let me decide whether to pour out the target water, the result is to let me ask how to operate.
First of all, the number of any given two, a, B can achieve the operation of the target C. Now suppose B is greater than a, first let the water in a fill in B until the water in B is filled. At this point, the remaining water in a is X, and then the water in B is emptied, the x is loaded into B and repeated to find that it is a classic operation-a more subtractive method. Is the method of seeking greatest common divisor. So as long as the number of C can be apart from the greatest common divisor of a and B can be able to pour out the target water, but also require less than B target water.

After reading the topic tangled up a long time how to handle the minimum operating procedures, set A to B in the water operation X,b to a in the water is Y. Can get, ax+by=1 such an equation, and then use the extended Euclidean formula to find a set of X, Y solution, and then to obtain the smallest x or y ....
Later can not think of the program how to write, look at other people's solution actually found any group of solutions to .... CNM.

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.