UVa10603 倒水 Fill-状态空间搜索
https://vjudge.net/problem/UVA-10603
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 2 2 3 4 2 96 97 199 62
Sample Output 2 2 9859 62
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
;
struct Node{
int x,y,z;
int water;
bool operator<(const Node& b)const{
return water>b.water;
}
};
int a,b,c,d,ans[maxn],done[maxn][maxn];
void init(){
memset(ans,-,sizeof(ans));
memset(done,,sizeof(done));
}
int main()
{
int T;
scanf("%d",&T);
while(T--){
priority_queue<Node> Q;
init();
scanf("%d %d %d %d",&a,&b,&c,&d);
Node start=(Node){,,c,};
Q.push(start);
while(!Q.empty()){
Node r=Q.top();Q.pop();
||r.water<ans[r.x]) ans[r.x]=r.water;
||r.water<ans[r.y]) ans[r.y]=r.water;
||r.water<ans[r.z]) ans[r.z]=r.water;
done[r.x][r.y]=;
) break;
int change;
change=min(r.x,b-r.y);
if(change&&!done[r.x-change][r.y+change]) Q.push((Node){r.x-change,r.y+change,r.z,r.water+change});
change=min(r.x,c-r.z);
if(change&&!done[r.x-change][r.y]) Q.push((Node){r.x-change,r.y,r.z+change,r.water+change});
change=min(r.y,a-r.x);
if(change&&!done[r.x+change][r.y-change]) Q.push((Node){r.x+change,r.y-change,r.z,r.water+change});
change=min(r.y,c-r.z);
if(change&&!done[r.x][r.y-change]) Q.push((Node){r.x,r.y-change,r.z+change,r.water+change});
change=min(r.z,a-r.x);
if(change&&!done[r.x+change][r.y]) Q.push((Node){r.x+change,r.y,r.z-change,r.water+change});
change=min(r.z,b-r.y);
if(change&&!done[r.x][r.y+change]) Q.push((Node){r.x,r.y+change,r.z-change,r.water+change});
}
){
){
printf("%d %d\n",ans[d],d);
break;
}
--d;
}
}
;
}
UVa10603 倒水 Fill-状态空间搜索的更多相关文章
- UVa 1343 The Rotation Game (状态空间搜索 && IDA*)
题意:有个#字型的棋盘,2行2列,一共24个格. 如图:每个格子是1或2或3,一共8个1,8个2,8个3. 有A~H一共8种合法操作,比如A代表把A这一列向上移动一个,最上面的格会补到最下面. 求:使 ...
- UVa 11212 Editing a Book (IDA* && 状态空间搜索)
题意:你有一篇n(2≤n≤9)个自然段组成的文章,希望将它们排列成1,2,…,n.可以用Ctrl+X(剪切)和Ctrl+V(粘贴)快捷键来完成任务.每次可以剪切一段连续的自然段,粘贴时按照顺序粘贴.注 ...
- UVA10603 倒水问题 Fill
伫倚危楼风细细,望极春愁,黯黯生天际.草色烟光残照里,无言谁会凭阑意. 拟把疏狂图一醉,对酒当歌,强乐还无味.衣带渐宽终不悔,为伊消得人憔悴.--柳永 题目:倒水问题 网址:https://onlin ...
- 状态空间搜索好题UVA10603
题目 分析:注意这里求的是最少流量, 二不是最少步数!!!所以我们用优先队列去维护一个最小流量,然后进行bfs即可,解释一下一个重要的数组ans[i],表示的是杯子中的水为i时的最小流量 #inclu ...
- uva10603 倒水问题
状态搜索.类似八数码问题 AC代码 #include<cstdio> #include<queue> #include<cstring> #include<a ...
- 【UVA10603】Fill (构图+最短路)
题目: Sample Input22 3 4 296 97 199 62Sample Output2 29859 62 题意: 有三个杯子它们的容量分别是a,b,c, 并且初始状态下第一个和第二个是空 ...
- UVA - 11212 Editing a Book(IDA*算法+状态空间搜索)
题意:通过剪切粘贴操作,将n个自然段组成的文章,排列成1,2,……,n.剪贴板只有一个,问需要完成多少次剪切粘贴操作可以使文章自然段有序排列. 分析: 1.IDA*搜索:maxn是dfs的层数上限,若 ...
- 用BFS和DFS解决圆盘状态搜索问题
人工智能课程的实验(我的解法其实更像是算法课程的实验) 用到的算法:深度优先搜索.宽度优先搜索(状态扩展的不同策略) 数据结构:表示状态的结构体.多维数组 (可能是最近做算法竞赛题的影响,这次并不像以 ...
- 7-10Editing aBook uva11212(迭代加深搜索 IDA*)
题意: 给出n( 2<=n<=9) 个乱序的数组 要求拍成升序 每次 剪切一段加上粘贴一段算一次 拍成1 2 3 4 ...n即可 求排序次数 典型的状态空间搜索问题 ...
随机推荐
- 嵌入式设计模式:有限状态自动机的C语言实现
转自:http://www.cnblogs.com/autosar/archive/2012/06/22/2558604.html 状态机模式是一种行为模式,在<设计模式>这本书中对其有详 ...
- 学习WordPress必须知道的函数(转)
WordPress是目前十分流行的独立博客程序,因傻瓜化安装和使用,其在网民中的应用已近乎普及.但也因为很多新入门的用户几乎对WordPress 程序没有任何了解,造成使用中碰到问题无法解决,求助也十 ...
- jquery.cookie.js存与取以及过期时间设置
$(function(){ $(".active_out .abtn").click(function(){ $(this).parents(".active_out&q ...
- [JS]Javascript的this用法
转自:阮一峰 this是Javascript语言的一个关键字. 它代表函数运行时,自动生成的一个内部对象,只能在函数内部使用.比如, function test(){ this.x = 1; } 随着 ...
- Spark on Yarn 架构解析
. 一.Hadoop Yarn组件介绍: 我们都知道yarn重构根本的思想,是将原有的JobTracker的两个主要功能资源管理器 和 任务调度监控 分离成单独的组件.新的架构使用全局管理所有应用程序 ...
- bug_ _fragment_“The specified child already has a parent. You must call removeView"的解决以及产生的原因
这个异常的出现往往是因为非法使用了某些方法引起的. 从字面意思上是说这个特定的child已经有一个parent了,你必须在这个parent中首先调用removeView()方法,才能继续你的内容.这里 ...
- 一步步优化JVM五:优化延迟或者响应时间(1)
http://blog.csdn.net/zhoutao198712/article/details/7791969 本节的目标是做一些优化以满足对应用对延迟的需求.这次需要几个步骤,包括完 ...
- queue 与 vector
优先队列是队列的一种,不过它可以按照自定义的一种方式(数据的优先级)来对队列中的数据进行动态的排序 每次的push和pop操作,队列都会动态的调整,以达到我们预期的方式来存储. 例如:我们常用的操作就 ...
- 如何使用javadoc
package com.frank.chapter1; // object.Documentation1.java // TIJ4 Chapter Object, Exercise 13 - 1 /* ...
- 庭审全程文字实录 z
备受关注的深圳快播公司涉黄案两日来在北京市海淀法院开庭审理,快播CEO王欣(微博).事业部总经理吴铭.事业部副总经理张克东.事业部副总经理兼市场部总监牛文举出庭接受审理. 面对传播淫秽物品牟利罪的指控 ...