pots

   Pots
Time Limit: 1000MS     Memory Limit: 65536KB     64bit IO Format: %I64d & %I64u

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 ≤ 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 potj is full (and there may be some water left in the pot i), or the poti 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 exactlyC liters of water in one of the pots.

Input

On the first and only line are the numbers A, B, andC. 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 operationsK. 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)
 
   
题意:

给出两个容积分别为 a 和 b 的罐子,按照以下三种操作方式,求出能否在一定步数后,使者两个罐子的其中一个的水量为c。

      1.FILL(i):将i罐子倒满水。

      2.DROP(i):将i罐子倒空水。

      3.POUR(i,j): 将i罐子的水倒到j罐子里,直至要么i罐子为空,要么j罐子为满。

让你写出最少的步骤数能使其中一个罐子的水达到c,并写出具体步骤。如果不能则输出‘impossible’。

code:

#include<iostream>
#include<algorithm>
#include<string.h>
#include<queue>
#include<cstdio>
using namespace std;
struct Element
{
       int x,y,r;
       Element(int n,int m,int s):x(n),y(m),r(s){}
       Element(){}
};
bool visit[105][105];//记录出现过的情况,避免重复步骤。
Element que[1000005];
int pre[1000005]; //记录这次操作的前一次操作。
int a,b,c;
void op(int&first,int&rear,int x,int y,int r)
//r代表操作,将a倒满==1,将b倒满==2;将a清空==3;将b清空 == 4;将a倒到b==5;将b倒到a==6
{
     if( !visit[x][y]){
         que[rear]=Element(x,y,r);
         pre[rear++]=first;
         visit[x][y]=true;
     }
}


int BFS()
{
    int first,rear;
    Element front;
    first=rear=0;
    memset(que,0,sizeof(que));
    memset(pre,-1,sizeof(pre));
    memset(visit,false,sizeof(visit));
    visit[0][0]=true;
    que[rear++]=Element(0,0,0);
    while( first!=rear)
    {
           front=que[first];


           if( front.x==c||front.y==c)
               return first;


           if( front.x!=a)         //1
               op(first,rear,a,front.y,1);


           if( front.y!=b)         //2
             op(first,rear,front.x,b,2);


           if( front.x!=0)           //3
               op(first,rear,0,front.y,3);


           if( front.y!=0)            //4
                op(first,rear,front.x,0,4);


           if( front.x!=0&&front.y!=b)   //5
           {
               if( front.x<=b-front.y)
                   op(first,rear,0,front.x+front.y,5);
               else
                    op(first,rear,front.x+front.y-b,b,5);
           }


           if( front.y!=0&&front.x!=a)  //6
           {
               if( front.y<=a-front.x)
                   op(first,rear,front.x+front.y,0,6);
               else
                    op(first,rear,a,front.x+front.y-a,6);
           }
           first++;


    }
    return -1;
}


void print(int j)
{
     int ans[1000],i,k;
     for( i=j,k=0; pre[i]!=-1; i=pre[i])
     {
          ans[k++]=que[i].r;
     }
     printf("%d\n",k);
     for( i=k-1; i>=0; i--)
     {
          switch( ans[i])
          {
                  case 1: printf("FILL(1)\n"); break;
                  case 2: printf("FILL(2)\n"); break;
                  case 3: printf("DROP(1)\n"); break;
                  case 4: printf("DROP(2)\n"); break;
                  case 5: printf("POUR(1,2)\n"); break;
                  case 6: printf("POUR(2,1)\n"); break;
                  default: ;
          }
     }
}
int main()
{
    int flag;
    while( scanf("%d%d%d",&a,&b,&c)!=EOF)
    {
           flag=BFS();
           if( flag==-1)
               printf("impossible\n");
           else
                print(flag);
    }


    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值