Pots

Pots

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 22062   Accepted: 9389   Special Judge

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)

Source

Northeastern Europe 2002, Western Subregion

#include <iostream>
#include <cstring>
using namespace std;
int a,b,c;
int vis[1123][1123];
int flag;
int id[12345];
struct node
{
    int k1,k2;//水的状态
    int step;//步数
    int op;//当前操作
    int pre;//记录前一步的编号
    //int nre;//记录当前编号
}q[112345];
int lastnumber;//最后一步的编号
int finalstep;//最终的步数
void bfs()
{
    memset(vis,0,sizeof(vis));
    int head=0,tail=0;
    vis[0][0]=1;
    q[tail].k1=0,q[tail].k2=0;
    q[tail].op=0,q[tail].pre=0,q[tail].step=0;
    tail++;
    while(head<tail)
    {
        struct node now;
        now=q[head];
        head++;
        if(now.k1==c||now.k2==c)
        {
            lastnumber=head-1;
            finalstep=now.step;
            flag=1;
            return ;
        }
        for(int i=1;i<=6;i++)
        {
            struct node next;
            if(i==1)//fill(1)
            {
                next.k1=a;
                next.k2=now.k2;
            }
            if(i==2)//fill(2)
            {
                next.k1=now.k1;
                next.k2=b;
            }
            if(i==3)//drop(1)
            {
                next.k1=0;
                next.k2=now.k2;
            }
            if(i==4)//drop(2)
            {
                next.k1=now.k1;
                next.k2=0;
            }
            if(i==5)//pour(1,2)
            {
                if(now.k1+now.k2<=b)
                {
                    next.k1=0;
                    next.k2=now.k1+now.k2;
                }
                else
                {
                    next.k1=now.k1+now.k2-b;
                    next.k2=b;
                }
            }
            if(i==6)
            {
                if(now.k2+now.k1<=a)
                {
                    next.k1=now.k1+now.k2;
                    next.k2=0;
                }
                else
                {
                    next.k1=a;
                    next.k2=now.k2+now.k1-a;
                }
            }
            next.op=i;
            if(!vis[next.k1][next.k2])
            {
                vis[next.k1][next.k2]=1;
                next.step=now.step+1;
                next.pre=head-1;
                q[tail++]=next;
                if(next.k1==c||next.k2==c)
                {
                    lastnumber=tail-1;
                    finalstep=next.step;
                    flag=1;
                    return ;
                }
            }
        }
    }
}
int main()
{
    while(cin>>a>>b>>c)
    {
        flag=0;
        bfs();
        if(flag==1)
        {
            cout<<finalstep<<endl;
            id[finalstep]=lastnumber;
            for(int i=finalstep-1;i>=1;i--)
            {
                id[i]=q[id[i+1]].pre;//当前的编号在下一步的pre中存储着
            }
            for(int i=1;i<=finalstep;i++)
            {
                if(q[id[i]].op==1)
                    cout<<"FILL(1)"<<endl;
                if(q[id[i]].op==2)
                    cout<<"FILL(2)"<<endl;
                if(q[id[i]].op==3)
                    cout<<"DROP(1)"<<endl;
                if(q[id[i]].op==4)
                    cout<<"DROP(2)"<<endl;
                if(q[id[i]].op==5)
                    cout<<"POUR(1,2)"<<endl;
                if(q[id[i]].op==6)
                    cout<<"POUR(2,1)"<<endl;
            }
        }
        else
            cout<<"impossible"<<endl;
    }
    return 0;
}

 

posted @ 2018-08-01 09:15  hum0r0  阅读(13)  评论(0)    收藏  举报