天天看點

【算法】算法之美—Jugs

題目概述: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 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:

    • you can fill a jug,
    • you can empty a jug
    • 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" means "pour 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

簡單描述

   題目意思還是比較簡單,簡單概述一下就是:有無限的水和兩個水桶,一個容量 ca升,一個容量 cb升,問怎麼用兩個桶得到 n升水,要求比較簡單,就不複述了。

題目分析

   簡單分析下此題目:

    1. 兩個水桶 A和B,則需要兩個變量來表示其容量,設為 ca和cb
    2. 目标變量 n
    3. 水在倒的過程中,兩個水桶的目前水量會發生變化,需要兩個變量表示目前量,設為 x和y
    4. 目标量可能由 A得到,也可能由 B得到,是以需要對 A和B進行判斷  

解題算法

#include < stdio.h>
int main()
{
    int ca,cb,n,x,y;
    /* Ca, Cb, and N. Ca and Cb are the capacities of jugs A and B, and N is the goal.
     * 即:ca為 A的容量,cb為 B的容量,N為要得到的水
     * x為 ca目前已有的水
     * y為 cb目前已有的水
     */
    while( scanf("%d %d %d",&ca,&cb,&n)!=EOF)
    {
        x=y=0;
        while(1)
        {
            printf("fill A\n");
            x=ca;
            if(x==n)    /* 如果 A得到目标 */
            {
                printf("success\n");
                break;  /* 成功,退出循環 */
            }
            while(x>0)
            {
                if((cb-y)>=x)    /* B能夠容納的水 > A目前的水 */
                {
                    printf("pour A B\n");   /* 則将 A中的水全部倒入 B中 */
                    y=y+x;  /* y增加 x升水 */
                    x=0;
                }
                else
                {
                    printf("pour A B\n");
                    x=x-(cb-y); /* A中還剩有水 */
                    y=cb;   /* B中裝滿 */
                }
                if(x==n)    /* A得到目标 */
                {
                    printf("success\n");
                    break;
                }
                if(y==n)    /* B得到目标 */
                    break;
                if(y==cb)   /* B空 */
                {
                    printf("empty B\n");
                    y=0;
                }
            }
            if(x==n)
                break;
            if(y==n)
            {
                printf("success\n");
                break;
            }
        }
    }
    return 0;
}      

繼續閱讀