UVa 10603 - Fill,经典倒水问题+隐式图搜索+dfs
题目链接:
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=110&page=show_problem&problem=1544
类型: 隐式图搜索
原题:
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.
OutputThe 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.
样例输入:
2
2 3 4 2
96 97 199 62
样例输出:
2 2
9859 62
题目大意:
有三个杯子它们的容量分别是a,b,c, 并且初始状态下第一个和第二个是空的, 第三个杯子是满水的。可以把一个杯子的水倒入另一个杯子,当然,当被倒的杯子满了或者倒的杯子水完了,就不能继续倒了。
你的任务是写一个程序计算出用最少的倒水量,使得其中一个杯子里有d升水。如果不能倒出d升水的话,那么找到一个d' < d ,使得d' 最接近d。
分析与总结:
因为共有3个水杯, 根据每一杯的水量v1,v2,v3, 可以得到一个状态state(v1,v2,v3);
为了方便进行dfs搜索的状态转移,可以用两个三维数组volume[3], state[3]分别表示三个杯子的容量和状态。然后会有倒水的动作,可以从第1个杯子倒入第2,3个,从第2个倒入第1,3个等等……所以用两个for循环,可以遍历出所有的倒水方案。
然后注意一些不能倒水的条件,比如要倒的杯子是空的,目标杯子是满的,则不进行倒水的动作。
网上解题报告的方法: 用bfs做
// dfs,隐式图搜索// Time: 0.072 s (UVA)#include<iostream>#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){// 更新结果的状态,有不少地方需要注意 for(int i=0; i<3; ++i){ if(state[i] && state[i] == d){ d1 = d; if(!flag) minVolume = tot; // 第一次发现等于d,那么直接把当前总量tot赋给minVolume else if(tot<minVolume) minVolume = tot; // 以后有其他情况等于d的,只有tot小于minVolume才更新 flag=true; // 标志为已经找到等于d的了 } else if(!flag && state[i] && state[i] < d){// 注意: !flag表示只有没有找到等于d的情况,才会执行下面这些语句 if(d-state[i]<d1){ d1 = d-state[i]; minVolume = tot; } else if(d-state[i]==d1 && tot<minVolume) minVolume = tot; } } for(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]; // 备份,回溯后要恢复 if(state[i] >= volume[j]-state[j]){ // 如果倒的水大于等于被倒杯子剩余容量,那么将被倒满 add = volume[j]-state[j]; state[i] -= add; state[j] = volume[j]; } else{ // 否则,全部倒光到目标杯子里 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; // 回溯,恢复状态 } state[i] = tmp_i; // 回溯,恢复状态 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;}
—— 生命的意义,在于赋予它意义。
原创 http://blog.csdn.net/shuangde800 , By D_Double (转载请标明)