uva 10603 Fill 搜索

题目:

There are three jugs with a volume of a, b and c liters. (a, b, and c are positive integers not greater than 200). The rst and the second jug 

are initially empty, while the third is completely lled with water. It is allowed to pour water from one jug into another until either the rst one 

is empty or the second one is full. This operation can be performed zero, one or more times.

 

You are to write a program that computes the least total amount of water that needs to be poured; so that at least one of the jugs contains 

exactly d liters of water (d is a positive integer not greater than 200). If it is not possible to measure d liters this way your program should

 nd a smaller amount of water d′ < d which is closest to d and for which d′ liters could be produced. When d′ is found, your program should compute the least total amount of poured water needed to produce d′ liters in at least one of the jugs.

 

Input

 

The rst line of input contains the number of test cases. In the next T lines, T test cases follow. Each test case is given in one line of input 

containing four space separated integers | a, b, c and d.

 

Output

 

The output consists of two integers separated by a single space. The rst integer equals the least total amount (the sum of all waters you 

pour from one jug to another) of poured water. The second integer equals d, if d liters of water could be produced by such transformations, 

or equals the closest smaller value d′ that your program has found.

 

Sample Input

 

2

 

2 3 4 2

 

96 97 199 62

 

Sample Output

 

2 2

 

9859 62

题目大意:

给三个不同大小的杯子,最后一个装满水,前两个不装水。求能否倒成n,

否则输出小于n最大的数。

题目思路:

 

1、题目没什么而特别的。直接优先队列搜索(优先搬运的数量)

 

程序:

 

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<cmath>
#include<algorithm>
#include <map>
#include <queue>
using namespace std;
//#define lint long long int
bool v[210][210][210];
struct node
{
    int z[3],bu;
    friend bool operator < (node a,node b)
    {
        return a.bu>b.bu;
    }
};
int dfs(int *,int);
int main()
{
    int ci;
    scanf("%d",&ci);
    while(ci--)
    {
        memset(v,0,sizeof(v));
        int z[3],m;
        scanf("%d%d%d%d",&z[0],&z[1],&z[2],&m);
        dfs(z,m);
    }
    return 0;
}
int dfs(int *z,int m)
{
   priority_queue <node>qu;
    node a,ans,b;
    ans.z[0]=0,ans.bu=0;
    a.z[0]=0,a.z[1]=0,a.z[2]=z[2],a.bu=0;
    qu.push(a);
    while(!qu.empty())
    {
        //cout<<'!';
        a=qu.top();
        qu.pop();
        if(v[a.z[0]][a.z[1]][a.z[2]])
            continue;
        v[a.z[0]][a.z[1]][a.z[2]]=1;
        if(a.z[0]==m||a.z[1]==m||a.z[2]==m)
        {
            //cout<<a.z[0]<<'\t'<<a.z[1]<<'\t'<<a.z[2]<<'\t'<<m<<endl;
            ans=a;
            ans.z[0]=m;
            break;
        }
        for(int i=0; i<3; i++)
            if(a.z[i]>ans.z[0]&&a.z[i]<m)
            {
                //cout<<'@';
                ans.z[0]=a.z[i];
                ans.bu=a.bu;
            }
        for(int i=0; i<3; i++)
            for(int j=0; j<3; j++)
                if(i!=j)
                    if(a.z[i]&&a.z[j]<z[j])
                    {
                        int t=min(z[j]-a.z[j],a.z[i]);
                        b.z[j]=a.z[j]+t;
                        b.z[i]=a.z[i]-t;
                        b.z[3-i-j]=a.z[3-i-j];
                        b.bu=a.bu+t;
                        qu.push(b);
                    }
    }
    printf("%d %d\n",ans.bu,ans.z[0]);
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值