You are given two pots, having the volume of A and B liters respectively. The following operations can be performed:
FILL(i) fill the pot i (1 ≤ i ≤ 2) from the tap;
DROP(i) empty the pot i to the drain;
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 A, B, 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)
给定两个杯子,有各自容量
每次可以进行以下操作
倒满A或者B中的水(FILL)
倒掉A或者B中的水(DROP)
将i中的水倒入j中(DROP(i,j))
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<queue>
#include<stdlib.h>
#define N 300
#include<string>
using namespace std;
int a,b,c,ans,flag,map[110][110][3],res[100];
struct node
{
int aa;
int bb,step;
};
queue<struct node>mmp;
void dfs()
{
int aa,bb,ss;
struct node qq;
qq=mmp.front();
aa=qq.aa;
bb=qq.bb;
ss=qq.step;
if(aa==c||bb==c)//判断是否为答案
{
flag=1;
ans=ss;
return;
}
mmp.pop();
if(!map[a][bb][2])
{
struct node mm;
mm.aa=a;
mm.bb=bb;
mm.step=ss+1;
map[a][bb][0]=aa;
map[a][bb][1]=bb;
map[a][bb][2]=1;
mmp.push(mm);
}
if(!map[aa][b][2])
{
struct node mm;
mm.aa=aa;
mm.bb=b;
mm.step=ss+1;
mmp.push(mm);
map[aa][b][0]=aa;
map[aa][b][1]=bb;
map[aa][b][2]=2;
}
if(!map[0][bb][2])
{
struct node mm;
mm.aa=0;
mm.bb=bb;
mm.step=ss+1;
mmp.push(mm);
map[0][bb][0]=aa;
map[0][bb][1]=bb;
map[0][bb][2]=3;
}
if(!map[aa][0][2])
{
struct node mm;
mm.aa=aa;
mm.bb=0;
mm.step=ss+1;
mmp.push(mm);
map[aa][0][0]=aa;
map[aa][0][1]=bb;
map[aa][0][2]=4;
}
//方法5
int ii,jj;
if((aa+bb)>a)
{
jj=aa+bb-a;
ii=a;
}
else
{
ii=aa+bb;
jj=0;
}
if(!map[ii][jj][2])
{
struct node mm;
mm.aa=ii;
mm.bb=jj;
mm.step=ss+1;
mmp.push(mm);
map[ii][jj][0]=aa;
map[ii][jj][1]=bb;
map[ii][jj][2]=5;
}
//方法6
if((aa+bb)>b)
{
ii=aa+bb-b;
jj=b;
}
else
{
ii=0;
jj=aa+bb;
}
if(!map[ii][jj][2])
{
struct node mm;
mm.aa=ii;
mm.bb=jj;
mm.step=ss+1;
mmp.push(mm);
map[ii][jj][0]=aa;
map[ii][jj][1]=bb;
map[ii][jj][2]=6;
}
}
void bfs()
{
struct node qq;
qq.aa=qq.bb=qq.step=0;
map[0][0][0]=map[0][0][1]=-1;
map[0][0][2]=-1;
mmp.push(qq);
while(!mmp.empty())
{
dfs();
if(flag)
return ;
}
}
int main()
{
scanf("%d %d %d",&a,&b,&c);
bfs();
if(flag)
{
int ii,jj,mod1=1,mod2=2;
struct node mm;
mm=mmp.front();
ii=mm.aa;
jj=mm.bb;
int k=0;
while((ii!=-1)||(jj!=-1))
{
res[k++]=map[ii][jj][2];
mod1=map[ii][jj][0];
mod2=map[ii][jj][1];
ii=mod1;
jj=mod2;
}
printf("%d\n",ans);
for(int i=k-1; i>=0; i--)
{
if(res[i]==1)
{
printf("FILL(1)\n");
continue;
}
if(res[i]==2)
{
printf("FILL(2)\n");
continue;
}
if(res[i]==3)
{
printf("DROP(1)\n");
continue;
}
if(res[i]==4)
{
printf("DROP(2)\n");
continue;
}
if(res[i]==5)
{
printf("POUR(2,1)\n");
continue;
}
if(res[i]==6)
{
printf("POUR(1,2)\n");
continue;
}
}
}
else
printf("impossible\n");
}