POJ 3414 Pots 记录路径的搜索

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)

题目大意:两个水壶,给出各自容量,开始都没有水,问经过如上几种操作后,如何能在最少操作下到达其中一个水壶中的水为c升。并输出该操作过程。

 

题目难点在于如何保存路径,这里的路径不单单是常数,而是一个操作。

这里采用map<data,data> 把后一个点指向前一个点,类似一棵树的多个分支,最后可以通过不断往回找 可以到达最初的点

即 初始状态。

注意:map 用到结构体时候 需要自定义 比较 bool operator < (DataType data) const

 

#include<iostream>
#include<stdio.h>
#include<vector>
#include<map>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<set>
#include<cmath>
#include<queue>
using namespace std;

int a,b,c;
struct Pot{
    int a,b;
    string s;
    //必须 指定 结构体的 大小比较 map 需要
    bool operator < (Pot o)const {
        if(a != o.a)
            return a < o.a;
        else
            return b < o.b;
    }
};
map<Pot,Pot> mp;

void bfs(int s_a, int s_b){
    Pot pot,tmp;
    queue<Pot> q;
    tmp.b = tmp.a = -1;
    pot.a = s_a; pot.b = s_b;
    mp[pot] = tmp;
    q.push(pot);
    while(!q.empty()){
        pot = q.front();
        q.pop();
        if(pot.a == c || pot.b == c){
            break;
        }
        // fill a
        tmp.a = a; tmp.b = pot.b;tmp.s = "FILL(1)";
        if(mp.find(tmp) == mp.end())
            mp[tmp] = pot,q.push(tmp);
        //fill b
        tmp.a = pot.a; tmp.b = b; tmp.s = "FILL(2)";
        if(mp.find(tmp) == mp.end())
            mp[tmp] = pot,q.push(tmp);
        // drop a
        tmp.a = 0; tmp.b = pot.b; tmp.s ="DROP(1)";
        if(mp.find(tmp) == mp.end())
            mp[tmp] = pot,q.push(tmp);
        // drop b
        tmp.a = pot.a; tmp.b = 0; tmp.s = "DROP(2)";
        if(mp.find(tmp) == mp.end())
            mp[tmp] = pot,q.push(tmp);
        //pour a to b
        tmp.a = max(0,pot.a+pot.b-b);
        tmp.b = min(pot.a+pot.b,b);
        tmp.s = "POUR(1,2)";
        if(mp.find(tmp) == mp.end())
            mp[tmp] = pot,q.push(tmp);
        // pour b to a
        tmp.a = min(a,pot.a+pot.b);
        tmp.b = max(0,pot.a+pot.b-a);
        tmp.s = "POUR(2,1)";
        if(mp.find(tmp) == mp.end())
            mp[tmp] = pot,q.push(tmp);
    }

    if(pot.a == c || pot.b == c){
        int step = 0;
        string ans = "";
        // p.a == 0 && p.b == 0 时候则为初始状态  结束
        for(Pot p = pot; p.a || p.b; p = mp[p]){
            ++step;
            ans = p.s + "\n" + ans;
        }
        cout << step << endl;
        cout << ans <<endl;
        return ;
    }
    cout << "impossible" << endl;
    return;
}

int main()
    {
        ios::sync_with_stdio(false);
        cin >> a >> b >> c;
        bfs(0,0);

        return 0;
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值