倒水问题 bfs (例:uva 10603)

uva 10603
问题大意

倒水问题一般有三个杯子, 容量分别为a, b, c,最初只有第3个杯子装满了c升水,其他两个杯子为空,最少需要几次操作能让某一个杯子水有d升,如果达不到d升,就选最接近d升的,输出最少倒水量和目标水量。

分析

设某一个时刻三个杯子状态为(i,j, k),那么bfs搜索六个方向,即三个杯子相互倒水即可。

代码有详细注释:

#include <cstdio>
#include <vector>
#include <queue>
#include <cstring>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <iostream>
#include <algorithm>
#include <functional>
#include <stack>
#include <ctime>
#include <cstdlib>
//#pragma comment (linker, "/STACK:256000000")
using namespace std;
#define db(x) cout<<x<<endl

typedef long long ll;
const int N = 200 + 10;
const int INF = 0x3f3f3f;

struct node
{
    int v[3];   //三个杯子水量
    int dist;   //移动总水量
    bool operator < (const node & rhs) const {
        return dist > rhs.dist;     //优先队列按dist排序从小到大
    }
};

int vis[N][N];    //当两个杯子水量确定时,第三个杯子水量也随之确定,故只需要二维标记即可
int cap[3];
int ans[N];         //ans[i]数组代表到达i状态的花费dist值

void update_ans(const node& u)
{
    for (int i = 0; i < 3; i++){
        int d = u.v[i];                         //保存到达的每个状态d
        if(ans[d] < 0 || u.dist < ans[d])     //如果没有到达过d状态 或者上一次到达d状态花费的dist比较大
            ans[d] = u.dist;                    //更新ans数组的每一个d值
    }
}

void solve(int a, int b, int c, int d)          
{
    cap[0] = a, cap[1] = b, cap[2] = c;
    memset(vis, 0, sizeof(vis));
    memset(ans, -1, sizeof(ans));
    priority_queue<node> q;

    node start;         //初始状态进入优先对列
    start.dist = 0;
    start.v[0] = 0;
    start.v[1] = 0;
    start.v[2] = c;
    q.push(start);

    vis[0][0] = 1;
    while(!q.empty()){
        node u = q.top();       //取出队列头部元素
        q.pop();
        update_ans(u);          //更新ans数组
        if(ans[d] > 0)          
            break;
        for (int i = 0; i < 3; i++)         //有六种可能的倒水方式 a->b a->c b->a b->c c->a c->b
            for (int j = 0; j < 3; j++)     //for循环代表从i倒给j
            if(i != j){                     //不能自己倒给自己
                if(u.v[i] == 0 || u.v[j] == cap[j])   //如果i杯子没水,或者j杯子满了,倒不了
                    continue;                                                       
                int amount = min(cap[j], u.v[i] + u.v[j]) - u.v[j];     //这样不用分开讨论能不能装满j杯
                node u2;                                
                memcpy(&u2, &u, sizeof(u));         //从u复制到u2
                u2.dist = u.dist + amount;
                u2.v[i] -= amount;
                u2.v[j] += amount;
                if(!vis[u2.v[0]][u2.v[1]]){
                    vis[u2.v[0]][u2.v[1]] = 1;
                    q.push(u2);
                }
            }
    }
    while(d >= 0){
        if(ans[d] >= 0){
            printf("%d %d\n", ans[d], d);
            return;
        }
        d--;
    }
}

int main()
{
    int T, a, b, c, d;
    scanf("%d", &T);
    while(T--){
        scanf("%d%d%d%d", &a, &b, &c, &d);
        solve(a, b, c, d);
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值