POJ 3414 Pots

本文介绍了一个利用BFS算法解决给定两个瓶子容量及目标水量时,找到达到目标状态的最短操作序列的方法。通过详细的输入输出解析,提供了一套完整的解题思路与代码实现。
Pots
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
Submit  Status  Practice  POJ 3414

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)

注:本人英语很渣,题目大意大多来自百度~=0=

 

题目大意:

给出了两个瓶子的容量a,b, 以及一个目标水量n

对A、B可以有如下操作:

FILL(i)        把i倒满

DROP(i)      把i倒空

POUR(i,j)     把i倒入j

问经过哪几个操作后能使得任意一个瓶子的水量为C。

若不可能得到则输出impossible

 

输入就是A(a) B(b) C(n)

输出第一行是一共需要几次操作

下面是每行是一次操作

 

解题思路 :

BFS

每次操作有8种可能(额,我是按照自己题解写的序号 将就着看吧=0=)

a倒入b时:

1.倒满, a剩余

7.倒空,a为0

b倒入a时:

 

2.倒满, b剩余

8.倒空,b为0

当a不满时

3.FILL(a)

当b不满时

4.FILL(b)

当a != 0时

5.倒空a

 

当b != 0时

6.倒空b

 

由于输出需要步骤,所以我用结构体保存了前一个状态的oa, ob, oc(原因是这三个都需要才能找到上一个操作)

 

代码如下:

#include <iostream>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <queue>
#define N 110
#define INF 0xfffffff
using namespace std;
int a, b, n;
bool f[N][N][9]; //f用来判断此状态是不是已经使用过
struct node
{
    int a, b, step, oa, ob, c, oc;//a, b 状态的下标,  c 状态操作 o+..是指前一个状态
}s, e, ans[N][N][9]; //ans用来保存每一个状态的值
void Putc(int c)
{
    if(c == 1 || c == 7) printf("POUR(1,2)\n");
    if(c == 2) printf("DROP(1)\n");
    if(c == 3 || c == 8) printf("POUR(2,1)\n");
    if(c == 4) printf("DROP(2)\n");
    if(c == 5) printf("FILL(1)\n");
    if(c == 6) printf("FILL(2)\n");
}
void inPut(node s)
{
    int d[N], dd = 0;
    printf("%d\n", s.step);
    d[dd++] = s.c;
    while(s.a || s.b) {
        s = ans[s.oa][s.ob][s.oc];
        d[dd++] = s.c; //将之前每一个操作储存起来  最后输出 因为如果直接输出顺序回事反的
    }
    for(int i = dd - 1; i >= 0; i--)
        Putc(d[i]);
}
void Init() //初始化
{
    memset(f, false, sizeof(f));
    memset(ans, 0, sizeof(ans));
}
void BFS()
{
    queue<node>q;
    s = {0, 0, 0};
    q.push(s);
    f[s.a][s.b][s.c] = 1;
    while(!q.empty()) {
        s = q.front();
        q.pop();
        if(s.a == n || s.b == n) {
            inPut(s);
            return;
        }
        if(s.a != 0) {
        //a->b
        if(s.a + s.b > b){//b被倒满a有剩余
            e = {s.a - (b - s.b), b, s.step + 1, s.a, s.b, 1, s.c};
            if(!f[e.a][e.b][e.c])
            q.push(e), f[e.a][e.b][e.c] = 1, ans[e.a][e.b][e.c] = e;
        }
        else {//a倒b  无剩余
            e = {0, s.a + s.b, s.step + 1, s.a, s.b, 7, s.c};
            if(!f[e.a][e.b][e.c])
            q.push(e), f[e.a][e.b][e.c] = 1, ans[e.a][e.b][e.c] = e;
        }
        //倒空a
        e = {0, s.b, s.step + 1, s.a, s.b, 2, s.c};
        if(!f[e.a][e.b][e.c])
        q.push(e), f[e.a][e.b][e.c] = 1, ans[e.a][e.b][e.c] = e;
        }
        if(s.b != 0) {
        //b->a
        if(s.a + s.b > a){//a被倒满b有剩余
            e = {a, s.b - (a - s.a), s.step + 1, s.a, s.b, 3, s.c};
            if(!f[e.a][e.b][e.c])
           q.push(e), f[e.a][e.b][e.c] = 1, ans[e.a][e.b][e.c] = e;
        }
        else {//b倒a  无剩余
            e = {s.a + s.b, 0, s.step + 1, s.a, s.b, 8, s.c};
            if(!f[e.a][e.b][e.c])
            q.push(e), f[e.a][e.b][e.c] = 1, ans[e.a][e.b][e.c] = e;
        }
        //~倒空b
        e = {s.a, 0, s.step + 1, s.a, s.b, 4, s.c};
            if(!f[e.a][e.b][e.c])
            q.push(e), f[e.a][e.b][e.c] = 1, ans[e.a][e.b][e.c] = e;
        }
        //把a加满
        if(s.a != a) {
        e = {a, s.b, s.step + 1, s.a, s.b, 5, s.c};
            if(!f[e.a][e.b][e.c])
            q.push(e), f[e.a][e.b][e.c] = 1, ans[e.a][e.b][e.c] = e;
        }
        //把b加满
        if(s.b != b){
        e = {s.a, b, s.step + 1, s.a, s.b, 6, s.c};
            if(!f[e.a][e.b][e.c])
            q.push(e), f[e.a][e.b][e.c] = 1, ans[e.a][e.b][e.c] = e;
        }
    }
    printf("impossible\n");
}
int main()
{
    while (~scanf("%d %d %d", &a, &b, &n)) {
        Init();
        BFS();
    }
}

 

转载于:https://www.cnblogs.com/wangyuhao/p/4685164.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值