POJ 3414 Pots【广搜】

Description

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

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)
code:
View Code
#include<stdio.h>
#include<string.h>
#define clr(x)memset(x,0,sizeof(x))
int v[1001][1001];
int va;
int vb;
struct node
{
int x,y,xu,step;
}q[100000];
int pre[100000];
//1 full 1 2 full 2 3 drop 1 4 drop 2 5 from a to b 6 from b to a
void print(int x)
{
if(pre[x]!=0)
print(pre[x]);
//printf("%d\n",q[x].xu);
if(q[x].xu==1)
printf("FILL(1)\n");
else if(q[x].xu==2)
printf("FILL(2)\n");
else if(q[x].xu==3)
printf("DROP(1)\n");
else if(q[x].xu==4)
printf("DROP(2)\n");
else if(q[x].xu==5)
printf("POUR(1,2)\n");
else printf("POUR(2,1)\n");
}
int main()
{
int a,b,c,i,j,front,rear,tmp,v1,v2,res,ans;
while(scanf("%d%d%d",&a,&b,&c)!=EOF)
{
va=vb=0;
front=rear=0;
q[rear].xu=0; q[rear].step=0; q[rear].x=0; q[rear++].y=0;
res=-1;
v[0][0]=1;

memset(pre,0,sizeof(pre));
while(front<=rear)
{
va=q[front].x; vb=q[front].y;
//printf("%d %d\n",va,vb);
if(va==c||vb==c) { res=q[front].step;ans=front; break;}
if(!v[a][vb]) { v[a][vb]=1; q[rear].x=a; q[rear].y=vb; q[rear].xu=1; pre[rear]=front; q[rear++].step=q[front].step+1; }
if(!v[va][b]) { v[va][b]=1; q[rear].x=va; q[rear].y=b; q[rear].xu=2; pre[rear]=front; q[rear++].step=q[front].step+1; }
if(!v[0][vb]) { v[0][vb]=1; q[rear].x=0; q[rear].y=vb; q[rear].xu=3; pre[rear]=front; q[rear++].step=q[front].step+1; }
if(!v[va][0]) { v[va][0]=1; q[rear].x=va; q[rear].y=0; q[rear].xu=4; pre[rear]=front; q[rear++].step=q[front].step+1; }
if((va+vb)/b<1){
if(!v[0][va+vb]){ v[0][va+vb]=1; q[rear].x=0; q[rear].y=va+vb; q[rear].xu=5; pre[rear]=front; q[rear++].step=q[front].step+1; }
}
else if(!v[va-(b-vb)][b]) { v[va-(b-vb)][b]=1; q[rear].x=va-(b-vb); q[rear].y=b; q[rear].xu=5; pre[rear]=front; q[rear++].step=q[front].step+1; }
if((va+vb)/a<1){
if(!v[va+vb][0]){ v[va+vb][0]=1; q[rear].x=va+vb; q[rear].y=0; q[rear].xu= 6; pre[rear]=front; q[rear++].step=q[front].step+1; }
}
else if(!v[a][vb-(a-va)]) { v[a][vb-(a-va)]=1; q[rear].x=a; q[rear].y=vb-(a-va); q[rear].xu=6; pre[rear]=front; q[rear++].step=q[front].step+1; }
front++;
}
if(res==-1)printf("impossible\n");
else {
printf("%d\n",res);
print(ans);
}
}
return 0;
}

posted @ 2012-03-22 19:27  'wind  阅读(205)  评论(0编辑  收藏  举报