uva10603 倒水问题
状态搜索。类似八数码问题
AC代码
#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=200+5;
int vis[maxn][maxn];//是否访问过该节点
struct node
{
int fill[3]; //当前状态的水的分布
int dist; //当前状态倒水量
bool operator < (const node &p) const //排序
{
return dist>p.dist;
}
};
int ans[maxn],cup[3];
void update_ans(node &u) //细节处理 难点
{
for(int i=0;i<3;++i)
{
int d=u.fill[i];
if(ans[d]<0||u.dist<ans[d]) ans[d]=u.dist;
}
}
void bfs(int a,int b,int c,int d)
{
cup[0]=a,cup[1]=b,cup[2]=c;
node start;
start.fill[0]=0,start.fill[1]=0,start.fill[2]=c;
start.dist=0;
priority_queue<node>q;
q.push(start);
while(!q.empty())
{
node u=q.top(); q.pop();
update_ans(u);
if(ans[d]>=0) break;
//尝试从第i个杯子中向第j个倒水
for(int i=0;i<3;++i)
for(int j=0;j<3;++j)
{
if(i==j) continue;
if(u.fill[i]==0||u.fill[j]==cup[j]) continue;
int pour=min(u.fill[i],cup[j]-u.fill[j]);
node temp;
memcpy(&temp,&u,sizeof(u));
temp.dist=u.dist+pour;
temp.fill[i]-=pour;
temp.fill[j]+=pour;
if(!vis[temp.fill[0]][temp.fill[1]])
{
q.push(temp);
vis[temp.fill[0]][temp.fill[1]]=1;
}
}
}
while(d>=0)
{
if(ans[d]>=0)
{
printf("%d %d\n",ans[d],d);
return ;
}
--d;
}
}
int main()
{
int T,a,b,c,d;
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d%d",&a,&b,&c,&d);
memset(ans,-1,sizeof(ans));
memset(vis,0,sizeof(vis));
bfs(a,b,c,d);
}
return 0;
}
如有不当之处欢迎指出!
uva10603 倒水问题的更多相关文章
- UVa10603 倒水 Fill-状态空间搜索
https://vjudge.net/problem/UVA-10603 There are three jugs with a volume of a, b and c liters. (a, b, ...
- UVA10603 倒水问题 Fill
伫倚危楼风细细,望极春愁,黯黯生天际.草色烟光残照里,无言谁会凭阑意. 拟把疏狂图一醉,对酒当歌,强乐还无味.衣带渐宽终不悔,为伊消得人憔悴.--柳永 题目:倒水问题 网址:https://onlin ...
- 关于BFS和dijkstra(2019.04.20)
我的BFS板子 struct node{/*略*/};//表示一个状态 std::map<node,bool>vis;//判断每个状态是否已访问过 std::queue<node&g ...
- UVa 10603 倒水问题
https://vjudge.net/problem/UVA-10603 题意:三个杯子,倒水问题.找出最少倒水量. 思路:路径寻找问题.不难,暴力枚举. #include<iostream&g ...
- 广度优先搜索 cdoevs 1226 倒水问题
cdoevs 1226 倒水问题 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 有两个无刻度标志的水壶,分别可装 x 升 ...
- 倒水问题 (codevs 1226) 题解
[问题描述] 有两个无刻度标志的水壶,分别可装x升和y升 ( x,y 为整数且均不大于100)的水.设另有一水缸,可用来向水壶灌水或接从水壶中倒出的水, 两水壶间,水也可以相互倾倒.已知x升壶为空壶, ...
- UVa10603 Fill
解题思路:这是神奇的一题,一定要好好体会.见代码: #include<cstdio> #include<cstring> #include<algorithm> # ...
- 【UVA10603】Fill (构图+最短路)
题目: Sample Input22 3 4 296 97 199 62Sample Output2 29859 62 题意: 有三个杯子它们的容量分别是a,b,c, 并且初始状态下第一个和第二个是空 ...
- POJ 3414 Pots【bfs模拟倒水问题】
链接: http://poj.org/problem?id=3414 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22009#probl ...
随机推荐
- python_如何派生内置不可变类型并修改实例化行为
案例: 我们想要自定义新类型的元组,对传入的可迭代对象我们只保留其中的int类型并且值大于0的元素,如下: [1, -2, 'xxx', 7, [1, 'oo'], 9] >> (1, ...
- ECLIPS-S测井系统下的仪器挂接 [CV模块]
常见简写 简写 全拼 含义 CV Calibration and Verification 刻度和校验 CP Primary Calibration 主刻度 VP Primary Verify 主校验 ...
- java.lang.ClassNotFoundException: com.radiadesign.catalina.session.RedisSessionHandlerValve
org.apache.tomcat.util.digester.Digester.startElement Begin event threw exception java.lang.ClassNot ...
- The server's host key is not cached in the registry. You have no guarantee that the server……
使用putty中的pscp.exe ,可以通过脚本方式实现windows向linux上传文件,但pscp.exe第一次运行时必须手工输入确认信息,本文主要解决掉初次运行时的人工交互,彻底实现静默运行. ...
- linkin大话面向对象--类和对象
我们每天在撸码,那么我们在敲什么东西呢?明显的我们在写类,写一个类,写一个接口,写某个接口里面写一些属性,在某个类里面写一个方法,然后以一个对象调用方法,对于j2ee来讲的话,可能还会写一些jsp,静 ...
- MyEclipse中Lombok的安装及使用
lombok是一款通过注解的形式简化我们必须有又显得臃肿的代码的工具.最常用的就是@Data注解.实体类上用了这个注解,实体类的各个属性就不需要书写get和set方法. 安装步骤: 1.关闭Myecl ...
- iOS 设置文本中指定某段文本的颜色 大小
NSString *money = @"300"; NSString *perStr = @"元/时"; NSString *text = [NSString ...
- Java自学能学会吗?最新Java高级学习路线
就好像一千个人心中有一千个哈利波特一样,这个答案不绝对也不唯一,Java好学但自学的确阻力有些大,作为一门技术语言,它所蕴含的智慧绝对不是靠几本XX入门,XX框架之类的书所能概括,自学Java你要做好 ...
- Spring AOP小记
一.概述 在通常的开发过程中,我们调用的顺序通常是controller->service-dao,其中,service中包含着太多的业务逻辑,并且还要不断调用dao来实现自身的业务逻辑,经常会导 ...
- bzoj4326 运输计划
4326: NOIP2015 运输计划 Time Limit: 30 Sec Memory Limit: 128 MB Description 公元 2044 年,人类进入了宇宙纪元.L 国有 n ...