Algorithm title: UVA 571 (number theory)

Source: Internet
Author: User

Jugs

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. This is problem generalizes that puzzle.

You have two jugs, A and B, and A infinite supply of water. There are three types of actions that can be use: (1) can fill a jug, (2) 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 "the" is empty or the second jug are full, whichever comes. 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 gall ONS in A.

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 are T He goal. A solution is a sequence of the steps that leaves exactly N gallons in jug B. The possible steps are

Fill A

Fill B

Empty A

Empty B

Pour A B

Pour B A

Success

where ' pour A B ' means ' pour the contents of jug A into jug B ', and ' success ' means that goal has been .

You may assume this input you are given does have a solution.

Input

Input to your program consists of the a series of input lines each defining one puzzle. Input for each puzzle are a single line of three positive INTEGERS:CA, Cb, and N. Ca and CB are the capacities of jugs a nd B, and Nis the goal. You can assume $ < Ca le cb$ and $N le Cb le 1000$ and that's A and B are 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.

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

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.