UVa 10603 - Fill,经典倒水问题+隐式图搜索+dfs
来源:互联网 发布:建筑设计软件图片 编辑:程序博客网 时间:2024/06/06 03:37
题目链接:
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.
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.
样例输入:
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 (转载请标明)
- UVa 10603 - Fill,经典倒水问题+隐式图搜索+dfs
- UVA 10603 Fill倒水问题
- 倒水问题(Fill,UVa 10603)
- 倒水问题(Fill,UVa 10603)
- UVA 10603 Fill(状态空间搜索,倒水问题)
- uva 10603 Fill(倒水问题 BFS)
- Fill (Uva 10603 bfs 倒水问题)
- UVA 10603 Fill(倒水问题)
- [隐式图搜索]Fill(倒水问题) UVA10603
- uva 10603(Fill, 隐式图搜索问题)
- 例题7-8 UVA 10603 Fill 倒水问题
- UVA-10603 Fill 倒水问题 BFS+优先队列
- 例题7-8 倒水问题(Fill, UVa 10603)
- uva 10603倒水问题(搜索 隐式图的最短路 )
- UVA 10603 FILL 隐式图搜索
- UVA - 10603 Fill(隐式图搜索)
- uva 10603 Fill 搜索
- 10603 - Fill,倒水问题 bfs解
- Nutch 2.0 之 抓取流程简单分析
- 用VC6.0编译驱动程序的工程设置方法
- android中实现消息推送
- XML解析详解
- C语言SHELL排序算法
- UVa 10603 - Fill,经典倒水问题+隐式图搜索+dfs
- Ubuntu 11.10 运行QQ、迅雷、MS Office
- Android Map Api 使用和开发(1) 添加地图和界面
- Windows驱动程序的安装(一)
- 创建结构体的三种方式及其使用
- 电子商务创业失败的总结
- 23/7/2012 ICPC培训 第八天
- WDK和VS2012的下载、安装、视频介绍
- C++中函数建立临时变量的机制(const)