程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> Fill(倒水問題) UVA10603

Fill(倒水問題) UVA10603

編輯:C++入門知識

Problem D

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 first and the second jug are initially empty, while the third

is completely filled with water. It is allowed to pour water from one jug into another until either the first 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 find 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 first 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 first 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

Sample Output

2

2 3 4 2

96 97 199 62

2 2

9859 62

Problem source: Bulgarian National Olympiad in Informatics 2003

Problem submitter: Ivaylo Riskov

Problem solution: Ivaylo Riskov, Sadrul Habib Chowdhury


一道典型的隱式圖搜索問題,把每次倒水後三個杯子的狀態視為一個狀態結點;然後BFS。

#include
#include
#include

using namespace std;

int val[3],goal,s,flag;
int vis[205][205],front,rear;

class Node
{
public:
    int v[3],dist;
}node[1005];

void bfs()
{
    front=0,rear=1;
        node[0].v[2]=val[2];
        vis[0][0]=1;
        while(front>s;
    while(s--)
    {
        cin>>val[0]>>val[1]>>val[2]>>goal;
        memset(vis,0,sizeof(vis));
        memset(node,0,sizeof(node));
        bfs();
        int i,j;
        if(flag)
        {
            cout<=0;i--)
            {
                for(j=0;j<=rear;j++)
                {
                    if(node[j].v[0]==i||node[j].v[1]==i||node[j].v[2]==i)
                    {
                        flag=1;
                        break;
                    }
                }
                if(flag) break;
            }
            cout<


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved