Topic Link:
Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=110&page=show_ problem&problem=1544
Type: Implicit graph Search
Original title:
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 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 ' one is empty or the ' second one is full. This operation can is performed zero, one or more times.
You are are to write a program which computes the least total amount of water, that needs to be poured; So this is in least one of the jugs contains exactly d liters of water (d is a positive integer not greater than 200). If It isn't possible to measure D liters this way your program should find a smaller amount of water d ' < d which are C Losest to D of which d ' liters could be produced. When d ' are found, your program should compute the least total amount of poured water-d ' needed in-lea St one of the jugs.
Input
The "a" of input contains the number of test cases. In the next t lines, t test cases follow. Each test case is given into one line of the input containing four space separated integers-a, B, C and D.
Output
The output consists of two integers separated by a. The total equals the least total amount (the sum of all waters your pour from one jug to another) of poured water. The second integer equals D, if d liters of water could is produced by such transformations, or equals the closest smaller Value d ' that your has found.
Sample input:
2
2 3 4 2
96 97 199 62
Sample output:
2 2
9859 62
The main effect of the topic:
There are three cups. Their capacity is a,b,c, and the first and second are empty in the initial state, and the third Cup is full of water. You can pour a cup of water into another cup, of course, when the poured glass is full or the glass is finished, it cannot continue to pour.
Your task is to write a program that calculates the minimum amount of water that can be used to make a glass with D-liter. If you can't pour the D-liter, then find a d ' < D that makes d ' the closest d.
Analysis and Summary:
Because there are 3 cups, according to each cup of water v1,v2,v3, you can get a state (V1,V2,V3);
In order to facilitate the state transfer of DFS search, two three-dimensional array volume[3], state[3, can be used to represent the capacity and state of three cups respectively. Then there will be the action of inverted water, can be poured from the 1th Cup 2nd, 3, from the 2nd into the 1th, 3 and so on ... So with two for loops, you can iterate through all the water pouring schemes.
Then pay attention to some conditions can not be poured water, such as the glass to be poured is empty, the target Cup is full, then do not pour water action.
The method of solving the problem on the Internet: doing with BFS
DFS, implicit graph search//url:http://www.bianceng.cn/programming/sjjg/201410/45697.htm//time:0.072 s (UVA) #include <iostrea
m> #include <cstdio> #include <cstring> using namespace std;
int d, volume[3], state[3], Minvolume, D1;
BOOL flag, vis[205][205][205]; void search (int tot) {//update the status of the result, there are many places to note for (int i=0; i<3; ++i) {if (state[i) && state[i
] = = d) {D1 = D; if (!flag) minvolume = tot; The first discovery equals D, then directly assigns the current total amount tot to minvolume else if (tot<minvolume) minvolume = tot; Later there are other circumstances equal to D, only tot less than minvolume to update the flag=true; Flag is already found equal to D of the} else if (!flag && state[i] && State[i] < D) {//NOTE:!
Flag that the following statements will execute if (d-state[i]<d1) {D1 = D-state[i] Only if the case is not found to be equal to D;
Minvolume = tot; else if (d-state[i]==d1 && tot<minvolume) Minvolume = Tot (int i=0; i<3; ++i) {for (int j=0; j<3; ++j) if (i!=j && state[i] &&
STATE[J]!=VOLUME[J]) {int add; int tmp_i = State[i], Tmp_j = State[j]; Backup, back to restore if (State[i] >= volume[j]-state[j]) {//If the inverted water is greater than or equal to the remaining capacity of the cup, then it will be filled with add =
VOLUME[J]-STATE[J];
State[i] = add;
STATE[J] = Volume[j];
else{//Otherwise, all the light into the target cup state[j] + = State[i];
add = State[i];
State[i] = 0; } if (!vis[state[0]][state[1]][state[2]]) {vis[state[0]][state[1]][state[2]
] = true;
Search (Tot+add); VIS[STATE[0]][STATE[1]][STATE[2]] = false; Backtracking, recovery status} state[i] = tmp_i;
Backtracking, restoring state state[j] = Tmp_j; }
}
}
int main () {int T;
scanf ("%d", &t);
while (t--) {scanf ("%d%d%d%d", &volume[0],&volume[1],&volume[2],&d);
State[0]=0, State[1]=0, state[2]=volume[2];
memset (Vis, 0, sizeof (VIS));
VIS[0][0][VOLUME[2]] = true;
Flag = false;
Minvolume = D1 = 1000000;
Search (0);
if (flag) printf ("%d%d\n", Minvolume, D1);
else printf ("%d%d\n", Minvolume, D-D1);
return 0; }