HDU - 1495:非常可乐(BFS + 最短次数)

题目链接
话说我有一个容量为S L的杯子,里面装满了从map里买的啤酒,庆庆也想喝,所以他拿来了两个容量分别为N L和M L的杯子,根据我和他的继承关系有一个显然的结论S=N+M,他想用这两个杯子和我的杯子把这个啤酒等分成两份,考虑到他是一个非数论选手以及他的高智商他显然不知道怎么分,所以请来机智的你来帮帮他,如果能够均分的话请输出最少操作次数,否则输出NO

Input
多组用例,每组三个整数S,N,M(0<S,N,M<=100,S=N+M),以0 0 0结束输入

Output
对于每组用例,如果可以等分则输出最少操作次数,否则输出NO

Sample Input
7 4 3

4 1 3

0 0 0

Sample Output
NO

3

Hint
第二组用例,先用4L的杯子将3L的杯子装满,然后用装了3L的杯子往1L的杯子里倒1L,然后把这1L倒回4L的杯子中,所以答案是3

题目描述

将一瓶可乐使用两个杯子的作用下,分为2份

思路:首先如果可乐含量是奇数L的话,那么就无法平分,其次要求最短操作次数,那么就使用广搜将可能的操作都存入队列中,第一个满足条件的就是最少的操作次数,需要注意的时,操作方式有6种,需要一一判断,如果可以就入队

代码如下

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <queue>

using namespace std;

int book[105][105][105],a,b,c;

struct node
{
    int a,b,c,step;
};

void dfs()
{
    queue<node> q;
    node now, next;
    now.a = a, now.b = 0, now.c = 0, now.step = 0;
    q.push(now);
    book[now.a][0][0] = 1;
    while(!q.empty())
    {
        now = q.front();
        q.pop();
        //在每次循环开始判断一次是否满足条件
        if((now.a==a/2&&now.b==a/2)||(now.a==a/2&&now.c==a/2)||(now.b==a/2&&now.c==a/2))
        {
            printf("%d\n",now.step);
            return;
        }
        //a倒入b
        if(now.a != 0)
        {
            if(now.a > b-now.b)
            {
                next.a = now.a - (b-now.b);
                next.b = b;
                next.c = now.c;
            }
            else
            {
                next.a = 0;
                next.b = now.b + now.a;
                next.c = now.c;
            }
            next.step = now.step + 1;
            if(!book[next.a][next.b][next.c])
            {
                book[next.a][next.b][next.c] = 1;
                q.push(next);
            }
        }
        //a倒入c
        if(now.a != 0)
        {
            if(now.a > c-now.c)
            {
                next.a = now.a - (c-now.c);
                next.b = now.b;
                next.c = c;
            }
            else
            {
                next.a = 0;
                next.b = now.b;
                next.c = now.c + now.a;
            }
            next.step = now.step + 1;
            if(!book[next.a][next.b][next.c])
            {
                book[next.a][next.b][next.c] = 1;
                q.push(next);
            }
        }
        //b倒入a
        if(now.b != 0)
        {
            if(now.b > a-now.a)
            {
                next.a = a;
                next.b = now.b - (a-now.a);
                next.c = now.c;
            }
            else
            {
                next.a = now.a + now.b;
                next.b = 0;
                next.c = now.c;
            }
            next.step = now.step + 1;
            if(!book[next.a][next.b][next.c])
            {
                book[next.a][next.b][next.c] = 1;
                q.push(next);
            }
        }
        //b倒入c
        if(now.b != 0)
        {
            if(now.b > c-now.c)
            {
                next.a = now.a;
                next.b = now.b - (c-now.c);
                next.c = c;
            }
            else
            {
                next.a = now.a;
                next.b = 0;
                next.c = now.c + now.b;
            }
            next.step = now.step + 1;
            if(!book[next.a][next.b][next.c])
            {
                book[next.a][next.b][next.c] = 1;
                q.push(next);
            }
        }
        //c倒入a
        if(now.c != 0)
        {
            if(now.c > a-now.a)
            {
                next.a = a;
                next.b = now.b;
                next.c = now.c - (a-now.a);
            }
            else
            {
                next.a = now.a + now.c;
                next.b = now.b;
                next.c = 0;
            }
            next.step = now.step + 1;
            if(!book[next.a][next.b][next.c])
            {
                book[next.a][next.b][next.c] = 1;
                q.push(next);
            }
        }
        //c倒入b
        if(now.c != 0)
        {
            if(now.c > b-now.b)
            {
                next.a = now.a;
                next.b = b;
                next.c = now.c - (b-now.b);
            }
            else
            {
                next.a = now.a;
                next.b = now.b + now.c;
                next.c = 0;
            }
            next.step = now.step + 1;
            if(!book[next.a][next.b][next.c])
            {
                book[next.a][next.b][next.c] = 1;
                q.push(next);
            }
        }
    }
    printf("NO\n");
}

int main()
{
    while(~scanf("%d %d %d",&a,&b,&c) && a && b && c)
    {
        memset(book, 0, sizeof(book));
        if(a%2 == 1)
            printf("NO\n");
        else
            dfs();
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值