Description
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)
倒水的问题,AB两个容器能不能倒出C所给的数值来;
这个题就是个光搜,可以看成是最短路径;
一共有6种操作:
倒满A,倒满B,清空A,清空B,把A倒入B,把B倒入A;
然后每次的状态就是A和B的现在有的水的容量;
用一个结构体来记录每次AB的容量以及到达这个状态的操作和步数。
#include <iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<queue>
using namespace std;
int n,m,c;
struct node
{
int v1,v2,step;
string buzhou;
};
bool flag[1000][1000];
node bfs()
{
node k= {0,0,0};
queue<node>Q;
Q.push(k);
flag[0][0]=1;
while(!Q.empty())
{
k=Q.front();
Q.pop();
if(k.v1==c||k.v2==c)
{
return k;
}
node t;
//倒满1
if(k.v1!=n)
{
t.v1=n;
t.step=k.step+1;
t.buzhou=k.buzhou+'1';
t.v2=k.v2;
if(!flag[t.v1][t.v2])
{
flag[t.v1][t.v2]=1;
Q.push(t);
}
}
//倒满2
if(k.v2!=m)
{
t.v2=m;
t.step=k.step+1;
t.buzhou=k.buzhou+'2';
t.v1=k.v1;
if(!flag[t.v1][t.v2])
{
flag[t.v1][t.v2]=1;
Q.push(t);
}
}
//清空1
if(k.v1!=0)
{
t.v1=0;
t.v2=k.v2;
t.step=k.step+1;
t.buzhou=k.buzhou+'3';
if(!flag[t.v1][t.v2])
{
flag[t.v1][t.v2]=1;
Q.push(t);
}
}
//清空2
if(k.v2!=0)
{
t.v2=0;
t.v1=k.v1;
t.step=k.step+1;
t.buzhou=k.buzhou+'4';
if(!flag[t.v1][t.v2])
{
flag[t.v1][t.v2]=1;
Q.push(t);
}
}
//2导入1
if(k.v2!=0&&k.v1!=n)
{
t.v2=k.v2-(n-k.v1);
if(t.v2<0) t.v2=0;
t.v1=k.v1+k.v2;
if(t.v1>n) t.v1=n;
t.step=k.step+1;
t.buzhou=k.buzhou+'5';
if(!flag[t.v1][t.v2])
{
flag[t.v1][t.v2]=1;
Q.push(t);
}
}
//1倒入2
if(k.v1!=0&&k.v2!=m)
{
t.v1=k.v1-(m-k.v2);
if(t.v1<0) t.v1=0;
t.v2=k.v2+k.v1;
if(t.v2>m) t.v2=m;
t.step=k.step+1;
t.buzhou=k.buzhou+'6';
if(!flag[t.v1][t.v2])
{
flag[t.v1][t.v2]=1;
Q.push(t);
}
}
}
k.step=-1;
return k;
}
int main()
{
while(~scanf("%d%d%d",&n,&m,&c))
{
memset(flag,0,sizeof(flag));
node ans;
ans=bfs();
if(ans.step>=0)
{
printf("%d\n",ans.step);
int l=ans.buzhou.size();
int i;
for(i=0; i<l; ++i)
{
switch(ans.buzhou[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(2,1)\n");
break;
case '6':
printf("POUR(1,2)\n");
break;
}
}
}
else printf("impossible\n");
}
}