题目来源:https://vjudge.net/contest/275251#problem/H

题目描述:

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)

代码如下:

#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
#define mm(a) memset(a,0,sizeof(a))
int A,B,C;
void print_path(int n)
{
    switch(n)
    {
    case 0:
        cout<<"FILL(1)";
        break;
    case 1:
        cout<<"FILL(2)";
        break;
    case 2:
        cout<<"DROP(1)";
        break;
    case 3:
        cout<<"DROP(2)";
        break;
    case 4:
        cout<<"POUR(1,2)";
        break;
    case 5:
        cout<<"POUR(2,1)";
        break;
    }
    cout<<endl;
}
struct node
{
    int x,y,step,op;
} vis[110][110];
struct point
{
    int x, y;
    point(int xx,int yy)
    {
        x=xx,y=yy;
    }
};
void DFS(int x, int y)//dfs找前缀
{
    if(x==0&&y==0)
        return;
    DFS(vis[x][y].x, vis[x][y].y);
    print_path(vis[x][y].op);
}
int BFS(point t)
{
    queue<point>Q;
    vis[0][0].step=1;
    Q.push(t);
    while(Q.size())
    {
        point s=Q.front();
        Q.pop();
        if(s.x==C||s.y==C)
        {
            cout<<vis[s.x][s.y].step-1<<endl;;
            DFS(s.x,s.y);
            return 0;
        }
        for(int i=0;i<6;i++)
        {
            point q=s;
            if(i==0)//FILL(1)
                q.x = A;
            else if(i==1)//FILL(2)
                q.y=B;
            else if(i==2)//DROP(1)
                q.x =0;
            else if(i==3)//DROP(2)
                q.y=0;
            else if(i==4)//POUR(1,2)
            {
                if(q.x+q.y<=B)
                    q.y+=q.x, q.x=0;
                else
                    q.x-=(B-q.y),q.y=B;
            }
            else//POUR(2,1)
            {
                if(q.x+q.y<=A)
                    q.x+=q.y,q.y=0;
                else
                    q.y-=(A-q.x),q.x=A;
            }
            if( vis[q.x][q.y].step==0)
            {
                vis[q.x][q.y].step=vis[s.x][s.y].step+1;
                vis[q.x][q.y].x=s.x;
                vis[q.x][q.y].y=s.y;
                vis[q.x][q.y].op=i;
                Q.push(q);
            }
        }
    }
    return -1;
}
int main()
{
    while(cin>>A>>B>>C)
    {
        mm(vis);
        int ans=BFS(point(0,0));
        if(ans==-1)
            cout<<"impossible\n";
    }
    return 0;
}