POJ -3414 Pots(BFS + 打印路径)

题意:有A,B两个体积的两个罐子,可以进行装满水, 倒空水,和相互倒水的操作,要求进行操作凑出体积为C, 求最少操作次数, 并打印操作步骤。

解题思路:

1。三种操作,两个罐子,共有六种操作情况, 把六中情况映射为0-5五个数字,用字符串保存6种操作。

2.定义结构体,结构为a,b, act,a为a罐子当前的水量,b为b罐子当前的水量,act为本次的操作!! 

3.定义pre[a][b]数组储存水量变为为a,b的前一个节点(这样就保存了上一个节点的act,即上一次的操作)

4.分水满, 水空, 不满不空,进行分类, 话说我代码写的长是因为代码的复用率不高, 写完一个情况复制粘贴改改字母(— —)!

5.最后就打印最短步数,和操作就行了

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)
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<list>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>

using namespace std;

#define MAX_N 200
const int INF = 0x3f3f3f3f;
int a, b, c;
int d[MAX_N][MAX_N];
bool vis[MAX_N][MAX_N];

char str[6][15] =
{
    "FILL(1)",//0 a接水
    "FILL(2)",//1 b接水
    "POUR(1,2)",// 2 a->b
    "POUR(2,1)",// 3 b->a
    "DROP(1)",//4 a放水
    "DROP(2)"//5 b放水
};
struct node
{
    int a, b;
    int act;//本次的操作
};
node pre[MAX_N][MAX_N];

void print_ans(node p)
{
    int step = d[p.a][p.b];
        cout<<step <<endl;
    vector<node> v;
    while(d[p.a][p.b] != 0)
    {
//        cout<<p.a<<' '<<p.b<<endl;
//        cout<<d[p.a][p.b]<<endl;
        v.push_back(p);
        p = pre[p.a][p.b];
    }
    for(int i = v.size() -1 ; i >= 0 ; i--)
    {
        int tmp = v[i].act;
        printf("%s\n", str[tmp]);
    }

}

void bfs(int a, int b)
{
    memset(vis, false, sizeof(vis));
    memset(d, -1, sizeof(d));
    queue<node> que;
    vis[0][0] = true;
    d[0][0] = 0;
    node s;
    s.a =0;
    s.b =0;
    que.push(s);
    while(!que.empty())
    {
        node p = que.front();
        que.pop();
        node p2;
        if(p.a == c || p.b == c)
        {
            print_ans(p);
            return;
        }
        if( p.a != a)//a非满可接水
        {
            p2.a = a;
            p2.b = p.b;
            p2.act = 0;
            if(!vis[p2.a][p2.b])
            {
                vis[p2.a][p2.b] = true;
                d[p2.a][p2.b] = d[p.a][p.b] + 1;
                pre[p2.a][p2.b] = p;
                que.push(p2);
            }
        }
        if(p.b != b)
        {
            p2.a = p.a;
            p2.b = b;
            p2.act = 1;
            if(!vis[p2.a][p2.b])
            {
                vis[p2.a][p2.b] = true;
                d[p2.a][p2.b] = d[p.a][p.b] + 1;
                pre[p2.a][p2.b] = p;
                que.push(p2);
            }
        }
        if(p.a != 0)//a非空可放水
        {
            p2.a = 0;
            p2.b = p.b;
            p2.act = 4;
            if(!vis[p2.a][p2.b])
            {
                vis[p2.a][p2.b] = true;
                d[p2.a][p2.b] = d[p.a][p.b] + 1;
                pre[p2.a][p2.b] = p;
                que.push(p2);
            }
        }
        if(p.b != 0)
        {
            p2.a = p.a;
            p2.b = 0;
            p2.act = 5;
            if(!vis[p2.a][p2.b])
            {
                vis[p2.a][p2.b] = true;
                d[p2.a][p2.b] = d[p.a][p.b] + 1;
                pre[p2.a][p2.b] = p;
                que.push(p2);
            }
        }
        if(p.a != 0 && p.b != b)//a非空,b非满 a->b倒水
        {
            if(p.a + p.b <= b)
            {
                p2.a = 0;
                p2.b = p.a + p.b;
            }
            else
            {
                p2.a = p.a - (b - p.b);
                p2.b = b;
            }
            p2.act = 2;
            if(!vis[p2.a][p2.b])
            {
                vis[p2.a][p2.b] = true;
                d[p2.a][p2.b] = d[p.a][p.b] + 1;
                pre[p2.a][p2.b] = p;
                que.push(p2);
            }
        }
        if(p.b != 0 && p.a != a)
        {
            if(p.a + p.b <= a)
            {
                p2.a = p.a + p.b;
                p2.b = 0;
            }
            else
            {
                p2.a = a;
                p2.b = p.b - (a - p.a);
            }
            p2.act = 3;
            if(!vis[p2.a][p2.b])
            {
                vis[p2.a][p2.b] = true;
                d[p2.a][p2.b] = d[p.a][p.b] + 1;
                pre[p2.a][p2.b] = p;
                que.push(p2);
            }
        }
    }
    cout<<"impossible"<<endl;
    return;
}

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

    bfs(a, b);

    return 0;


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值