倒水问题 (codevs 1226) 题解
【问题描述】
有两个无刻度标志的水壶,分别可装x升和y升 ( x,y 为整数且均不大于100)的水。设另有一水缸,可用来向水壶灌水或接从水壶中倒出的水, 两水壶间,水也可以相互倾倒。已知x升壶为空壶, y升壶为空壶。问如何通过倒水或灌水操作, 用最少步数能在x或y升的壶中量出 z(z ≤ 100)升的水来。
【样例输入】
3 22 1
【样例输出】
14
【解题思路】
看到求最少步数,马上想到用广度优先搜索,那么问题在于如何展开?要不要剪枝?其实,这道题是不需要任何剪枝的,只要判重就行了,那么关键就在于如何展开。
对于x壶和y壶,设它们壶中各装了a,b升水,那么一共有以下六种操作:
若a>0且b<y,那么可以用x向y中倾倒min(a,y-b)升水,此时,x中剩余a-min(a,y-b)升水,y中剩余b+min(a,y-b)升水,同理,当a<x,b>0时可以反操作。
若a>0时,可将x向水缸中倒出a升水,此时x中没有水,y中不变,若b>0时也可反操作。
若a<x时,可从水缸中倒出x-a升水至x中,此时x中有x升水,y中不变,b<y时也可反操作。
根据这六种操作,我们如何拓展的问题就解决了。
【代码实现】
uses math;
type rec=record
dep,x,y:longint;
end;
var a:array[..] of rec;
f:array[..,..] of boolean;
h,r,x,y,z,xx,yy:longint;
begin
readln(x,y,z);
fillchar(f,sizeof(f),true);
f[,]:=false;
h:=;r:=;
while h<>r do
begin
inc(h);
if (a[h].x>=)and(a[h].y<=y) then
begin
inc(r);
a[r]:=a[h];
a[r].dep:=a[h].dep+;a[r].x:=a[h].x-min(a[h].x,y-a[h].y);a[r].y:=a[h].y+min(a[h].x,y-a[h].y);
if not(f[a[r].x,a[r].y]) then
dec(r)
else
f[a[r].x,a[r].y]:=false;
end;
if (a[r].x=z)or(a[r].y=z) then
begin
writeln(a[r].dep);
exit;
end;
if (a[h].x<=x)and(a[h].y>=) then
begin
inc(r);
a[r]:=a[h];
a[r].dep:=a[h].dep+;a[r].x:=a[h].x+min(a[h].y,x-a[h].x);a[r].y:=a[h].y-min(a[h].y,x-a[h].x);
if not(f[a[r].x,a[r].y]) then
dec(r)
else
f[a[r].x,a[r].y]:=false;
end;
if (a[r].x=z)or(a[r].y=z) then
begin
writeln(a[r].dep);
exit;
end;
if a[h].x>= then
begin
inc(r);
a[r]:=a[h];
a[r].dep:=a[h].dep+;a[r].x:=;
if not(f[a[r].x,a[r].y]) then
dec(r)
else
f[a[r].x,a[r].y]:=false;
end;
if (a[r].x=z)or(a[r].y=z) then
begin
writeln(a[r].dep);
exit;
end;
if a[h].x<=x then
begin
inc(r);
a[r]:=a[h];
a[r].dep:=a[h].dep+;a[r].x:=x;
if not(f[a[r].x,a[r].y]) then
dec(r)
else
f[a[r].x,a[r].y]:=false;
end;
if (a[r].x=z)or(a[r].y=z) then
begin
writeln(a[r].dep);
exit;
end;
if a[h].y>= then
begin
inc(r);
a[r]:=a[h];
a[r].dep:=a[h].dep+;a[r].y:=;
if not(f[a[r].x,a[r].y]) then
dec(r)
else
f[a[r].x,a[r].y]:=false;
end;
if (a[r].x=z)or(a[r].y=z) then
begin
writeln(a[r].dep);
exit;
end;
if a[h].y<=y then
begin
inc(r);
a[r]:=a[h];
a[r].dep:=a[h].dep+;a[r].y:=y;
if not(f[a[r].x,a[r].y]) then
dec(r)
else
f[a[r].x,a[r].y]:=false;
end;
if (a[r].x=z)or(a[r].y=z) then
begin
writeln(a[r].dep);
exit;
end;
end;//六种展开方式,每次展开后都要判重,然后看是否达到目标
writeln('impossible');
end.
倒水问题 (codevs 1226) 题解的更多相关文章
- 洛谷P1432 倒水问题(CODEVS.1226)
To 洛谷.1432 倒水问题 题目背景 In the movie "Die Hard 3", Bruce Willis and Samuel L. Jackson were co ...
- codevs 1226 倒水问题
1226 倒水问题 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 有两个无刻度标志的水壶,分别可装 x 升和 y 升 ( x, ...
- CodeVS 1226 倒水问题【DFS/BFS】
题目描述 Description 有两个无刻度标志的水壶,分别可装 x 升和 y 升 ( x,y 为整数且均不大于 100 )的水.设另有一水 缸,可用来向水壶灌水或接从水壶中倒出的水, 两水壶间,水 ...
- 计算系数 (codevs 1137) 题解
[问题描述] 给定一个多项式(ax + by)^k,给定a.b.k.n.m,请求出多项式展开后x^n y^m项的系数. [样例输入] 1 1 3 1 2 [样例输出] 3 [解题思路] 本题为NOIP ...
- lightoj 1226 - One Unit Machine(dp+大组合数去摸)
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1226 题解:由于这些任务完成是有先后的所以最后一个完成的肯定是最后一个任务的子 ...
- 【线性规划与网络流 24题】已完成(3道题因为某些奇怪的原因被抛弃了QAQ)
写在前面:SDOI2016 Round1滚粗后蒟蒻开始做网络流来自我拯救(2016-04-11再过几天就要考先修课,现在做网络流24题貌似没什么用←退役节奏) 做的题目将附上日期,见证我龟速刷题. 1 ...
- HDU 5881 Tea -2016 ICPC 青岛赛区网络赛
题目链接 题意:有一壶水, 体积在 L和 R之间, 有两个杯子, 你要把水倒到两个杯子里面, 使得杯子水体积几乎相同(体积的差值小于等于1), 并且使得壶里剩下水体积不大于1. 你无法测量壶里剩下水的 ...
- hdu_5881_Tea(xjb猜)
题目链接:hdu_5881_Tea 题意: 有一壶水, 体积在 L 和 R 之间, 有两个杯子, 你要把水倒到两个杯子里面, 使得杯子水体积几乎相同(体积的差值小于等于1), 并且使得壶里剩下水体积不 ...
- code1052 地鼠游戏
贪心算法,从后往前 来自codevs的题解: 我的纠结思考过程:如果每一秒都没有重复的地鼠出现 那么肯定是一个一个挨着打如果有重复的地鼠 那么要考虑打那个更优 当然是选分值最大的 单纯这样想很合理 但 ...
随机推荐
- JBPM的引擎内核学习
http://atongyeye.iteye.com/blog/2093505 流程引擎 http://www.cnblogs.com/aspnetx/archive/2009/09/24/15735 ...
- 使用JDBC构建简单的数据访问层
本教程的目的是使用Java编写的分离的层去访问数据库中的表,这一层通常称为数据访问层(DAL) 使用DAL的最大好处是通过直接使用一些类似insert()和find()的方法简化了数据库的访问操作,而 ...
- Loadrunner 添加windows资源没反应
使用 LoadRunner Controller 添加Windows资源系统没有反应, 解决办法 : 1.关闭Windows 防火墙 2.若使用的不是本机 1) 首先要启动所监测机器的remote r ...
- 06-CABasicAnimation基础核心动画
*:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...
- OpenWRT TP_LINK703N 校园网 锐捷认证解决办法
OpenWRT TP_LINK703N 校园网 锐捷认证解决办法 一.准备的工具 1) SSH登录工具,推荐使用MobaXterm_Personal下载链接https://moba.en.s ...
- Type-base dispatch
In the previous section we added two Time objects, but you also might want to add an integer to a Ti ...
- UVa11054 Gergovia的酒交易 Wine trading in Gergovia-递推
https://vjudge.net/problem/UVA-11054 As you may know from the comic “Asterix and the Chieftain’s Shi ...
- sql实现分页
IF EXISTS(SELECT * FROM sysobjects WHERE name='usp_getPage') DROP PROC usp_getPage GO CREATE PROC us ...
- Leetcode027. Remove Element
//water class Solution { public: int removeElement(vector<int>& nums, int val) { for(vecto ...
- WF4 常用类<第二篇>
一.WorkflowInvoker 常用方法如下: 方法 说明 BeginInvoke() 使用指定的 AsyncCallback 和用户提供的状态以异步方式调用工作流 EndInvoke() 返回使 ...