poj 3414 Pots (优先队列)

http://poj.org/problem?id=3414

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)

有两个容器的容器分别是A,B

六种状态

FILL(1)//将1号容器充满

FILL(2)//将2号容器充满

DROP(1)//将1号容器的水全部倒出

DROP(2)//将2号容器的水全部倒出

POUR(1,2)//将1号容器的水倒入2号容器,若不足则倒完,否则,将2号容器倒满为止

POUR(2,1)//将2号容器的水倒入1号容器,若不足则倒完,否则,将1号容器倒满为止

pots[][]记录路径,简单的BFS就OK了

用C++交一直RE,最后改了G++就AC了

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <stack>
#include <queue>

using namespace std;

#define N 106
#define met(a, b) memset (a, b, sizeof (a))

typedef long long LL;
//const int INF = ((1<<31)-1);

struct node1
{
    int prea, preb, op;
}pots[N][N];

struct node
{
    int a, b, step;

    bool friend operator < (const node &a, const node &b)
    {
        return a.step > b.step;
    }
}p, q;

int a, b, c, flag, path[N*20000], vis[N][N];

node BFS ()
{
    priority_queue <node> que;

    p.a = p.b = p.step = 0;
    pots[0][0].prea = pots[0][0].preb = pots[0][0].op = 0;

    met (vis, 0);
    vis[0][0] = 1;

    que.push(p);

    while (que.size())
    {
        q = que.top(); que.pop();

        if (q.a == c || q.b == c)
        {
            flag = 1;
            return q;
        }

        for (int i=1; i<=6; i++)
        {
            if (i == 1) p.a = a, p.b = q.b;//FILL(1);
            if (i == 2) p.a = q.a, p.b = b;//FILL(2);
            if (i == 3) p.a = 0, p.b = q.b;//DROP(1);
            if (i == 4) p.a = q.a, p.b = 0;//DROP(2);
            if (i == 5)//POUR(1,2);
            {
                if (q.a+q.b > b) p.a=q.a-(b-q.b), p.b=b;
                else p.a=0, p.b=q.b+q.a;
            }
            if (i == 6)//POUR(2,1);
            {
                if (q.a+q.b > a) p.a=a, p.b=q.b-(a-q.a);
                else p.a=q.a+q.b, p.b=0;
            }

            if (!vis[p.a][p.b])
            {
                vis[p.a][p.b] = 1;

                pots[p.a][p.b].prea = q.a;
                pots[p.a][p.b].preb = q.b;
                pots[p.a][p.b].op = i;

                p.step = q.step + 1;

                que.push (p);
            }
        }
    }
}

int main ()
{
    node ans;

    while (scanf ("%d %d %d", &a, &b, &c) != EOF)
    {
        met (path, 0);

        flag = 0;

        ans = BFS ();

        if (!flag)
        {
            puts ("impossible");
            continue;
        }

        int step = ans.step, x = ans.a, y = ans.b;

        printf ("%d\n", step);

        for (int i=step; i>=1; i--)
        {
            path[i] = pots[x][y].op;

            int xx = x;
            x = pots[xx][y].prea;
            y = pots[xx][y].preb;
        }

        for (int i=1; i<=step; i++)
        {
            if (path[i] == 1) puts ("FILL(1)");
            if (path[i] == 2) puts ("FILL(2)");
            if (path[i] == 3) puts ("DROP(1)");
            if (path[i] == 4) puts ("DROP(2)");
            if (path[i] == 5) puts ("POUR(1,2)");
            if (path[i] == 6) puts ("POUR(2,1)");
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_大太阳_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值