【问题描述】

有两个无刻度标志的水壶,分别可装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) 题解的更多相关文章

  1. 洛谷P1432 倒水问题(CODEVS.1226)

    To 洛谷.1432 倒水问题 题目背景 In the movie "Die Hard 3", Bruce Willis and Samuel L. Jackson were co ...

  2. codevs 1226 倒水问题

    1226 倒水问题 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold   题目描述 Description 有两个无刻度标志的水壶,分别可装 x 升和 y 升 ( x, ...

  3. CodeVS 1226 倒水问题【DFS/BFS】

    题目描述 Description 有两个无刻度标志的水壶,分别可装 x 升和 y 升 ( x,y 为整数且均不大于 100 )的水.设另有一水 缸,可用来向水壶灌水或接从水壶中倒出的水, 两水壶间,水 ...

  4. 计算系数 (codevs 1137) 题解

    [问题描述] 给定一个多项式(ax + by)^k,给定a.b.k.n.m,请求出多项式展开后x^n y^m项的系数. [样例输入] 1 1 3 1 2 [样例输出] 3 [解题思路] 本题为NOIP ...

  5. lightoj 1226 - One Unit Machine(dp+大组合数去摸)

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1226 题解:由于这些任务完成是有先后的所以最后一个完成的肯定是最后一个任务的子 ...

  6. 【线性规划与网络流 24题】已完成(3道题因为某些奇怪的原因被抛弃了QAQ)

    写在前面:SDOI2016 Round1滚粗后蒟蒻开始做网络流来自我拯救(2016-04-11再过几天就要考先修课,现在做网络流24题貌似没什么用←退役节奏) 做的题目将附上日期,见证我龟速刷题. 1 ...

  7. HDU 5881 Tea -2016 ICPC 青岛赛区网络赛

    题目链接 题意:有一壶水, 体积在 L和 R之间, 有两个杯子, 你要把水倒到两个杯子里面, 使得杯子水体积几乎相同(体积的差值小于等于1), 并且使得壶里剩下水体积不大于1. 你无法测量壶里剩下水的 ...

  8. hdu_5881_Tea(xjb猜)

    题目链接:hdu_5881_Tea 题意: 有一壶水, 体积在 L 和 R 之间, 有两个杯子, 你要把水倒到两个杯子里面, 使得杯子水体积几乎相同(体积的差值小于等于1), 并且使得壶里剩下水体积不 ...

  9. code1052 地鼠游戏

    贪心算法,从后往前 来自codevs的题解: 我的纠结思考过程:如果每一秒都没有重复的地鼠出现 那么肯定是一个一个挨着打如果有重复的地鼠 那么要考虑打那个更优 当然是选分值最大的 单纯这样想很合理 但 ...

随机推荐

  1. Remove Duplicates from Sorted List(链表)

    Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...

  2. Hadoop 2.6.0动态添加节点

    文章出自:http://my.oschina.net/leoleong/blog/477508 本文主要从基础准备,添加DataNode和添加NodeManager三个部分详细说明在Hadoop2.6 ...

  3. 条码解析的一片js

    function HIBC_CheckCode(code) {     var nonCheckCode = code.substr(0, code.length - 1);     var arr ...

  4. MongoDB 3: 使用中的问题,及其应用场景

    导读:用MongoDB去存储非关系型的数据,是一个比较正确的选择.但是,如果只是用MongoDB,那么也会出现一些问题.MongoDB,尤其使用的最佳场景,更多的时候,需要结合关系型数据库共同解决问题 ...

  5. session StateServer 方式 初始化StateServer服务器

    1.初始化StateServer服务器启动ASP.NET 状态服务[aspnet_state],该服务默认是手动启动的,可以通过修改注册表,设置为自动启动并允许远程连接.修改方法如下:修改注册表: [ ...

  6. kafka概念

    一.结构与概念解释 1.基础概念 topics: kafka通过topics维护各类信息. producer:发布消息到Kafka topic的进程. consumer:订阅kafka topic进程 ...

  7. leetcode 118

    118. Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, ...

  8. poj2000

    为了凑今天的数,大水题.不解释了,说来惭愧. #include <stdio.h> int main(){ int n; int i,cnt,j; int tot; while(~scan ...

  9. 样式重置 取消input默认样式

    body, h1, h2, h3, h4, h5, h6, hr, p,blockquote, dl, dt, dd, ul, ol, li,pre, form, fieldset, legend, ...

  10. Android IOS WebRTC 音视频开发总结(二三)-- hurtc使用说明

    本文主要介绍如何测试基于浏览器和手机的视频通话程序,转载请说明出处,文章来自博客园RTC.Blacker,更多详见www.blackerteam.com   很多人想测试浏览器(包括浏览器版本和桌面e ...