UVA 10603 (13.07.24)

来源:互联网 发布:黑马就业班java视频 编辑:程序博客网 时间:2024/06/04 00:11

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

 

题意: 有a, b, c三个杯子, 容量不一, 自己输入容量

然后设定一个目标水量, 然后就要进行倒水操作

输出包含两个数, 第一个数是某个杯子达到目标水量后, 总的转移水量是多少?

第二个数特殊点, 如果能达到目标水量, 输出目标水量值, 如果一直操作都达不到目标水量, 输出最接近目标水量的一个值~


解法:

针对水量进行DFS, 设定三个数组, volume(容量), state(当前状态, 即当前杯子里多少水), vis(三个杯子都总状态)


看AC代码把, 不难理解的:


#include<stdio.h>#include<stdlib.h>#include<string.h>int aim;int volume[3];int state[3];int vis[205][205][205];int Min;int flag;int d;void DFS(int num) {for(int i = 0; i < 3; i++) {if(state[i] == aim) {d = aim;if(flag)Min = num;else if(num < Min)Min = num;flag = 0;}else if(flag && state[i] < aim) {if(aim - state[i] < d) {d = aim - state[i];Min = num;}else if(aim - state[i] == d && num < Min)Min = num;}}for(int i = 0; i < 3; i++) {for(int j = 0; j < 3; j++) {if(i != j && state[i] && state[j] != volume[j]) {int t1 = state[i];int t2 = state[j];int add;if(state[i] >= volume[j] - state[j]) {add = volume[j] - state[j];state[i] = state[i] - add;state[j] = volume[j];}else {state[j] = state[j] + state[i];add = state[i];state[i] = 0;}if(vis[state[0]][state[1]][state[2]] == 0) {vis[state[0]][state[1]][state[2]] = 1;DFS(num+add);vis[state[0]][state[1]][state[2]] = 0;}state[i] = t1;state[j] = t2;}}}}int main() {int T;scanf("%d", &T);while(T--) {scanf("%d%d%d%d", &volume[0], &volume[1], &volume[2], &aim);memset(vis, 0, sizeof(vis));state[0] = 0;state[1] = 0;state[2] = volume[2];flag = 1;Min = d = 9998;DFS(0);if(flag == 0)printf("%d %d\n", Min, d);elseprintf("%d %d\n", Min, d);}}


原创粉丝点击