Pots

来源:互联网 发布:微信直接跳转手机淘宝 编辑:程序博客网 时间:2024/06/05 05:48

Pots

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 68   Accepted Submission(s) : 21
Special Judge
Problem Description

You are given two pots, having the volume of A and B liters respectively. The following operations can be performed:

  1. FILL(i)        fill the pot i (1 ≤ ≤ 2) from the tap;
  2. DROP(i)      empty the pot i to the drain;
  3. POUR(i,j)    pour from pot i to pot j; after this operation either the pot j is full (and there may be some water left in the pot i), or the pot i is empty (and all its contents have been moved to the pot j).

Write a program to find the shortest possible sequence of these operations that will yield exactly C liters of water in one of the pots.

 

Input
<p>On the first and only line are the numbers <b>A</b>, <b>B</b>, and <b>C</b>. These are all integers in the range from 1 to 100 and <b>C</b>≤max(<b>A</b>,<b>B</b>).</p>
 

Output
<p>The first line of the output must contain the length of the sequence of operations <b>K</b>. The following <b>K</b> lines must each describe one operation. If there are several sequences of minimal length, output any one of them. If the desired result can’t be achieved, the first and only line of the file must contain the word ‘<b>impossible</b>’.</p>
 

Sample Input
3 5 4
 

Sample Output
6FILL(2)POUR(2,1)DROP(1)POUR(2,1)FILL(2)POUR(2,1)
     题意:给两个水杯,容量a,b,给个数c,让通过6个倒水方法,是某个水杯中水量为c。
     广搜,求最短路径。
     我写的代码蛮长的,但是结构清晰,一看就能够明白,倒水的6个方法依次列出,搜索函数中6次调用,以及每次调用要有一定条件,最重要的一点:标记状态的方式!!!二维数组vis【i】【j】分别代表a b中水量情况!!!

#include<iostream>#include<cstring>using namespace std;struct Pot{    int a;    int b;    int way;    int last;};int A,B,C,flag;Pot p[100010];int vis[110][110];int v[110][110];Pot Fill1(Pot x,int h){    Pot y;    y.a=A;      //a满    y.b=x.b;    y.way=1;    y.last=h;    return y;}Pot Fill2(Pot x,int h){    Pot y;    y.a=x.a;    y.b=B;      //b满    y.way=2;    y.last=h;    return y;}Pot Drop1(Pot x,int h){    Pot y;    y.a=0;      //a空    y.b=x.b;    y.way=3;    y.last=h;    return y;}Pot Drop2(Pot x,int h){    Pot y;    y.a=x.a;    y.b=0;      //b空    y.way=4;    y.last=h;    return y;}Pot Pour1(Pot x,int h){    Pot y;    if((x.a+x.b)<=B)    {        y.a=0;        y.b=x.a+x.b;    }    else    {        y.b=B;        y.a=x.a-(B-x.b);    }    y.way=5;    y.last=h;    return y;}Pot Pour2(Pot x,int h){    Pot y;    if((x.a+x.b)<=A)    {        y.b=0;        y.a=x.a+x.b;    }    else    {        y.a=A;        y.b=x.b-(A-x.a);    }    y.way=6;    y.last=h;    return y;}Pot ioan(){    int head=0,tail=1;    Pot x,y,xx;    x.a=0;    x.b=0;    x.way=0;    x.last=0;    xx=x;    vis[0][0]=1;    p[tail]=x;//入队    do    {        head++;        x=p[head];//出队    if(v[x.a][x.b]==0)    {        v[x.a][x.b]=1;        //cout<<endl<<"head= "<<head<<"    a= "<<x.a<<" b= "<<x.b<<" way= "<<x.way<<endl;;        //1        if(x.a!=A)        {            //cout<<"1 ";            y=Fill1(x,head);            if(y.a ==C||y.b==C)            {                flag=1;                return y;            }            if(vis[y.a][y.b]==0)            {                vis[y.a][y.b]=1;                tail++;                p[tail]=y;            }        }    //2     if(x.b!=B)     {        //cout<<"2 ";        y=Fill2(x,head);        if(y.a==C||y.b==C)        {            flag=1;            return y;        }        if(vis[y.a][y.b]==0)        {            vis[y.a][y.b]=1;            tail++;            p[tail]=y;        }     }   //3    if(x.a!=0)    {        //cout<<"3 ";        y=Drop1(x,head);        if(y.a==C||y.b==C)        {            flag=1;            return y;        }        if(vis[y.a][y.b]==0)        {            vis[y.a][y.b]=1;            tail++;            p[tail]=y;        }    }    //4    if(x.b!=0)    {        //cout<<"4 ";        y=Drop2(x,head);        if(y.a==C||y.b==C)        {            flag=1;            return y;        }        if(vis[y.a][y.b]==0)        {            vis[y.a][y.b]=1;            tail++;            p[tail]=y;        }    }    //5    if(x.a!=0&&x.b!=B)    {        y=Pour1(x,head);        //cout<<"5-";        //cout<<" *"<<y.a<<"*"<<y.b<<"*  ";        if(y.a==C||y.b==C)        {            flag=1;            return y;        }        if(vis[y.a][y.b]==0)        {            vis[y.a][y.b]=1;            tail++;            p[tail]=y;        }    }    //6    if(x.a!=A&&x.b!=0)    {        y=Pour2(x,head);        //cout<<"6+";        //cout<<" ."<<y.a<<"."<<y.b<<". ";        if(y.a==C||y.b==C)        {            flag=1;            return y;        }        if(vis[y.a][y.b]==0)        {            vis[y.a][y.b]=1;            tail++;            p[tail]=y;        }    }    }    }while(head<tail);}int main(){    while(cin>>A>>B>>C)    {        flag=0;        int pp[10010];        Pot y;        memset(v,0,sizeof(v));        memset(vis,0,sizeof(vis));        y=ioan();        if(flag==0)        {            cout<<"NO"<<endl;            continue;        }        int n=0;        int m=y.last;        while(m!=0)        {            pp[n]=y.way;            n++;            y=p[m];            m=y.last;        }        cout<<n<<endl;        for(int i=n-1;i>=0;i--)        {            if(pp[i]==1)                cout<<"FILL(1)"<<endl;            else if(pp[i]==2)                cout<<"FILL(2)"<<endl;            else if(pp[i]==3)                cout<<"DROP(1)"<<endl;            else if(pp[i]==4)                cout<<"DROP(2)"<<endl;            else if(pp[i]==5)                cout<<"POUR(1,2)"<<endl;            else if(pp[i]==6)                cout<<"POUR(2,1)"<<endl;        }    }    return 0;}





原创粉丝点击