[隐式图搜索]Fill(倒水问题) UVA10603
来源:互联网 发布:javascript功能 编辑:程序博客网 时间:2024/05/18 03:18
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 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.
Sample Input
Sample Output
2
2 3 4 2
96 97 199 62
2 2
9859 62
Problem source: Bulgarian National Olympiad in Informatics 2003
Problem submitter: Ivaylo Riskov
Problem solution: Ivaylo Riskov, Sadrul Habib Chowdhury
一道典型的隐式图搜索问题,把每次倒水后三个杯子的状态视为一个状态结点;然后BFS。
#include<iostream>#include<cstring>#include<algorithm>using namespace std;int val[3],goal,s,flag;int vis[205][205],front,rear;class Node{public: int v[3],dist;}node[1005];void bfs(){ front=0,rear=1; node[0].v[2]=val[2]; vis[0][0]=1; while(front<rear) { Node &p=node[front]; if(p.v[0]==goal||p.v[1]==goal||p.v[2]==goal) { flag=1; break; } for(int i=0;i<3;i++) { for(int j=0;j<3;j++) { if(i!=j) { int t=min(p.v[i],val[j]-p.v[j]); Node &d=node[rear]; for(int k=0;k<3;k++) d.v[k]=p.v[k]; d.v[i]=d.v[i]-t; d.v[j]=d.v[j]+t; if(vis[d.v[1]][d.v[2]]==0) { vis[d.v[1]][d.v[2]]=1; d.dist=p.dist+t; rear++; } } } } front++; }}int main(){ cin>>s; while(s--) { cin>>val[0]>>val[1]>>val[2]>>goal; memset(vis,0,sizeof(vis)); memset(node,0,sizeof(node)); bfs(); int i,j; if(flag) { cout<<node[front].dist<<" "<<goal<<endl; } else { for(i=goal-1;i>=0;i--) { for(j=0;j<=rear;j++) { if(node[j].v[0]==i||node[j].v[1]==i||node[j].v[2]==i) { flag=1; break; } } if(flag) break; } cout<<node[j].dist<<" "<<goal<<endl; } } return 0;}
- [隐式图搜索]Fill(倒水问题) UVA10603
- UVA10603 倒水问题
- uva10603 倒水问题
- UVA10603 倒水问题 BFS
- UVa 10603 - Fill,经典倒水问题+隐式图搜索+dfs
- UVA 10603 Fill(状态空间搜索,倒水问题)
- uva10603倒水问题宽度优先遍历!
- UVA10603倒水问题加优先队列
- uva10603 - Fill
- uva10603 Fill
- UVa10603 Fill
- UVa10603 - Fill
- UVa10603 - Fill
- uva10603 Fill
- UVA10603 Fill
- 倒水问题 隐式图搜索问题
- UVA 10603 Fill倒水问题
- 倒水问题(Fill,UVa 10603)
- ubuntu如何调整屏幕亮度
- JDK与JRE内部的一些区别和联系
- 学习鸟哥的Linux私房菜笔记(5)——目录
- 五角星填充FillModesClassical
- 函数中关于“函数声明”“函数定义”“函数原型”的辨析
- [隐式图搜索]Fill(倒水问题) UVA10603
- 在silverlight中EnitityFramework4.1框架下的以codefirst模式进行后台处理的SQL日志记录方法
- 构建多线程应用程序2 Parallel 并行类:Parallel.ForEach()、Task异步操作类:Task.Factory.StartNew()、并行LINQ查询: AsParallel()
- 基数排序的一个例子
- WMI--Windows API--SWbemLocator Object
- 绘制花朵Flower
- 揭秘JavaScript中谜一样的this
- DLL Hell
- ZTE VX10 H618B不增加设备、不拆机、不破解,开启自带路由+iTV的详细设置方法