POJ 3414 Pots 均分水(bfs)

15 篇文章 0 订阅
9 篇文章 0 订阅

题目链接:

POJ 3414 Pots

题目大意:

有两个水杯,一开始都没有装水,给出两个水杯的容量,和要达到的目标容量
每个水被有三种操作:1.装满水,2.倒掉所有水,3,将水倒入另一个杯子中.

思路:

两个杯子,六种操作,,求最短路,并打印路径,6入口bfs,跟非常可乐一题类似

Description

You are given two pots, having the volume of A and B liters respectively. The >following operations can be performed:

FILL(i) fill the pot i (1 ≤ i ≤ 2) from the tap;
DROP(i) empty the pot i to the drain;
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)

代码:

/*************************************************************************
        > File Name: poj_3414.cpp
      > Author: dulun
      > Mail: dulun@xiyoulinux.org
      > Created Time: 2016年04月13日 星期三 12时59分54秒
 ************************************************************************/

#include<iostream>
#include<stdio.h>
#include<cstring>
#include<cstdlib>
#include<queue>
#include<algorithm>
#define LL long long
using namespace std;

const int N = 186;
struct Node
{
    int a, b, step;
    char str[N][N];
};

bool v[N][N];

void bfs(int a, int b, int c)
{
    memset(v, 0, sizeof(v));
    Node t, s;
    queue<Node> q;
    t = (Node){0, 0, 0};
    q.push(t);
    v[t.a][t.b] = true;

    while(!q.empty())
    {
        t = q.front();
        q.pop();
        v[t.a][t.b] = true;
        if(t.a == c || t.b == c)
        {
            printf("%d\n", t.step);
            for(int i = 1; i <= t.step; i++)
            {
                printf("%s\n", t.str[i]);
            }
            return ;
        }

        if(t.a == 0)
        {
            s = t;
            s.a = a;
            s.step++;
            strcpy(s.str[s.step], "FILL(1)");
            if(!v[s.a][s.b])
            {
                v[s.a][s.b] = true;
                q.push(s);
            }
        }
        else if(t.a <= a)
        {
            s = t;
            s.a = 0;
            s.step++;
            strcpy(s.str[s.step], "DROP(1)");
            if(!v[s.a][s.b])
            {
                v[s.a][s.b] = true;
                q.push(s);
            }
            if(t.b < b)
            {
                s = t;
                if(t.a+t.b <= b)
                {
                    s.a = 0;
                    s.b = t.a+t.b;
                }
                else{
                    s.a = t.a - (b-t.b);
                    s.b = b;
                }
                s.step++;
                strcpy(s.str[s.step], "POUR(1,2)");
                if(!v[s.a][s.b])
                {
                    v[s.a][s.b] = true;
                    q.push(s);
                }
            }
        }

        if(t.b == 0)
        {
            s = t;
            s.b = b;
            s.step++;
            strcpy(s.str[s.step], "FILL(2)");
            if(!v[s.a][s.b])
            {
                v[s.a][s.b] = true;
                q.push(s);
            }
        }
        else if(t.b <= b)
        {
            s = t;
            s.b = 0;
            s.step++;
            strcpy(s.str[s.step], "DROP(2)");
            if(!v[s.a][s.b])
            {
                v[s.a][s.b] = true;
                q.push(s);
            }
            if(t.a < a)
            {
                if(t.a+t.b < a)
                {
                    s = t;
                    s.a = t.a+t.b;
                    s.b = 0;
                }
                else{
                    s = t;
                    s.a = a;
                    s.b = t.b - (a-t.a);
                }
                s.step++;
                strcpy(s.str[s.step], "POUR(2,1)");
                if(!v[s.a][s.b])
                {
                    v[s.a][s.b] = true;
                    q.push(s);
                }
            }
        }

    }
    printf("impossible\n");
    return;

}

int main()
{
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);

    bfs(a, b, c);
    return 0;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值