# Openjudge Pots

## Pots

1000ms

65536kB

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 are 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)

(A, j) : FILL(1)

(i, B) : FILL(2)

(0, j): DROP(1)

(i, 0): DROP(2)

(i+j, 0) or (A, j-A+i) : POUR(2,1)

(0, i+j) or (i-B+j, B) : POUR(1,2)

 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 #include #include #include #include #include using namespace std; /* Starting From (0,0),End With (C,X) Or (X,C) Operations: FILL(1):(A,XB) FILL(2):(XA,B) DROP(1):(0,XB) DROP(2):(XA,0) POUR(2,1): POUR(1,2): */ struct State{     char C;//F:Fill;P:Pour;D:Drop     int N1;     int N2;     int Parent_X;     int Parent_Y; }; int A,B,C; int pos_x,pos_y; State S; bool visited; inline void Fill_State(int x,int y,char type,int N1,int N2,int Parent_X,int Parent_Y){     if(visited[x][y])return;     S[x][y].C=type;     S[x][y].N1=N1;     S[x][y].N2=N2;     S[x][y].Parent_X=Parent_X;     S[x][y].Parent_Y=Parent_Y; } bool BFS(){     queue N1,N2;     N1.push(0);     N2.push(0);     while(!N1.empty()){         int t1=N1.front(),t2=N2.front();         N1.pop();N2.pop();         if(visited[t1][t2])continue;         visited[t1][t2]=true;         if(t1==C||t2==C){             pos_x=t1;             pos_y=t2;             return true;         }         //Operations         if(t1!=A){//FIll A             N1.push(A);             N2.push(t2);             Fill_State(A,t2,'F',1,0,t1,t2);         }         if(t2!=B){//FILL B             N1.push(t1);             N2.push(B);             Fill_State(t1,B,'F',2,0,t1,t2);         }         if(t1!=0){//DROP A             N1.push(0);             N2.push(t2);             Fill_State(0,t2,'D',1,0,t1,t2);         }         if(t2!=0){//DROP B             N1.push(t1);             N2.push(0);             Fill_State(t1,0,'D',2,0,t1,t2);         }         if(t1!=0&&t2!=B){//POUR(A,B)             if(t1<=B-t2){                 N1.push(0);                 N2.push(t2+t1);                 Fill_State(0,t2+t1,'P',1,2,t1,t2);             }else{                 N1.push(t1-B+t2);                 N2.push(B);                 Fill_State(t1-B+t2,B,'P',1,2,t1,t2);             }         }         if(t1!=A&&t2!=0){//POUR(B,A)             if(t2<=A-t1){                 N1.push(t2+t1);                 N2.push(0);                 Fill_State(t2+t1,0,'P',2,1,t1,t2);             }else{                 N1.push(A);                 N2.push(t2-A+t1);                 Fill_State(A,t2-A+t1,'P',2,1,t1,t2);             }         }     }     return false; } int main(){     scanf("%d%d%d",&A,&B,&C);     memset(visited,false,sizeof(visited));     S.Parent_X=-1;     S.Parent_Y=-1;     bool flag=BFS();     if(!flag){         printf("impossible");     }else{         stack Stack_ins;         for(State * i=&S[pos_x][pos_y];!(i->Parent_X==-1&&i->Parent_Y==-1);i=&S[i->Parent_X][i->Parent_Y])Stack_ins.push(i);         printf("%d\n",Stack_ins.size());         while(!Stack_ins.empty()){             switch(Stack_ins.top()->C){             case 'F':                 printf("FILL(%d)\n",Stack_ins.top()->N1);                 break;             case 'P':                 printf("POUR(%d,%d)\n",Stack_ins.top()->N1,Stack_ins.top()->N2);                 break;             case 'D':                 printf("DROP(%d)\n",Stack_ins.top()->N1);                 break;             }             Stack_ins.pop();         }     } }