hdu 1495 非常可乐(广搜)

题目地址

题目大意:给出三个数s,n,m,(s=m+n)初始态为s装满可乐,n和m为空,求是否能将可能均分,若能输出倒可乐的最小次数

解题思路:对于3个杯中每个杯子有可乐时用队列将状态加入队尾,且标记当前状态,直到队列中有均分的情况停止

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <list>
#include <set>

using namespace std;

const int maxn = 100+10;

int visit[maxn][maxn][maxn];
int s,n,m;

struct Node
{
    int s,n,m,step;
};

int check(int x,int y,int z)
{
    if(x==0 && y==z)    return 1;
    if(y==0 && x==z)    return 1;
    if(z==0 && x==y)    return 1;
    return 0;
}
int bfs()
{
    queue<Node> q;
    Node a,next;
    a.s = s,a.n = 0,a.m = 0,a.step = 0;
    visit[s][0][0] = 1;
    q.push(a);
    while(!q.empty())
    {
        a = q.front();
        q.pop();
        if(check(a.s,a.n,a.m))  return a.step;
        if(a.n)//n还有
        {
            if(a.n>=s-a.s)//n能将s倒满,将n倒入s装满s
            {
                next = a;
                next.n = next.n-(s-a.s);//剩余的n
                next.s = s;//s满了
                if(!visit[next.s][next.n][next.m])
                {
                    next.step = a.step+1;
                    q.push(next);
                    visit[next.s][next.n][next.m] = 1;
                }
            }
            else //n不能将s倒满,将n全部倒入s中
            {
                next = a;
                next.s = next.n+next.s;
                next.n = 0;
                if(!visit[next.s][next.n][next.m])
                {
                    next.step = a.step+1;
                    q.push(next);
                    visit[next.s][next.n][next.m] = 1;
                }
            }
            if(a.n>=m-a.m)//n能将m倒满,将n倒入m装满m
            {
                next = a;
                next.n = next.n-(m-a.m);
                next.m =  m;
                if(!visit[next.s][next.n][next.m])
                {
                    next.step = a.step+1;
                    q.push(next);
                    visit[next.s][next.n][next.m] = 1;
                }
            }
            else//n不能将m倒满,将n全部倒入m中
            {
                next = a;
                next.m = next.n+next.m;
                next.n = 0;
                if(!visit[next.s][next.n][next.m])
                {
                    next.step = a.step+1;
                    q.push(next);
                    visit[next.s][next.n][next.m] = 1;
                }
            }
        }
        if(a.m)//m还有
        {
            if(a.m>=s-a.s)//m能将s倒满,将m倒入s装满s
            {
                next = a;
                next.m = next.m-(s-a.s);
                next.s = s;
                if(!visit[next.s][next.n][next.m])
                {
                    next.step = a.step+1;
                    q.push(next);
                    visit[next.s][next.n][next.m] = 1;
                }
            }
            else //m不能将s倒满,将m全部倒入s中
            {
                next = a;
                next.s = next.m+next.s;
                next.m = 0;
                if(!visit[next.s][next.n][next.m])
                {
                    next.step = a.step+1;
                    q.push(next);
                    visit[next.s][next.n][next.m] = 1;
                }
            }
            if(a.m>=n-a.n)//m能将n倒满,将m倒入n装满n
            {
                next = a;
                next.m = next.m-(n-a.n);
                next.n =  n;
                if(!visit[next.s][next.n][next.m])
                {
                    next.step = a.step+1;
                    q.push(next);
                    visit[next.s][next.n][next.m] = 1;
                }
            }
            else//m不能将n倒满,将m全部倒入n中
            {
                next = a;
                next.n = next.n+next.m;
                next.m = 0;
                if(!visit[next.s][next.n][next.m])
                {
                    next.step = a.step+1;
                    q.push(next);
                    visit[next.s][next.n][next.m] = 1;
                }
            }
        }
        if(a.s)//s还有
        {
            if(a.s>=m-a.m)//s能将m倒满,将s倒入m装满m
            {
                next = a;
                next.s = next.s-(m-a.m);
                next.m = m;
                if(!visit[next.s][next.n][next.m])
                {
                    next.step = a.step+1;
                    q.push(next);
                    visit[next.s][next.n][next.m] = 1;
                }
            }
            else //S不能将m倒满,将s全部倒入m中
            {
                next = a;
                next.m = next.m+next.s;
                next.s = 0;
                if(!visit[next.s][next.n][next.m])
                {
                    next.step = a.step+1;
                    q.push(next);
                    visit[next.s][next.n][next.m] = 1;
                }
            }
            if(a.s>=n-a.n)//s能将n倒满,将s倒入n装满n
            {
                next = a;
                next.s = next.s-(n-a.n);
                next.n =  n;
                if(!visit[next.s][next.n][next.m])
                {
                    next.step = a.step+1;
                    q.push(next);
                    visit[next.s][next.n][next.m] = 1;
                }
            }
            else//s不能将n倒满,将s全部倒入n中
            {
                next = a;
                next.n = next.n+next.s;
                next.s = 0;
                if(!visit[next.s][next.n][next.m])
                {
                    next.step = a.step+1;
                    q.push(next);
                    visit[next.s][next.n][next.m] = 1;
                }
            }
        }
    }
    return 0;
}
int main()
{
    int ans;
    while(scanf("%d%d%d",&s,&n,&m) != EOF)
    {
        if(!(s+n+m))    break;
        if(s%2)
        {
            printf("NO\n");
            continue;
        }
        memset(visit,0,sizeof(visit));
        ans = bfs();
        if(ans)
            printf("%d\n",ans);
        else
            printf("NO\n");
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值