Pots POJ - 3414

14 篇文章 0 订阅
8 篇文章 0 订阅

一个比较麻烦一点的BFS
题意是有两个容量分别为A,B的空水杯,可以对他们进行如下操作:
1. 选择其中一个水杯倒满水
2. 选择其中一个水杯清空其中的水
3. 选择一个水杯,将其中的水倒入到另一个水杯,直到另一个水杯满或者选择的水杯没有水了

希望通过这些操作使其中的一个水杯中水量为C

整体依然是BFS,只是在寻找下一步可能情况的时候比较麻烦,我直接写了对应操作的三个函数,然后记录下当前节点的操作,和当前节点的父节点,寻得解之后回溯逆序输出答案即可
另外要注意无解的情况,不需要跑BFS,当C不能整除 A,B的公约数时一定无解。
注意BFS时也需要通过visit数组记录下当前已经实现了的情况,不然会超时

//leehaoze
#include <iostream>
#include <deque>
#include <string>
#include <vector>
#include <queue>
#include <cstdio>
#include <stack>
#include <algorithm>
#include <cstring>
#include <cctype>
#include <cstdio>
#include <cmath>
#include <cstdlib>

using namespace std;
const int INF = 1<<29;
#define INC_SAT( val ) (val = ((val)+1 > (val)) ? (val)+1 : (val))
#define ARR_SIZE( a ) ( sizeof( (a) ) / sizeof( (a[0]) ) )
#define ULL unsigned long long

struct Pot{
    Pot(int s,int w):size_(s),water_(w){}
    Pot(const Pot&cp):size_(cp.size_),water_(cp.water_){}
    int size_;
    int water_;
};

struct Comb{
    Comb(Pot A,Pot B,int step,int pre_order):A_(A),B_(B),step_(step),pre_order_(pre_order),father_(NULL){}
    Comb(const Comb&cp):A_(cp.A_),B_(cp.B_),step_(cp.step_){}
    Pot A_;
    Pot B_;
    int step_;
    int pre_order_;
    Comb *father_;
};

int A,B,C;
bool visit[101][101];
void Input(){
    scanf("%d%d%d",&A,&B,&C);
    for (int i = 0; i < 101; ++i) {
        for (int j = 0; j < 101; ++j) {
            visit[i][j] = false;
        }
    }
}

int gcd(int a, int b) {
    return b ? gcd(b, a%b) : a;
}

bool Match(Comb &now){
    return now.A_.water_ == C || now.B_.water_ == C;
}

void Fill(Comb &temp, int i){
    if(i == 1){
        temp.A_.water_ = temp.A_.size_;
    }
    else{
        temp.B_.water_ = temp.B_.size_;
    }
}

void Drop(Comb &temp,int i){
    if(i == 1){
        temp.A_.water_ = 0;
    }
    else{
        temp.B_.water_ = 0;
    }
}

void Pour(Comb &temp,int i){
    if(i == 1){
        int B_left = temp.B_.size_ - temp.B_.water_;
        if(temp.A_.water_ >= B_left){
            temp.B_.water_ = temp.B_.size_;
            temp.A_.water_ -= B_left;
        }
        else{
            temp.B_.water_ += temp.A_.water_;
            temp.A_.water_ = 0;
        }
    }
    else{
        int A_left = temp.A_.size_ - temp.A_.water_;
        if(temp.B_.water_ >= A_left){
            temp.A_.water_ = temp.A_.size_;
            temp.B_.water_ -= A_left;
        }
        else{
            temp.A_.water_ += temp.B_.water_;
            temp.B_.water_ = 0;
        }
    }
}

void BFS(){
    queue<Comb *> S;
    S.push(new Comb(Pot(A,0),Pot(B,0),0,-1));
    while(!S.empty()){
        Comb *now = S.front();
        S.pop();
        if(Match(*now)){
            cout << now->step_ << endl;
            stack<string> ans;
            while(now != NULL){
                switch (now->pre_order_){
                    case 0:
                        ans.push("FILL(1)");
                        break;
                    case 1:
                        ans.push("FILL(2)");
                        break;
                    case 2:
                        ans.push("DROP(1)");
                        break;
                    case 3:
                        ans.push("DROP(2)");
                        break;
                    case 4:
                        ans.push("POUR(1,2)");
                        break;
                    case 5:
                        ans.push("POUR(2,1)");
                        break;
                    default:break;
                }
                now = now->father_;
            }
            while(!ans.empty()){
                cout << ans.top() << endl;
                ans.pop();
            }
            return ;
        }
        for (int i = 0; i < 6; ++i) {
            Comb *temp = new Comb(*now);
            switch (i) {
                case 0:
                    Fill(*temp, 1);
                    break;
                case 1:
                    Fill(*temp, 2);
                    break;
                case 2:
                    Drop(*temp, 1);
                    break;
                case 3:
                    Drop(*temp, 2);
                    break;
                case 4:
                    Pour(*temp, 1);
                    break;
                case 5:
                    Pour(*temp, 2);
                    break;
                default:
                    break;
            }
            temp->step_++;
            temp->pre_order_ = i;
            temp->father_ = now;
            if(!visit[temp->A_.water_][temp->B_.water_]) {
                S.push(temp);
                visit[temp->A_.water_][temp->B_.water_] = true;
            }
        }
    }
}

int main() {
#ifdef LOCAL
    freopen("IN.txt", "r", stdin);
#endif
    std::ios::sync_with_stdio(false);
    Input();
    if (C % gcd(A, B) != 0)
        puts("impossible");
    else
        BFS();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值