poj 3414 Pots

题意:给出A, B, C分别代表第一个瓶子的容量,第二个瓶子的容量,以及目标水量,只要是经过以下六个步骤能够有一个瓶子能够达到目标水量,就完成目标,记录步骤;

1. 倒空第一个

2. 倒空第二个

3. 把第一个瓶子的水倒入第二个

4. 把第二个瓶子的水倒入第一个

5. 倒满第一个

6. 倒满第二个

ps : 上述六个步骤无先后顺序

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 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 A, B, 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 <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <cmath>
#define LL long long
#define INF 0x3f3f3f3f
#define PI 3.1415926
#define RR freopen("in.txt","r",stdin)
#define WW freopen("out.txt","w",stdout)
using namespace std;
int A,B,C;
struct node
{
    int a,b;
    int step;
    string str[200];
};

bool vis[110][110];

bool BFS()
{
    memset(vis,false,sizeof(vis));
    queue<node>Q;
    node tmp,c;
    tmp.a = 0;
    tmp.b = 0;
    tmp.step = 0;
    vis[0][0] = true;
    Q.push(tmp);

    while(!Q.empty())
    {
        tmp = Q.front();
        Q.pop();
        if(tmp.a == C || tmp.b == C)
        {
            printf("%d\n",tmp.step);
            for(int i=1; i<=tmp.step; i++)
            {
                cout<<tmp.str[i]<<endl;
            }
            return true;
        }

        if(tmp.a == 0)
        {
            c = tmp;
            c.a = A;
            c.step ++;
            c.str[c.step] = "FILL(1)";
            if(!vis[c.a][c.b])
            {
                vis[c.a][c.b] = true;
                Q.push(c);
            }
        }
        if(tmp.b == 0)
        {
            c = tmp;
            c.step ++;
            c.b = B;
            c.str[c.step] = "FILL(2)";
            if(!vis[c.a][c.b])
            {
                vis[c.a][c.b] = true;
                Q.push(c);
            }
        }
        if(tmp.a <= A && tmp.a != 0)
        {
            c = tmp;
            c.step ++;
            c.a = 0;
            c.str[c.step] = "DROP(1)";
            if(!vis[c.a][c.b])
            {
                vis[c.a][c.b] = true;
                Q.push(c);
            }
        }
        if(tmp.b <= B && tmp.b != 0)
        {
            c = tmp;
            c.step ++;
            c.b = 0;
            c.str[c.step] = "DROP(2)";
            if(!vis[c.a][c.b])
            {
                vis[c.a][c.b] = true;
                Q.push(c);
            }
        }

        if(tmp.a < A && tmp.b)
        {
            c = tmp;
            c.step ++;
            if(c.a + c.b <= A)
            {
                c.a += c.b;
                c.b = 0;
            }
            else
            {
                c.b -= (A- c.a);
                c.a = A;
            }
            c.str[c.step] = "POUR(2,1)";
            if(!vis[c.a][c.b])
            {
                vis[c.a][c.b] = true;
                Q.push(c);
            }
        }

        if(tmp.b < B && tmp.a)
        {
            c = tmp;
            c.step ++;
            if(c.a + c.b <= B)
            {
                c.b += c.a;
                c.a = 0;
            }
            else
            {
                c.a -= (B - c.b);
                c.b = B;
            }
            c.str[c.step] = "POUR(1,2)";
            if(!vis[c.a][c.b])
            {
                vis[c.a][c.b] = true;
                Q.push(c);
            }
        }
    }
    return false;
}
int main()
{
    cin>>A>>B>>C;
    if(!BFS())
        printf("impossible\n");
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值