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 jis 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 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)
这道题的意思大家都能理解,就是给你三个空水杯,看你怎么操作把第三个水杯装满。困扰了我很久的这道题,终于被我解决了,虽然程序长了一点,但是我还是蛮开心能解决它的。
利用string的增添字符方便,用字符去代表操作,然后最后遍历字符,将操作指令输出。
#include<stdio.h>
#include<string>
#include<queue>
#include<string.h>
#include<algorithm>
using namespace std;
struct node
{
int a,b,n;
string s;
};
int a,b,c;
int book[110][110];
void cn()
{
queue<node>Q;
node st,en;
st.a=a,st.b=0,st.n=1,st.s="1";
Q.push(st);
st.a=0,st.b=b,st.s="2";
Q.push(st);
book[a][0]=book[0][b]=1;
while(!Q.empty())
{
en=Q.front();
Q.pop();
if(en.a==c||en.b==c)
{
printf("%d\n",en.n);
for(int i=0; i<en.n; i++)
{
if(en.s[i]=='1')
printf("FILL(1)\n");
else if(en.s[i]=='2')
printf("FILL(2)\n");
else if(en.s[i]=='3')
printf("POUR(1,2)\n");
else if(en.s[i]=='4')
printf("POUR(2,1)\n");
else if(en.s[i]=='5')
printf("DROP(1)\n");
else if(en.s[i]=='6')
printf("DROP(2)\n");
}
return;
}
for(int i=1; i<=6; i++)
{
if(i==1)
{
if(en.a!=a&&book[a][en.b]==0)
{
st.a=a;
st.b=en.b;
st.s=en.s;
st.s+='1';
st.n=en.n+1;
book[a][en.b]=1;
Q.push(st);
}
}
else if(i==2)
{
if(en.b!=b&&book[en.a][b]==0)
{
st.a=en.a;
st.b=b;
st.s=en.s;
st.s+='2';
st.n=en.n+1;
book[en.a][b]=1;
Q.push(st);
}
}
else if(i==3)
{
if(en.a!=0&&en.b!=b)
{
if(en.a>(b-en.b))
{
st.a=en.a-(b-en.b);
st.b=b;
if(book[st.a][st.b]==0)
{
st.n=en.n+1;
st.s=en.s;
st.s+='3';
book[st.a][st.b]=1;
Q.push(st);
}
}
else
{
st.a=0;
st.b=en.b+en.a;
if(book[st.a][st.b]==0)
{
st.n=en.n+1;
st.s=en.s;
st.s+='3';
book[st.a][st.b]=1;
Q.push(st);
}
}
}
}
else if(i==4)
{
if(en.b!=0&&en.a!=a)
{
if(en.b>(a-en.a))
{
st.a=a;
st.b=en.b-(a-en.a);
if(book[st.a][st.b]==0)
{
st.n=en.n+1;
st.s=en.s;
st.s+='4';
book[st.a][st.b]=1;
Q.push(st);
}
}
else
{
st.a=en.a+en.b;
st.b=0;
if(book[st.a][st.b]==0)
{
st.n=en.n+1;
st.s=en.s;
st.s+='4';
book[st.a][st.b]=1;
Q.push(st);
}
}
}
}
else if(i==5)
{
if(en.a!=0&&book[0][en.b]==0)
{
st.a=0;
st.b=en.b;
st.n=en.n+1;
st.s=en.s;
st.s+='5';
book[0][en.b]=1;
Q.push(st);
}
}
else if(i==6)
{
if(en.b!=0&&book[en.a][0]==0)
{
st.a=en.a;
st.b=0;
st.n=en.n+1;
st.s=en.s;
st.s+='6';
book[en.a][0]=1;
Q.push(st);
}
}
}
}
printf("impossible\n");
}
int main()
{
while(~scanf("%d%d%d",&a,&b,&c)&&a+b+c)
{
memset(book,0,sizeof(book));
if(c>max(a,b))
printf("impossible\n");
else
cn();
}
return 0;
}