首页 > 代码库 > UVA fill
UVA fill
题目如下:
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 secondjug are initially empty, while the third
is completely filled with water. It is allowed to pour waterfrom one jug into another until either the first one is empty or the second oneis full. This operation can be performed zero, one or more times.
You are to write a program that computes the least totalamount of water that needs to be poured; so that at least one of the jugscontains exactly d liters of water (d is a positive integer not greater than200). If it is not possible to measure d liters this way your program shouldfind 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 theleast total amount of poured water needed to produce d‘ liters in at least oneof 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 lineof input containing four space separated integers - a, b, c and d.
Output
The output consists of two integers separated by a singlespace. The first integer equals the least total amount (the sum of all watersyou 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 theclosest 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 Informatics2003
Problem submitter: Ivaylo Riskov
Problem solution: Ivaylo Riskov, Sadrul Habib Chowdhury
倒水问题,但和经典的有差别。这道题要求倒出指定水量所倒的总水量最少,如果倒不出指定水量,求出能倒出的水量中最接近指定水量的值(小于),并求出相应的所倒的最小水量。不过还是用BFS,用VIS【】【】记录状态,但有两点需要注意:1、用sum【】数组记录当前节点所对应的总倒水量,用V[]数组记录当前杯子的深度对应的总倒水量(便于以后查找)2、当当前状态被访问过(在我的代码中也就是vis[][]!=-1)并不能立刻剪枝,因为如果当前代码所对应的总倒水量比之前同一状态的少还是要继续BFS的,因为这个WA了几次。3、在BFS完成后从d开始向下判断,直到找到被访问过的点(V[i]!=-1)输出即可。注意到这几点然后正常BFS即可。
AC的代码如下:
UVA fill