EricYang

Tech Spot of Eric

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

Pots

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 5062 Accepted: 2156 Special Judge

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

 
记录路径BFS,参考算法导论BFS
#include <iostream>
#include <cstring>
#include <queue>

using namespace std;

const int SIZE=100;

typedef struct
{
    int a;
    int b;
    int step;
}Node;

struct pn
{
 int prex;
 int prey;
}prev[SIZE+1][SIZE+1];

bool visit[SIZE+1][SIZE+1];
int a,b,c;

void DFS(int x, int y)
{
    if(prev[x][y].prex+prev[x][y].prey!=0)
        DFS(prev[x][y].prex,prev[x][y].prey);

    int px=prev[x][y].prex;
    int py=prev[x][y].prey;

    //FILL(1)
    if(px!=a&&x==a&&y==py)
    {
        cout<<"FILL(1)"<<endl;
        return;
    }

    //FILL(2)
    if(py!=b&&y==b&&x==px)
    {
        cout<<"FILL(2)"<<endl;
        return;
    }

    //DROP(1)
    if(px!=0 && x==0 && y==py)
    {
        cout<<"DROP(1)"<<endl;
        return ;
    }

    //DROP(2)
    if(py!=0 && y==0 && x==px)
    {
        cout<<"DROP(2)"<<endl;
        return;
    }

    //从A倒入B或B倒入A
    if(px+py==x+y)
    {
        //A->B
        if(x==0||y==b)
        {
            cout<<"POUR(1,2)"<<endl;
        }
        else
            cout<<"POUR(2,1)"<<endl;
        return;
    }

}
void BFS()
{
    Node temp, temp1;
    queue<Node> nq;
    bool isSolve;

    isSolve=false;
    visit[0][0]=true;
    temp.a=0;
    temp.b=0;
    temp.step=0;
    nq.push(temp);
    while(!nq.empty())
    {
        temp=nq.front();
        nq.pop();

        if(temp.a==c || temp.b==c)
        {
            cout<<temp.step<<endl;
            DFS(temp.a,temp.b);
            isSolve=true;
            break;
        }

        temp1.step=temp.step+1;
        //OP 0 FILL(1)
        temp1.a=a;
        temp1.b=temp.b;
        if(!visit[temp1.a][temp1.b])
        {
            visit[temp1.a][temp1.b]=true;
            prev[temp1.a][temp1.b].prex=temp.a;
            prev[temp1.a][temp1.b].prey=temp.b;
            nq.push(temp1);
        }
        //OP 1 FILL(2)
        temp1.a=temp.a;
        temp1.b=b;
        if(!visit[temp1.a][temp1.b])
        {
            visit[temp1.a][temp1.b]=true;
            prev[temp1.a][temp1.b].prex=temp.a;
            prev[temp1.a][temp1.b].prey=temp.b;
            nq.push(temp1);
        }
        //OP 2 DROP(1)
        temp1.a=0;
        temp1.b=temp.b;
        if(!visit[temp1.a][temp1.b])
        {
            visit[temp1.a][temp1.b]=true;
            prev[temp1.a][temp1.b].prex=temp.a;
            prev[temp1.a][temp1.b].prey=temp.b;
            nq.push(temp1);
        }
        //OP 3 DROP(2)
        temp1.a=temp.a;
        temp1.b=0;
        if(!visit[temp1.a][temp1.b])
        {
            visit[temp1.a][temp1.b]=true;
            prev[temp1.a][temp1.b].prex=temp.a;
            prev[temp1.a][temp1.b].prey=temp.b;
            nq.push(temp1);
        }
        //OP 4 POUR(1,2)
        temp1.b=(temp.a+temp.b)<b?(temp.a+temp.b):b;
        temp1.a=temp.a-(temp1.b-temp.b);
        if(!visit[temp1.a][temp1.b])
        {
            visit[temp1.a][temp1.b]=true;
            prev[temp1.a][temp1.b].prex=temp.a;
            prev[temp1.a][temp1.b].prey=temp.b;
            nq.push(temp1);
        }
        //OP 5 POUR(2,1)
        temp1.a=(temp.a+temp.b)<a?(temp.a+temp.b):a;
        temp1.b=temp.b-(temp1.a-temp.a);
        if(!visit[temp1.a][temp1.b])
        {
            visit[temp1.a][temp1.b]=true;
            prev[temp1.a][temp1.b].prex=temp.a;
            prev[temp1.a][temp1.b].prey=temp.b;
            nq.push(temp1);
        }
    }
    if(!isSolve)
        cout<<"impossible"<<endl;
}
int main()
{
    while(cin>>a>>b>>c)
    {
        memset(visit,0,sizeof(visit));
        memset(prev,0,sizeof(prev));
        BFS();
    }
    return 0;
}
posted on 2011-05-09 22:10  Eric-Yang  阅读(308)  评论(0编辑  收藏  举报