POJ 1606 Jugs

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

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和数论推导。我只想到了BFS,由上一状态推导出6个可能的状态,成广搜树结构。一开始老是RuntimeError,
后来看了别人的代码发现是因为没有加标记数组,用过的状态不用再加到队列中了。
标记数组很重要啊,我还以为这道题用不到呢,仔细想想如果不标记很可能会有大量的重复状态加入到队列中,导致数组越界。
题目要求输出路径,容易想到栈。用一个数pre记录他的上一状态,再用一个数opt记录达到这个状态的操作。
因为front是上一状态在队列中的序号,rear是本状态的序号,所以对这两个数的++操作要谨慎啊,挺容易出错的。
用自己编的数组假栈和假队列0msA了,比STL快啊。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
struct Node
{
    int a,b;
    int pre;
    int opt;
}Q[20000],st,head,cur;

char opt[6][10]={"fill A","fill B","empty A","empty B","pour A B","pour B A"};

int A,B,Goal;
int S[10000];
bool vis[1001][1001];

void Output(int ans)
{
    int tmp=ans;
    int size=0;
    while(Q[tmp].pre!=0)
    {
        S[++size]=tmp;
        tmp=Q[tmp].pre;
    }
    for(int i=size;i>0;i--)
    {
        int t;
        t=S[i];
        t=Q[t].opt;
        printf("%s\n",opt[t]);
    }
    printf("success\n");
    return ;
}
void  BFS()
{
    int front=1;
    int rear=1;
    Q[1]=st;
    while(true)
    {
        head=Q[front];

        for(int i=0;i<6;++i)
        {
            cur.pre=front;
            cur.opt=i;
            if(i==0)   //fill  A
            {
                cur.a=A;
                cur.b=head.b;
            }
            else  if(i==1)  //fill  B
            {
                cur.a=head.a;
                cur.b=B;
            }
            else if(i==2)  //empty  A
            {
                cur.a=0;
                cur.b=head.b;
            }
            else if(i==3)   //empty  B
            {
                cur.a=head.a;
                cur.b=0;
            }
            else if(i==4)  //pour A B
            {
                int tmp=B-head.b;
                if(head.a<=tmp)
                {
                    cur.a=0;
                    cur.b=head.a+head.b;
                }
                else
                {
                    cur.a=(head.a+head.b-B);
                    cur.b=B;
                }
            }
            else            //pour  B A
            {
                if(head.b<=(A-head.a))
                {
                    cur.a=head.a+head.b;
                    cur.b=0;
                }
                else
                {
                    cur.a=A;
                    cur.b=(head.a+head.b-A);
                }
            }

            if(!vis[cur.a][cur.b])
            {
                vis[cur.a][cur.b]=true;
                Q[++rear]=cur;
                if(cur.b==Goal)
                {
                    Output(rear);
                    return ;
                }
            }
        }
        ++front;// 这地方调试了好久,只能在这++front
    }
}

int main()
{
    st.a=0;st.b=0;
    st.pre=0;
    while(scanf("%d %d %d",&A,&B,&Goal)!=EOF)
    {
        memset(vis,0,sizeof(vis));
        BFS();
    }
}

数论推导的代码我不太明白,先贴上,以后仔细学学数论再来看。

#include <stdio.h>
int main()
{
 int A,B,C,tmp;
 while(~scanf("%d %d %d",&A,&B,&C)){
     for(tmp=0;tmp!=C;){
         puts("fill A");
         puts("pour A B");
         if((tmp+=A)>B){
             tmp-=B;
             puts("empty B");
             puts("pour A B");
         }
     }
     puts("success");
 }
 return 0;
}

完工!

原文地址:https://www.cnblogs.com/liuyalunuli/p/2703711.html