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);
    }
}


 

目录
相关文章
|
算法
1091 zoj Knight Moves的BFS算法和DFS
1091 zoj Knight Moves的BFS算法和DFS
52 0
|
11月前
light oj 1159 - Batman LCS
学过简单动态规划的人应该对最长公共子序列的问题很熟悉了,这道题只不过多加了一条字符串变成三条了,还记得,只要把状态变成三维的即可。
30 0
|
11月前
hdoj 1028/poj 2704 Pascal's Travels(记忆化搜索||dp)
有个小球,只能向右边或下边滚动,而且它下一步滚动的步数是它在当前点上的数字,如果是0表示进入一个死胡同。求它从左上角到右下角到路径数目。 注意, 题目给了提示了,要用64位的整数。
33 0
|
机器学习/深度学习 测试技术
UVA11175 有向图D和E From D to E and Back
UVA11175 有向图D和E From D to E and Back
UVA11175 有向图D和E From D to E and Back
hdu 1312 Red and Black(BFS)
hdu 1312 Red and Black(BFS)
138 0
HDU-1142,A Walk Through the Forest(Dijkstra+DFS)
HDU-1142,A Walk Through the Forest(Dijkstra+DFS)