POJ

来源:互联网 发布:淘宝客推广首页 编辑:程序博客网 时间:2024/06/08 11:59
Time Limit: 1000MS  Memory Limit: 65536KTotal Submissions: 18011 Accepted: 7622 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 ≤ 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 potj is full (and there may be some water left in the pot i), or the poti 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 exactlyC liters of water in one of the pots.

Input

On the first and only line are the numbers A, B, andC. 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 operationsK. 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

6FILL(2)POUR(2,1)DROP(1)POUR(2,1)FILL(2)POUR(2,1)这题的思路和bfs迷宫一个思路,bfs一遍就行了
#include <iostream>#include <cstdio>#include <cmath>#include <cstring>#include <queue>#include <stack>using namespace std;typedef struct{    int x, y;    int pr;    int num;    int f;    int i, j;} node;int v[150][150];void bfs(int n, int m, int c){    node a, b;    int i = 2;    int pr;    queue<node>q;    stack<node>s1;    stack<node>s2;    a.x = n;    a.y = 0;    a.num = 0;    a.pr = -1;    a.f = 1;    a.i = 1;    v[n][0] = 1;    q.push(a);    a.x = 0;    a.y = m;    a.num = 1;    a.pr = -1;    a.f = 1;    a.i = 2;    v[0][m] = 1;    q.push(a);    while(!q.empty())    {        a = q.front();        q.pop();        s1.push(a);        if(a.x == c || a.y == c)        {            pr = a.pr;            s2.push(a);            s1.pop();            while(!s1.empty() && a.pr != -1)            {                b = s1.top();                s1.pop();                if(b.num == pr)                {                    pr = b.pr;                    s2.push(b);                }            }            cout<<s2.size()<<endl;            while(!s2.empty())            {                b = s2.top();                s2.pop();                if(b.f == 1)                {                    printf("FILL(%d)\n",b.i);                }                else if(b.f == 2)                {                    printf("DROP(%d)\n",b.i);                }                else                    printf("POUR(%d,%d)\n",b.i,b.j);            }            return;        }        if(a.x >= m - a.y && !v[a.x-(m-a.y)][m])        {            b.y = m;            b.x = a.x- (m - a.y);            b.num = ++i;            b.pr = a.num;            b.f = 3;            b.i = 1;            b.j = 2;            v[b.x][b.y] = 1;            q.push(b);        }        if(a.x < m - a.y && !v[0][a.x+a.y])        {            b.x = 0;            b.y = a.x+a.y;            b.num = ++i;            b.pr = a.num;            b.f = 3;            b.i = 1;            b.j = 2;            v[b.x][b.y] = 1;            q.push(b);        }        if(a.x > 0 && !v[0][a.y])        {            b.x = 0;            b.y = a.y;            b.num = ++i;            b.pr = a.num;            b.f = 2;            b.i = 1;            v[b.x][b.y] = 1;            q.push(b);        }        if(a.x < n && !v[n][a.y])        {            b.x = n;            b.y = a.y;            b.num = ++i;            b.pr = a.num;            b.f = 1;            b.i = 1;            v[b.x][b.y] = 1;            q.push(b);        }        if(a.y >= n - a.x && !v[n][a.y-(n-a.x)])        {            b.y = a.y - (n - a.x);            b.x = n;            b.num = ++i;            b.pr = a.num;            b.f = 3;            b.i = 2;            b.j = 1;            v[b.x][b.y] = 1;            q.push(b);        }        if(a.y < n - a.x && !v[a.x+a.y][0])        {            b.x = a.x + a.y;            b.y = 0;            b.num = ++i;            b.pr = a.num;            b.f = 3;            b.i = 2;            b.j = 1;            v[b.x][b.y] = 1;            q.push(b);        }        if(a.y < m && !v[a.x][m])        {            b.x = a.x;            b.y = m;            b.pr = a.num;            b.num = ++i;            b.f = 1;            b.i = 2;            v[b.x][b.y] = 1;            q.push(b);        }        if(a.y > 0 && !v[a.x][0])        {            b.x = a.x;            b.y = 0;            b.pr = a.num;            b.num = ++i;            b.f = 2;            b.i = 2;            v[b.x][b.y] = 1;            q.push(b);        }    }    printf("impossible\n");}int main(){    int n, m, c;    while(cin>>n>>m>>c)    {        memset(v,0,sizeof(v));        bfs(n,m,c);    }    return 0;}