搜索进阶题目之Pots

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 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.

On the first and only line is the numbers A, B, and C. These are all integers in the range from 1 to 100 and C≤max(A,B).

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’.

``3 5 4``

``````6
FILL(2)
POUR(2,1)
DROP(1)
POUR(2,1)
FILL(2)
POUR(2,1)``````

``````#include <cstdio>
#include <cstring>
int a,b,cc;
const int mn=105;
typedef struct
{
int x,y,s,p,op;
}node;
bool vis[mn][mn];
node que[mn*mn];
void BFS()
{
node c,t;
{
for(int i=1;i<=6;i++)
{
if(i==1)
{
t.x=a;
t.y=c.y;
}
if(i==2)
{
t.x=c.x;
t.y=b;
}
if(i==3)
{
t.x=0;
t.y=c.y;
}
if(i==4)
{
t.x=c.x;
t.y=0;
}
if(i==5)
{
if(c.x+c.y<=b)
{
t.x=0;
t.y=c.x+c.y;
}
else
{
t.x=c.x+c.y-b;
t.y=b;
}
}
if(i==6)
{
if(c.x+c.y<=a)
{
t.x=c.x+c.y;
t.y=0;
}
else
{
t.x=a;
t.y=c.x+c.y-a;
}
}
if(vis[t.x][t.y])
{
vis[t.x][t.y]=false;
t.op=i;
t.s=c.s+1;
que[tail++]=t;
if(t.x==cc||t.y==cc)
{
printf("%d\n",t.s);
return;
}

}
}
}
}
int main()
{
scanf("%d%d%d",&a,&b,&cc);
int x=a,y=b,t;
while(y){t=x%y,x=y,y=t;}
if(cc%x)
{
printf("impossible\n");
return 0;
}
memset(vis,true,sizeof(vis));
vis[0][0]=false;
que[0].x=0,que[0].y=0,que[0].s=0,que[0].p=-1,que[0].op=0;
BFS();
x=y=t=tail-1;
while(x>0)
{
x=que[x].p;
que[--y]=que[x];
}
while(y<=t)
{
if(que[y].op==1)
printf("FILL(1)\n");
else if(que[y].op==2)
printf("FILL(2)\n");
else if(que[y].op==3)
printf("DROP(1)\n");
else if(que[y].op==4)
printf("DROP(2)\n");
else if(que[y].op==5)
printf("POUR(1,2)\n");
else if(que[y].op==6)
printf("POUR(2,1)\n");
y++;
}
return 0;
}``````

``````#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <queue>
using namespace std;

struct node{
int Aliters , Bliters;
int step;
int myFalg;
int myNow;
};
node p[10002];
bool visited[102][102];
int A,B,C;
char lastOperation[][10] = {"","POUR(1,2)","POUR(2,1)","DROP(1)","DROP(2)","FILL(1)","FILL(2)"};
void print(node p1)
{
if( p1.myFalg != -1 )
{
print(p[p1.myFalg]);
cout << lastOperation[p1.myNow] << endl;
}
}
void BFS()
{
p[0].Aliters = 0 ;
p[0].Bliters = 0 ;
p[0].step = 0;
p[0].myFalg = -1;
p[0].myNow = 0;
int last = 1,f = 0;
visited[0][0] = true;
while( f <= last )
{
node p1 = p[f];
if( p1.Aliters == C || p1.Bliters == C )
{
cout << p1.step << endl;
print(p1);
return;
}
if( p1.Aliters < A )    //给A注满水
{
if(!visited[A][p1.Bliters]){
visited[A][p1.Bliters] = true;
p[last].Aliters = A;
p[last].Bliters = p1.Bliters;
p[last].step = p1.step + 1 ;
p[last].myNow = 5;
p[last].myFalg = f;
last++;
}
}
if( p1.Bliters < B )    //给B注满水
{
if(!visited[p1.Aliters][B]){
visited[p1.Aliters][B] = true;
p[last].Aliters = p1.Aliters;
p[last].Bliters = B;
p[last].step = p1.step + 1 ;
p[last].myNow = 6;
p[last].myFalg = f;
last++;
}
}
if( p1.Aliters < A && p1.Bliters != 0 )    //从B注水到A
{
int ml = p1.Bliters - ( A - p1.Aliters );
if( ml >= 0 ){
if( !visited[A][ml]){
visited[A][ml] = true;
p[last].Aliters = A;
p[last].Bliters = ml;
p[last].step = p1.step + 1 ;
p[last].myNow = 2;
p[last].myFalg = f;
last++;
}
}else if(ml < 0 && !visited[p1.Aliters + p1.Bliters][0]){
visited[p1.Aliters + p1.Bliters][0] = true;
p[last].Aliters = p1.Aliters + p1.Bliters;
p[last].Bliters = 0;
p[last].step = p1.step + 1 ;
p[last].myNow = 2;
p[last].myFalg = f;
last++;
}
}
if( p1.Bliters < B && p1.Aliters != 0 )    //从A注水到B
{
int ml = p1.Aliters - ( B - p1.Bliters );
if( ml >= 0 ){
if( !visited[ml][B]){
visited[ml][B] = true;
p[last].Aliters = ml;
p[last].Bliters = B;
p[last].step = p1.step + 1 ;
p[last].myNow = 1;
p[last].myFalg = f;
last++;
}
}else if( ml < 0 && !visited[0][p1.Aliters + p1.Bliters]) {
visited[0][p1.Aliters + p1.Bliters] = true;
p[last].Aliters = 0;
p[last].Bliters = p1.Aliters + p1.Bliters;
p[last].step = p1.step + 1 ;
p[last].myNow = 1;
p[last].myFalg = f;
last++;
}
}
if( p1.Aliters != 0 )       //DROP A
{
if( !visited[0][p1.Bliters] )
{
visited[0][p1.Bliters] = true;
p[last].Aliters = 0;
p[last].Bliters = p1.Bliters;
p[last].step = p1.step + 1 ;
p[last].myNow = 3;
p[last].myFalg = f;
last++;
}
}
if( p1.Bliters != 0 )   //DROP B
{
if( !visited[p1.Aliters][0] )
{
visited[p1.Aliters][0] = true;
p[last].Aliters = p1.Aliters;
p[last].Bliters = 0;
p[last].step = p1.step + 1 ;
p[last].myNow = 4;
p[last].myFalg = f;
last++;
}
}
f++;
}
cout << "impossible" << endl;
}
int main( )
{
cin >> A >> B >> C;
memset(visited,false,sizeof(visited));
BFS();
return 0;
}
``````