您的位置:首页 > 其它

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

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

解题思路:

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

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

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

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

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

Description

You are given two pots, having the volume of [b]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)


#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;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: