题目链接:点击打开链接
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)
操作:1,将水桶装满fill(i);
2,将水桶清空
3,将水桶i倒入水桶j中(直到i到完或j到满)
问两个水桶中的残余两符合给出的体积的最短操作
输出操作过程,若不能实现输出impossble
基本思路:从起始点(两个水桶都为空),对每个状态进行上面的6种操作,bfs搜索
这个题wa了好几发,最后是因为没有判断桶里水的多少就进行操作了.注意这一点,先判断桶的水,根据情况在进行操作那一步
<span style="font-size:24px;">#include <iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int A,B,C;
struct node
{
int a,b;
int id;
int pre;
} q[999999],t;
int vis[200][200];
int bfs(int x,int y)
{
int in,out;
in=out=0;
q[in].a=x;
q[in].b=y;
q[in].id=-1;
q[in++].pre=-1;
int d;
while(in>out)
{
//cout<<out<<"&&"<<in<<endl;
t=q[out];
d=out++;
//cout<<t.a<<" "<<t.b<<endl;
if(t.a==C||t.b==C)
{
//cout<<"##\n";
return d;
}
if(t.a<A&&vis[A][t.b]==0)
{
vis[A][t.b]=1;
q[in].a=A;
q[in].b=t.b;
q[in].id=1;
q[in++].pre=d;
}
if(t.b<B&&vis[t.a][B]==0)
{
vis[t.a][B]=1;
q[in].a=t.a;
q[in].b=B;
q[in].id=2;
q[in++].pre=d;
}
if(t.a>0&&vis[0][t.b]==0)
{
vis[0][t.b]=1;
q[in].a=0;
q[in].b=t.b;
q[in].id=3;
q[in++].pre=d;
}
if(t.b>0&&vis[t.a][0]==0)
{
vis[t.a][0]=1;
q[in].a=t.a;
q[in].b=0;
q[in].id=4;
q[in++].pre=d;
}
if(t.a>0&&t.b>=0&&t.b<B)
{
if(t.a+t.b<=B&&vis[0][t.a+t.b]==0)
{
vis[0][t.a+t.b]=1;
q[in].a=0;
q[in].b=t.a+t.b;
q[in].id=5;
q[in++].pre=d;
}
else if(vis[t.a-(B-t.b)][B]==0)
{
vis[t.a-(B-t.b)][B]=1;
q[in].a=t.a-(B-t.b);
q[in].b=B;
q[in].id=5;
q[in++].pre=d;
}
}
if(t.a>=0&&t.b>0&&t.a<A)
{
if(t.a+t.b<=A&&vis[t.a+t.b][0]==0)
{
vis[t.a+t.b][0]=1;
q[in].a=t.a+t.b;
q[in].b=0;
q[in].id=6;
q[in++].pre=d;
}
else if(vis[A][t.b-(A-t.a)]==0)
{
vis[A][t.b-(A-t.a)]=1;
q[in].a=A;
q[in].b=t.b-(A-t.a);
q[in].id=6;
q[in++].pre=d;
}
}
}
return -1;
}
int main()
{
while(cin>>A>>B>>C&&C)
{
memset(vis,0,sizeof(vis));
vis[0][0]=1;
int d=bfs(0,0);
char str[10000][10];
//memset(str,'\0',sizeof(str));
int top=0;
int sum=0;
if(d==-1)cout<<"impossible\n";
else
{
while(1)
{
sum++;
if(q[d].id==1)
{
strcpy(str[top],"FILL(1) ");
}
else if(q[d].id==2)
{
strcpy(str[top],"FILL(2)");
}
else if(q[d].id==3)
{
strcpy(str[top],"DROP(1)");
}
else if(q[d].id==4)
{
strcpy(str[top],"DROP(2)");
}
else if(q[d].id==5)
{
strcpy(str[top],"POUR(1,2)");
}
else if(q[d].id==6)
{
strcpy(str[top],"POUR(2,1)");
}
top++;
d=q[d].pre;
if(d==0)break;
}
cout<<sum<<endl;
for(int i=top-1; i>=0; i--)
{
cout<<str[i]<<endl;
}
}
}
return 0;
}</span>