poj1606 Jugs

来源:互联网 发布:phantomjs pdf python 编辑:程序博客网 时间:2024/05/22 08:09

第一次做bfs+路径的题。。小白一个,代码略复杂。不过个人觉得思路还是很清楚的


原题:

Description

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" 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

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 
题意就是小学高数做过的两个罐子倒水的问题。用搜索来模拟路径。

思路:bfs+路径记录。


源代码:

//By Sean Chen#include <iostream>#include <cstdio>#include <queue>#include <cstring>#include <stdlib.h>using namespace std;struct volum{    int a,b,pre,operate;            //a,b表示罐子内的容量,pre表示前置结点,operate表示进行操作的编号。};volum data[1000001];int visit[1001][1001],ans[1000001];    //visit记录两个罐子的某种状态是否访问过,ans记录可以获得答案的操作步骤int maxa,maxb,target;int main(){    while (scanf("%d%d%d",&maxa,&maxb,&target)!=EOF)    {        //cout<<maxa<<' '<<maxb<<' '<<target<<endl;        volum temp;        temp.a=0;        temp.b=0;        int cnt=0;        temp.pre=-1;                  //至最前端用pre=-1表示        data[cnt]=temp;        int pos=0;        memset(visit,0,sizeof(visit));        memset(ans,0,sizeof(ans));        visit[0][0]=1;        while (pos<=cnt)        {            temp=data[pos];            /*cout<<temp.a<<' '<<temp.b<<' '<<temp.pre<<endl;            system("pause");*/            if (temp.a==target || temp.b==target)                break;            volum temp1;            temp1.pre=pos;temp1.a=temp.a;temp1.b=temp.b;            for (int i=1;i<=6;i++)            {                temp1.operate=i;                switch (i)                {                    case 1:    //fill a                    {                        temp1.a=maxa;                        temp1.b=temp.b;                        if (!visit[temp1.a][temp1.b])                        {                            cnt++;                            data[cnt]=temp1;                        }                        break;                    }                    case 2:    //fill b                    {                        temp1.a=temp.a;                        temp1.b=maxb;                        if (!visit[temp1.a][temp1.b])                        {                            cnt++;                            data[cnt]=temp1;                        }                        break;                    }                    case 3:    //empty a                    {                        temp1.a=0;                        temp1.b=temp.b;                        if (!visit[temp1.a][temp1.b])                        {                            cnt++;                            data[cnt]=temp1;                        }                        break;                    }                    case 4:    //empty b                    {                        temp1.b=0;                        temp1.a=temp.a;                        if (!visit[temp1.a][temp1.b])                        {                            cnt++;                            data[cnt]=temp1;                        }                        break;                    }                    case 5:    //pour a b                    {                        if(temp.a+temp.b>maxb)                        {                            temp1.a=temp.a-(maxb-temp.b);                            temp1.b=maxb;                        }                        else                        {                            temp1.a=0;                            temp1.b=temp.a+temp.b;                        }                        if (!visit[temp1.a][temp1.b])                        {                            cnt++;                            data[cnt]=temp1;                        }                        break;                    }                    case 6:    //pour b a                    {                        if(temp.a+temp.b>maxa)                        {                            temp1.b=temp.b-(maxa-temp.a);                            temp1.a=maxa;                        }                        else                        {                            temp1.b=0;                            temp1.a=temp.a+temp.b;                        }                        if (!visit[temp1.a][temp1.b])                        {                            cnt++;                            data[cnt]=temp1;                        }                        break;                    }                }                visit[temp1.a][temp1.b]=1;            }            pos++;        }        /*cout<<pos<<' '<<cnt<<endl;        system("pause");*/        int i=0;                  //逆向寻找路径        while (pos!=-1)        {            ans[i]=data[pos].operate;            //cout<<ans[i]<<endl;            i++;            pos=data[pos].pre;            /*cout<<pos<<endl;            system("pause");*/        }        for (int j=i-2;j>=0;j--)                 {            switch (ans[j])            {                case 1:                {                    printf("fill A\n");                    break;                }                case 2:                {                    printf("fill B\n");                    break;                }                case 3:                {                    printf("empty A\n");                    break;                }                case 4:                {                    printf("empty B\n");                    break;                }                case 5:                {                    printf("pour A B\n");                    break;                }                case 6:                {                    printf("pour B A\n");                    break;                }            }        }        printf("success\n");    }    return 0;}


0 0