HDU - 1495 - 非常可乐 - BFS / 数论

本文介绍了一个经典的编程竞赛问题——如何使用两个无刻度杯子将一瓶可乐平均分成两份,并提供了一种基于广度优先搜索(BFS)的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

非常可乐

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 14827    Accepted Submission(s): 5950


Problem Description
大家一定觉的运动以后喝可乐是一件很惬意的事情,但是seeyou却不这么认为。因为每次当seeyou买了可乐以后,阿牛就要求和seeyou一起分享这一瓶可乐,而且一定要喝的和seeyou一样多。但seeyou的手中只有两个杯子,它们的容量分别是N 毫升和M 毫升 可乐的体积为S (S<101)毫升 (正好装满一瓶) ,它们三个之间可以相互倒可乐 (都是没有刻度的,且 S==N+M,101>S>0,N>0,M>0) 。聪明的ACMER你们说他们能平分吗?如果能请输出倒可乐的最少的次数,如果不能输出"NO"。
 

Input
三个整数 : S 可乐的体积 , N 和 M是两个杯子的容量,以"0 0 0"结束。
 

Output
如果能平分的话请输出最少要倒的次数,否则输出"NO"。
 

Sample Input
  
7 4 3 4 1 3 0 0 0
 

Sample Output
  
NO 3
 

Author
seeyou
 

Source
 

Recommend
LL

你有三个容量分别为S, M, N的无刻度瓶子且保证S == M + N, 初始状态下S装满了可乐,问最少倒多少次能使得S, M, N 中
两个容器各装为S / 2体积的可乐。

直接BFS疯狂赋值黏贴模拟过程就好了,一个容器要倒到另一个容器就两种情况,要不然一个瓶子倒空,要不然另一个瓶子倒满,溢出也好负数也好直接通过一个check剪掉就好了。。就不用一直用if  去 judge了
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<stack>
using namespace std;

int const N = 100010;
int n, m, w;
int arm;
int visa[200], visb[200], visc[200];
int mark;

struct Node
{
    int a;
    int b;
    int c;
    int ans;
};

bool judge(Node x)
{
    if(visa[x.a] && visb[x.b] && visc[x.c]) return false;
    if(x.a > w || x.b > n || x.c > m) return false;
    if(x.a < 0 || x.b < 0 || x.c < 0) return false;
    return true;
}

int bfs(Node x)
{
    queue<Node> q;
    Node now;
    Node next;
    int t;
    int ans = 0;

    q.push(x);
    while(!q.empty()){
        now = q.front();
        q.pop();
        if(now.a == arm && now.b == arm || now.a == arm && now.c == arm || now.b == arm && now.c == arm){
            return now.ans;
        }
        if(judge(now) == false) continue;
        visa[now.a] = 1;
        visb[now.b] = 1;
        visc[now.c] = 1;
        t = n - now.b;  //1 - 2
        next.a = 0;     //1Пе
        next.b = now.b + now.a;
        next.c = now.c;
        next.ans = now.ans + 1;
        q.push(next);
        
        next.a = now.a - t; //2Тњ
        next.b = n;
        next.c = now.c;
        next.ans = now.ans + 1;
        q.push(next);

        t = m - now.c;      //1 - 3
        next.a = 0;     //1Пе
        next.c = now.c + now.a;
        next.b = now.b;
        next.ans = now.ans + 1;
        q.push(next);
        
        next.a = now.a - t; //2Тњ
        next.c = m;
        next.b = now.b;
        next.ans = now.ans + 1;
        q.push(next);

        t = w - now.a;  //2 - 1
        next.b = 0;     //2Пе
        next.a = now.b + now.a;
        next.c = now.c;
        next.ans = now.ans + 1;
        q.push(next);
    
        next.b = now.b - t; //1Тњ
        next.a = w;
        next.c = now.c;
        next.ans = now.ans + 1;
        q.push(next);

        t = m - now.c;      //2 - 3
        next.b = 0;     //2Пе
        next.c = now.c + now.b;
        next.a = now.a;
        next.ans = now.ans + 1;
        q.push(next);

        next.b = now.b - t; //3Тњ
        next.c = m;
        next.a = now.a;
        next.ans = now.ans + 1;
        q.push(next);

        t = w - now.a;  //3 - 1
        next.c = 0;     //3Пе
        next.a = now.c + now.a;
        next.b = now.b;
        next.ans = now.ans + 1;
        q.push(next);

        next.c = now.c - t; //1Тњ
        next.a = w;
        next.b = now.b;
        next.ans = now.ans + 1;
        q.push(next);

        t = n - now.b;      //3 - 2
        next.c = 0;     //3Пе
        next.b = now.c + now.b;
        next.a = now.a;
        next.ans = now.ans + 1;
        q.push(next);
        
        next.c = now.c - t; //2Тњ
        next.b = n;
        next.a = now.a;
        next.ans = now.ans + 1;
        q.push(next);
        
        ans ++;
    }

    return 0;
}

int main()
{
    int a, b;
    Node first;
    int ans;

    while(scanf("%d%d%d", &w, &n, &m) == 3){
        if(w % 2){
            printf("NO\n");
            continue;
        }
        if(!n && !m && !w) break;
        arm = w / 2;
        memset(visa, 0, sizeof(visa));
        memset(visb, 0, sizeof(visb));
        memset(visc, 0, sizeof(visc));
        first.a = w;
        first.b = 0;
        first.c = 0;
        first.ans = 0;
        ans = bfs(first);
        if(ans)  printf("%d\n", ans);
        else printf("NO\n");
    }

    return 0;
}


后来再翻这道题的时候发现有数论的做法,然而我还看不懂。。。代码短的一比。。。。惭愧。。。还有很长的路要走。。。这样的代码才叫做ACM啊
http://m.blog.youkuaiyun.com/article/details?id=52097459



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值