poj-3414-Pots【BFS+记录路径】


3414-Pots


                Time Limit: 1000MS      Memory Limit: 65536K

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)

题目链接:POJ-3414

题目大意:有A,B两个容器。三种操作:

  1. 清空任一容器内的水。
  2. 加满任一容器的水。
  3. 将A倒水给B(A倒满B或者A倒空)或者B倒水给A

题目思路:直接BFS就可以了。但是难点是记录路径。

我的方法是用一个 father[i][j]数组记录的是 A中水量为 i,B中水量为j的时候它的前一种状态。 然后输出的时候,从最后一个状态一直找到开始状态。

WA了很久,发现vis数组开太小了。eg: 69 87 14 这个样例会RE

以下是代码:

//
//  H.cpp
//  搜索
//
//  Created by pro on 16/3/25.
//  Copyright (c) 2016年 pro. All rights reserved.
//

#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <string>
#include <cstring>
#include <queue>
using namespace std;
int a,b,c;
struct node
{
    int a,b;
    int cnt;
};
node  init(int num1,int num2,int num3)
{
    node tmp;
    tmp.a = num1;
    tmp.b = num2;
    tmp.cnt = num3;
    return tmp;
}
node father[105][105];
int vis[1005][1005];
queue <node> que;
string s[100];
void printf_ans(node ans)
{
    cout << ans.cnt << endl;
    int k = 0;
    while(1)
    {
        if (ans.a == 0 && ans.b == 0) break;
        node pre = father[ans.a][ans.b];
        if (ans.b == pre.b) //b 没有改变
        {
            if (ans.a == 0) s[k++] = "DROP(1)";
            else s[k++] = "FILL(1)";
        }
        else if (ans.a == pre.a)
        {
            if (ans.b == 0) s[k++] = "DROP(2)";
            else s[k++] = "FILL(2)";
        }
        else if ((ans.a == 0 && ans.b != pre.b) || (ans.a != pre.a && ans.b == b))  //把a倒进b
        {
            s[k++] = "POUR(1,2)";
        }
        else
        {
            s[k++] = "POUR(2,1)";
        }
        ans.a = pre.a;
        ans.b = pre.b;
    }
    for (int i = k - 1; i >= 0; i--)
    {
        cout << s[i] << endl;
    }
}
void bfs()
{

    node zero = init(0,0,0);
    que.push(zero);
    vis[0][0] = 1;
    while(!que.empty())
    {
        node front = que.front();
        que.pop();
        node tmp;
        if (front.a == c || front.b == c)
        {
            printf_ans(front);
            return;
        }
        if (front.a != 0 && !vis[0][front.b])  //清空a操作
        {
            tmp = init(0,front.b,front.cnt + 1);
            que.push(tmp);
            vis[0][front.b] = 1;
            father[0][front.b] = front;
        }
        if (front.b != 0 && !vis[front.a][0]) //清空b操作
        {
            tmp = init(front.a,0,front.cnt + 1);
            que.push(tmp);
            vis[front.a][0] = 1;
            father[front.a][0] = front;
        }

        if (front.a != a && !vis[a][front.b]) //倒满a
        {
            tmp = init(a,front.b,front.cnt + 1);
            que.push(tmp);
            vis[a][front.b] = 1;
            father[a][front.b] = front;
        }
        if (front.b != b && !vis[front.a][b]) //倒满b
        {
            tmp = init(front.a,b,front.cnt + 1);
            que.push(tmp);
            vis[front.a][b] = 1;
            father[front.a][b] = front;
        }

        if (front.a > 0 && front.b < b)  //a倒给b
        {
            int need = b - front.b;
            if (front.a > need && !vis[front.a - need][b])  //b满
            {
                tmp = init(front.a - need,b,front.cnt + 1);
                que.push(tmp);
                vis[front.a - need][b] = 1;
                father[front.a - need][b] = front;
            }
            else if (!vis[0][front.b + front.a]) //a倒完
            {
                tmp = init(0,front.b + front.a,front.cnt + 1);
                que.push(tmp);
                vis[0][front.b + front.a] = 1;
                father[0][front.b + front.a] = front;
            }

        }
        if (front.b > 0 && front.a < a)  //b倒给a
        {
            int need = a - front.a;
            if (front.b > need && !vis[a][front.b - need])  //a满
            {
                tmp = init(a,front.b - need,front.cnt + 1);
                que.push(tmp);
                vis[a][front.b - need] = 1;
                father[a][front.b - need] = front;
            }
            else if (!vis[front.a + front.b][0])//b倒完
            {
                tmp = init(front.a + front.b,0,front.cnt + 1);
                que.push(tmp);
                vis[front.a + front.b][0] = 1;
                father[front.a + front.b][0] = front;
            }
        }
    }
    cout << "impossible\n";
}

int main()
{
    while(cin >> a >> b >> c)
    {
        while (!que.empty()) {
            que.pop();
        }
        memset(vis, 0, sizeof(vis));
        memset(father,0,sizeof(father));
        bfs();
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值