poj 3414 Pots

题目链接:点击打开链接

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

On the first and only line are the numbers AB, and C. These are all integers in the range from 1 to 100 and C≤max(A,B).

Output

The first line of the output must contain the length of the sequence of operations K. The following K 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 ‘impossible’.

Sample Input

3 5 4

Sample Output

6
FILL(2)
POUR(2,1)
DROP(1)
POUR(2,1)
FILL(2)
POUR(2,1)

题目大意:两个水桶,给出两个水桶的容积和经过操作后有一个水桶满足的容积;
操作:1,将水桶装满fill(i);
            2,将水桶清空
            3,将水桶i倒入水桶j中(直到i到完或j到满)
            问两个水桶中的残余两符合给出的体积的最短操作
            输出操作过程,若不能实现输出impossble

基本思路:从起始点(两个水桶都为空),对每个状态进行上面的6种操作,bfs搜索

这个题wa了好几发,最后是因为没有判断桶里水的多少就进行操作了.注意这一点,先判断桶的水,根据情况在进行操作那一步

<span style="font-size:24px;">#include <iostream>
#include<cstring>
#include<cstdio>

using namespace std;
int A,B,C;
struct node
{
    int a,b;
    int id;
    int pre;
} q[999999],t;
int vis[200][200];
int bfs(int x,int y)
{
    int in,out;
    in=out=0;
    q[in].a=x;
    q[in].b=y;
    q[in].id=-1;
    q[in++].pre=-1;
    int d;
    while(in>out)
    {
        //cout<<out<<"&&"<<in<<endl;
        t=q[out];
        d=out++;
        //cout<<t.a<<" "<<t.b<<endl;
        if(t.a==C||t.b==C)
        {
            //cout<<"##\n";
            return d;
        }
        if(t.a<A&&vis[A][t.b]==0)
        {
            vis[A][t.b]=1;
            q[in].a=A;
            q[in].b=t.b;
            q[in].id=1;
            q[in++].pre=d;
        }
        if(t.b<B&&vis[t.a][B]==0)
        {
            vis[t.a][B]=1;
            q[in].a=t.a;
            q[in].b=B;
            q[in].id=2;
            q[in++].pre=d;
        }
        if(t.a>0&&vis[0][t.b]==0)
        {
            vis[0][t.b]=1;
            q[in].a=0;
            q[in].b=t.b;
            q[in].id=3;
            q[in++].pre=d;
        }
        if(t.b>0&&vis[t.a][0]==0)
        {
            vis[t.a][0]=1;
            q[in].a=t.a;
            q[in].b=0;
            q[in].id=4;
            q[in++].pre=d;
        }
        if(t.a>0&&t.b>=0&&t.b<B)
        {
            if(t.a+t.b<=B&&vis[0][t.a+t.b]==0)
            {
                vis[0][t.a+t.b]=1;
                q[in].a=0;
                q[in].b=t.a+t.b;
                q[in].id=5;
                q[in++].pre=d;
            }
            else if(vis[t.a-(B-t.b)][B]==0)
            {
                vis[t.a-(B-t.b)][B]=1;
                q[in].a=t.a-(B-t.b);
                q[in].b=B;
                q[in].id=5;
                q[in++].pre=d;
            }
        }
        if(t.a>=0&&t.b>0&&t.a<A)
        {
            if(t.a+t.b<=A&&vis[t.a+t.b][0]==0)
            {
                vis[t.a+t.b][0]=1;
                q[in].a=t.a+t.b;
                q[in].b=0;
                q[in].id=6;
                q[in++].pre=d;
            }
            else if(vis[A][t.b-(A-t.a)]==0)
            {
                vis[A][t.b-(A-t.a)]=1;
                q[in].a=A;
                q[in].b=t.b-(A-t.a);
                q[in].id=6;
                q[in++].pre=d;
            }
        }
    }
    return -1;
}
int main()
{
    while(cin>>A>>B>>C&&C)
    {
        memset(vis,0,sizeof(vis));
        vis[0][0]=1;
        int d=bfs(0,0);
        char str[10000][10];
        //memset(str,'\0',sizeof(str));
        int top=0;
        int sum=0;
        if(d==-1)cout<<"impossible\n";
        else
        {
            while(1)
            {
                sum++;
                if(q[d].id==1)
                {
                    strcpy(str[top],"FILL(1) ");
                }
                else if(q[d].id==2)
                {
                    strcpy(str[top],"FILL(2)");
                }
                else if(q[d].id==3)
                {
                    strcpy(str[top],"DROP(1)");
                }
                else if(q[d].id==4)
                {
                    strcpy(str[top],"DROP(2)");
                }
                else if(q[d].id==5)
                {
                    strcpy(str[top],"POUR(1,2)");
                }
                else if(q[d].id==6)
                {
                    strcpy(str[top],"POUR(2,1)");
                }
                top++;
                d=q[d].pre;
                if(d==0)break;
            }
            cout<<sum<<endl;
            for(int i=top-1; i>=0; i--)
            {
                cout<<str[i]<<endl;
            }
        }
    }
    return 0;
}</span>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值