uva 571 Jugs

來源:互聯網
上載者:User

原題:
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.

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 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 is the goal. A solution is a sequence of 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" meanspour the contents of jug A into jug B”, and “success” means that the goal has been accomplished.

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

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.
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 should be the line “success”. Output lines start in column 1 and there should be 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
大意:
給你兩個壺a,b還有一個目標水量c。現在讓你用a,b和無限的水倒出目標水量。其中a,b互質。

#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<jb)            {                cout<<"pour A B"<<endl;                if(b+ja>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;}

解答:
很經典的一個問題,剛讀題的時候還以為讓我判斷能否倒出目標水量來,結果卻是讓我求操作方法。
首先先說明一下對任意給定的兩個數a,b能否實現操作出目標c。現在假設b大於a,首先讓a裡的水裝滿不斷倒進b中,直到b中的水裝滿。此時a裡剩下的水是x,再把b裡的水倒空,把x裝進b中,不斷重複就能發現這是一個經典的操作——更相減損的方法。也就是求最大公約數的方法。所以只要c的數量能夠除開a與b的最大公約數就能夠倒出目標水量,同時要求目標水量要小於b。

讀完題後糾結了好長時間如何處理最少操作步驟,設a往b的裡裝水操作x,b往a裡裝水是y。則能得到,ax+by=1這樣一個方程,再利用擴充歐幾裡得公式求出一組x,y的解,然後求得最小的x或者y….
後來想不出程式該怎麼寫了,看別人的題解居然發現任意一組解就可以….CNM的。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.