内存:128  时间:1

题目描述

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 ≤ i ≤ 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.

输入

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

输出

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’.

样例输入

3 5 4

样例输出

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

提示

代码如下

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n1,m,k;
struct node
{
    int aa;
    int bb;
    int parent;
    int level;
    int ope;
    node(int x,int y,int p,int l,int o):aa(x),bb(y),parent(p),level(l),ope(o){};
    node(){};
};
node nod[100000];
int stk[100000];
int vis[205][205];

void output(int p)
{
    int top=0;
    stk[0]=p;
    while(nod[stk[top]].parent!=-1)
    {
        top++;
        stk[top]=nod[stk[top-1]].parent;
    }
    for(int i=top;i>=0;i--)
    {
        if(nod[stk[i]].ope==0)
            printf("FILL(1)
");
        else if(nod[stk[i]].ope==1)
            printf("FILL(2)
");
        else if(nod[stk[i]].ope==2)
            printf("DROP(1)
");
        else if(nod[stk[i]].ope==3)
            printf("DROP(2)
");
        else if(nod[stk[i]].ope==4)
            printf("POUR(1,2)
");
        else if(nod[stk[i]].ope==5)
            printf("POUR(2,1)
");
    }
}
void f1(node &n)
{
    n.aa=n1;
}
void f2(node &n)
{
    n.bb=m;
}
void f3(node &n)
{
    n.aa=0;
}
void f4(node &n)
{
    n.bb=0;
}
void f5(node &n)
{
    if(m-n.bb<n.aa)
    {
        n.aa=n.aa-(m-n.bb);
        n.bb=m;
    }
    else
    {
        n.bb=n.bb+n.aa;
        n.aa=0;
    }
}
void f6(node &n)
{
    if(n.bb>n1-n.aa)
    {
        n.bb=n.bb-(n1-n.aa);
        n.aa=n1;
    }
    else
    {
        n.aa=n.aa+n.bb;
        n.bb=0;
    }
}
void (*f[6])(node &n)={f1,f2,f3,f4,f5,f6};
int main()
{
    while(scanf("%d%d%d",&n1,&m,&k)!=EOF)
    {
    memset(vis,0,sizeof(vis));
    int res,res_loc=-1;
    int head=0,rear=0;
    int my_cnt=0;
    nod[0]=node(0,0,-1,0,-1);
    vis[0][0]=1;
    while(head<=rear)
    {
        node nd=nod[head];
        if(nd.aa==k||nd.bb==k)
        {
            res=nd.level;
            res_loc=head;
            break;
        }
        for(int i=0;i<6;i++)
        {
            node nt=nd;
            f[i](nt);
            if(vis[nt.aa][nt.bb]==0)
            {
                vis[nt.aa][nt.bb]=1;
                rear++;
                nod[rear]=node(nt.aa,nt.bb,head,nt.level+1,i);
            }
        }
        head++;
    }
         if(res_loc==-1)
        {
            printf("impossible
");
            continue;
        }
        printf("%d
",res);
        output(res_loc);
    }
    return 0;
}

代码来源于互联网,仅供参考!