uva 10603 - Fill bfs

10603 - Fill

 

      经典的倒水问题,求最接近的可得到的水量d‘ d'<=d 和需要倒的水量

      因为a,b,c最大只有200,所以总的状态只有8,000,000,而又因为总水量恒定,所以状态量只有40,000,bfs遍历即可。

 

/*
author:jxy
lang:C/C++
university:China,Xidian University
**If you need to reprint,please indicate the source**
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
#define INF 1E9
using namespace std;
struct node
{
    int a[3],p;
};
int cost[202][202];
int Min,up,push;
int A[3],d;
node now,next;
queue<node> q;
int main()
{
    int T,i,j,k,t,*a;
    bool flag=1;
    scanf("%d",&T);
    while(T--)
    {
        while(!q.empty())q.pop();
        memset(cost,0,sizeof(cost));
        flag=1;Min=INF;
        scanf("%d%d%d%d",&A[0],&A[1],&A[2],&d);
        if(A[2]==d||d==0){printf("0 %d\n",d);continue;}
        now.a[0]=0;now.a[1]=0;now.a[2]=A[2];
        q.push(now);cost[0][0]=1;
        while(!q.empty()&&flag)
        {
            now=q.front();q.pop();
            a=now.a;
            for(i=0;i<3&&flag;i++)//倒出杯
            {
                if(now.a[i]==0)continue;
                for(j=0;j<3&&flag;j++)//接受杯,分别是6种倒水情况
                {
                    if(i==j||a[j]==A[j])continue;
                    next.a[i]=max(a[i]-(A[j]-a[j]),0);
                    next.a[j]=a[j]+a[i]-next.a[i];
                    next.a[3-i-j]=a[3-i-j];
                    next.p=cost[now.a[0]][now.a[1]]+a[i]-next.a[i];
                    if(cost[next.a[0]][next.a[1]])continue;
                    for(k=0;k<3;k++)
                    {
                        t=d-next.a[k];
                        if(t<Min&&t>=0){Min=t;push=next.p;up=next.a[k];}
                        if(Min==0){flag=0;break;}
                    }
                    cost[next.a[0]][next.a[1]]=next.p;
                    q.push(next);
                }
            }
        }
        printf("%d %d\n",push-1,up);
    }
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值