POJ3414(KB1-H BFS)

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

Input

On the first and only line are the numbers AB, 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)

Source

Northeastern Europe 2002, Western Subregion
 
 //2017-02-24
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; struct node
{
int a, b, step, pre, op;
}q[]; int pots[], POT[], book[][];
string option[] = {"", "FILL(1)", "FILL(2)", "DROP(1)", "DROP(2)", "POUR(1,2)", "POUR(2,1)"}; void fill(int i)
{
pots[i] = POT[i];
} void drop(int i)
{
pots[i] = ;
} void pour(int i, int j)
{
if(pots[i] > POT[j]-pots[j]){
pots[i] -= (POT[j]-pots[j]);
pots[j] = POT[j];
}else{
pots[j] += pots[i];
pots[i] = ;
}
} void init(int a, int b)
{
pots[] = a;
pots[] = b;
} void push(int pos, int a, int b, int step, int pre, int op)
{
q[pos].a = a;
q[pos].b = b;
q[pos].step = step;
q[pos].pre = pre;
q[pos].op = op;
} void print(int pos)
{
if(q[pos].pre == -)return;
print(q[pos].pre);
cout<<option[q[pos].op]<<endl;
} int min(int a, int b)
{
return a < b ? a : b;
} int main()
{
int C, a, b, step;
while(cin>>POT[]>>POT[]>>C)
{
int head = , tail = ;
q[].a = ;
q[].b = ;
q[].step = ;
q[].pre = -;
memset(book, , sizeof(book));
book[][] = ;
while(head < tail)
{
a = q[head].a;
b = q[head].b;
step = q[head].step; if(a==C || b==C){
cout<<step<<endl;
print(head);
break;
} init(a, b);
fill();
if(!book[pots[]][pots[]]){
book[pots[]][pots[]] = ;
push(tail, pots[], pots[], step+, head, );
tail++;
} init(a, b);
fill();
if(!book[pots[]][pots[]]){
book[pots[]][pots[]] = ;
push(tail, pots[], pots[], step+, head, );
tail++;
} if(a>){
init(a, b);
drop();
if(!book[pots[]][pots[]]){
book[pots[]][pots[]] = ;
push(tail, pots[], pots[], step+, head, );
tail++;
}
} if(b>){
init(a, b);
drop();
if(!book[pots[]][pots[]]){
book[pots[]][pots[]] = ;
push(tail, pots[], pots[], step+, head, );
tail++;
}
} init(a, b);
pour(, );
if(!book[pots[]][pots[]]){
book[pots[]][pots[]] = ;
push(tail, pots[], pots[], step+, head, );
tail++;
} init(a, b);
pour(, );
if(!book[pots[]][pots[]]){
book[pots[]][pots[]] = ;
push(tail, pots[], pots[], step+, head, );
tail++;
} head++;
}
if(head>=tail)cout<<"impossible"<<endl;
}
return ;
}
上一篇:golang prometheus包的使用


下一篇:基于虹软人脸识别,实现RTMP直播推流追踪视频中所有人脸信息(C#)