HDU-1495 非常可乐(bfs 或数论)

HDU-1495 非常可乐
Time Limit: 1000 ms / Memory Limit: 32768 kb
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
Source
“2006校园文化活动月”之“校庆杯”大学生程序设计竞赛暨杭州电子科技大学第四届大学生程序设计竞赛

这道题是个好题,有两种做法,bfs和数论
bfs就六中状态,a-b,a-c,b-a,b-c,c-a,c-b,用到一个三维数组来防止bfs中原先出现的状态再次出现

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std;
typedef long long LL;
const int inf = 0x3f3f3f3f;
typedef struct Node{
    int a,b,c;
    int step;
}Node;
bool vis[105][105][105];
int main()
{
    int n,m,s;
    while(~scanf("%d %d %d",&s,&n,&m))
    {
        if(n == 0 && m == 0 && s == 0){
            break;
        }
        if(s % 2 != 0)
        {
            printf("NO\n");
            continue;
        }
        Node node;
        node.a = s;
        node.b = 0;
        node.c = 0;
        node.step = 0;
        memset(vis,false,sizeof(vis));
        queue<Node>que;
        que.push(node);
        vis[s][0][0] = true;
        bool flag = false;
        while(!que.empty())
        {
            Node tmp = que.front();
            que.pop();
            Node ans;
            if((tmp.a == s / 2 && tmp.b == s / 2) || (tmp.a == s / 2 && tmp.c == s / 2) || (tmp.b == s / 2 && tmp.c == s / 2))
            {
                printf("%d\n",tmp.step);
                flag = true;
                break;
            }
            if(tmp.a != 0){
                if(tmp.a >= (n - tmp.b)){
                    ans.a = tmp.a - (n - tmp.b);
                    ans.b = n;
                    ans.c = tmp.c;
                    ans.step = tmp.step + 1;
                }
                else{
                    ans.a = 0;
                    ans.b = tmp.b + tmp.a;
                    ans.c = tmp.c;
                    ans.step = tmp.step + 1;
                }
                if(!vis[ans.a][ans.b][ans.c]){
                    vis[ans.a][ans.b][ans.c] = true;
                    que.push(ans);
                }
            }
            if(tmp.a != 0){
                if(tmp.a >= (m - tmp.c)){
                    ans.a = tmp.a - (m - tmp.c);
                    ans.b = tmp.b;
                    ans.c = m;
                    ans.step = tmp.step + 1;
                }
                else{
                    ans.a = 0;
                    ans.b = tmp.b;
                    ans.c = tmp.c + tmp.a;
                    ans.step = tmp.step + 1;
                }
                if(!vis[ans.a][ans.b][ans.c]){
                    vis[ans.a][ans.b][ans.c] = true;
                    que.push(ans);
                }
            }
            if(tmp.b != 0){
                ans.a = tmp.a + tmp.b;
                ans.b = 0;
                ans.c = tmp.c;
                ans.step = tmp.step + 1;
                if(!vis[ans.a][ans.b][ans.c]){
                    vis[ans.a][ans.b][ans.c] = true;
                    que.push(ans);
                }
            }
            if(tmp.b != 0){
                if(tmp.b >= (m - tmp.c)){
                   ans.a = tmp.a;
                   ans.b = tmp.b - (m - tmp.c);
                   ans.c = m;
                   ans.step = tmp.step + 1;
                }
                else{
                    ans.a = tmp.a;
                    ans.b = 0;
                    ans.c = tmp.c + tmp.b;
                    ans.step = tmp.step + 1;
                }
                 if(!vis[ans.a][ans.b][ans.c]){
                    vis[ans.a][ans.b][ans.c] = true;
                    que.push(ans);
                }
            }
            if(tmp.c != 0){
                ans.a = tmp.a + tmp.c;
                ans.b = tmp.b;
                ans.c = 0;
                ans.step = tmp.step + 1;
                if(!vis[ans.a][ans.b][ans.c]){
                    vis[ans.a][ans.b][ans.c] = true;
                    que.push(ans);
                }
            }
            if(tmp.c != 0){
                if(tmp.c >= (n - tmp.b)){
                   ans.a = tmp.a;
                   ans.b = n;
                   ans.c = tmp.c - (n - tmp.b);
                   ans.step = tmp.step + 1;
                }
                else{
                    ans.a = tmp.a;
                    ans.b = tmp.b + tmp.c;
                    ans.c = 0;
                    ans.step = tmp.step + 1;
                }
                 if(!vis[ans.a][ans.b][ans.c]){
                    vis[ans.a][ans.b][ans.c] = true;
                    que.push(ans);
                }
            }
        }
        if(!flag){
            printf("NO\n");
        }
    }
    return 0;
}

数论解法也特别牛逼,感觉数学功底好的人就是厉害
转自:https://blog.csdn.net/v5zsq/article/details/52097459
设两个小瓶子容积分别为a,b,问题转化成通过两个小瓶子的若干次倒进或倒出操作得到(a+b)/2体积的可乐,设两个小瓶子被倒进或倒出x次和y次(这里的x和y是累加后的操作,即x=第一个瓶子倒出的次数-倒进的次数,y=第二个瓶子倒出的次数-倒进的次数),那么问题转化成:
这里写图片描述
所以|x+|y|的最小值为(c+d)/2,通过x和y的通解形式显然可以看出x和y一正一负,不妨设x<0,那么就是往第一个小瓶子倒进x次,第二个小瓶子倒出y次,但是由于瓶子容积有限,所以倒进倒出操作都是通过大瓶子来解决的,一次倒进操作后为了继续使用小瓶子还要将小瓶子中可乐倒回大瓶子中,倒出操作同理,所以总操作次数是(c+d)/2*2=c+d,但是注意最后剩下的(a+b)/2体积的可乐一定是放在两个小瓶子中较大的那个中,而不是再倒回到大瓶子中,所以操作数要减一,答案就是c+d-1

这种理解完问题,列方程组求解,用到了扩展欧几里得求二元一次方程的思想,强强强

#include<bits/stdc++.h>
using namespace std;
int gcd(int a,int b)
{
    return (b == 0) ? a : gcd(b,a % b);
}
int main()
{
    int a,b,c;
    while(~scanf("%d %d %d",&a,&b,&c) && (a || b || c))
    {
        if(a % 2 != 0){
            cout << "NO" << endl;
            continue;
        }
        if(((a / 2) % gcd(b,c)) != 0){
            cout << "NO" << endl;
        }
        else{
            printf("%d\n",(a / gcd(b,c)) - 1);
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_36386435/article/details/80301648