题目背景

In the movie "Die Hard 3", Bruce Willis and Samuel L. Jackson were confronted with the following puzzle. They were given a 3-gallon jug and a 5-gallon jug and were asked to fill the 5-gallon jug with exactly 4 gallons. This problem generalizes that puzzle.

在电影“虎胆龙威3-纽约大劫案”中,布鲁斯·威利斯和杰里米·艾恩斯遇到这样一个难题:给他们一个3加仑水壶和一个5加仑水壶,要求在5加仑水壶里准确装入4加仑的水。真是个难题呢。

//恩可以不用在意这个,看看题目描述的翻译就行了。

题目描述

You have two jugs, A and B, and an infinite supply of water. There are three types of actions that you can use: (1) you can fill a jug, (2) you can empty a jug, and (3) you can pour from one jug to the other. Pouring from one jug to the other stops when the first jug is empty or the second jug is full, whichever comes first. For example, if A has 5 gallons and B has 6 gallons and a capacity of 8, then pouring from A to B leaves B full and 3 gallons in A.

A problem is given by a triple (Ca,Cb,N), where Ca and Cb are the capacities of the jugs A and B, respectively, and N is the goal. A solution is a sequence of steps that leaves exactly N gallons in jug B. The possible steps are

fill A fill B empty A

empty B

pour A B

pour B A

success

where "pour A B" means "pour the contents of jug A into jug B", and "success" means that the goal has been accomplished.

You may assume that the input you are given does have a solution.

假定两个水壶A和B,供水量不限。可以使用三种方法装水:

给一个水壶装水;

把一个水壶倒空;

从一个水壶倒进另一个水壶。

当从一个水壶倒进另一个水壶时,如果第一个水壶倒空,或者第二个水壶装满就不能再倒了。例如,一个水壶A是5加仑和另一个水壶B是6加仑,水量是8加仑,则从水壶A倒进水壶B时,让水壶B充满水而水壶A剩3加仑水。

问题由3个参数:Ca,Cb和N,分别表示水壶A和B的容量,目标水量N。解决问题的目标是,给出一系列倒水的步骤,使水壶B中的水量恰好是N。

“pour A B”,表示将水从水壶A倒进水壶B;“success”表示目标已经完成。

我们假定每个输入都有一个解决方案。

//可能有多解,但是洛谷目前不支持spj,所以评测结果仅供参考。

输入输出格式

输入格式:

Input to your program consists of a series of
input lines each defining one puzzle. Input for each puzzle is a single
line of three positive integers: Ca, Cb, and N. Ca and Cb are the
capacities of jugs A and B, and N is the goal. You can assume 0 < Ca
<= Cb and N <= Cb <=1000 and that A and B are relatively prime
to one another.

输入有多行,每行都是一个难题。每个难题有三个正整数:Ca,Cb和N。假设0<Ca≤Cb和N≤Cb≤1000,且A和B互质。

输出格式:

Output from your program will consist of a
series of instructions from the list of the potential output lines which
will result in either of the jugs containing exactly N gallons of
water. The last line of output for each puzzle should be the line
"success". Output lines start in column 1 and there should be no empty
lines nor any trailing spaces.

输出是由一系列倒水操作构成的,其目标是实现水壶B中有N加仑的水。最后一行是“success”;从第1列开始输出,行末没有空格。

输入输出样例

输入样例#1:

3 5 4
5 7 3
输出样例#1:

fill B
pour B A
empty A
pour B A
fill B
pour B A
success
fill A
pour A B
fill A
pour A B
empty B
pour A B
success
分析:看到这道题不知道为啥我想到了迭代加深......事实证明,宽搜就好了,相当于走迷宫一样,把每种可能都扩展一下,只是状态的记录比较麻烦.我在一个结构体中用一个string,每次直接加上操作字符串,顺便加上\n,不过麻烦的是会在输出success之前又输出一个\n,于是我就一位一位地输出就好了.string这个东西如果不会用的话会有很多奇怪的问题,在用之前一定要先初始化一下!能用char就不要用string.
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <string> using namespace std; int a,b,n,vis[][]; struct node
{
int ca,cb;
string op;
};
queue <node>q; void zhuangtai(int x,int y,string o,node p)
{
if (!vis[x][y])
{
vis[x][y] = ;
node temp;
temp.ca = x;
temp.cb = y;
temp.op = p.op + o;
q.push(temp);
}
} void print(node x)
{
//cout << x.op << endl;
for (int i = ; i < x.op.size(); i++)
cout << x.op[i];
cout << "success" << endl;
return;
} void bfs()
{
while (!q.empty())
q.pop();
memset(vis,,sizeof(vis));
node t;
t.ca = ;
t.cb = ;
t.op = "";
q.push(t);
vis[][] = ;
while (!q.empty())
{
node u = q.front();
q.pop();
if (u.cb == n)
{
print(u);
return;
}
zhuangtai(u.ca,b,"fill B\n",u);
zhuangtai(a,u.cb,"fill A\n",u);
zhuangtai(,u.cb,"empty A\n",u);
zhuangtai(u.ca,,"empty B\n",u);
int minn = min(u.ca,b - u.cb);
zhuangtai(u.ca - minn,u.cb + minn,"pour A B\n",u);
minn = min(a - u.ca,u.cb);
zhuangtai(u.ca + minn,u.cb - minn,"pour B A\n",u);
}
} int main()
{
while (scanf("%d%d%d",&a,&b,&n) == )
bfs(); return ;
}

洛谷P1432 倒水问题的更多相关文章

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

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

  2. 洛谷 P1432 倒水问题

    目录 题目 思路 \(Code\) 题目 戳 思路 \(bfs\) 第一遍提交\(50\),第二遍就\(100\)了,qwq \(Code\) #include<iostream> #in ...

  3. 洛谷 P1582 倒水 解题报告

    P1582 倒水 题目描述 一天,CC买了N个容量可以认为是无限大的瓶子,开始时每个瓶子里有1升水.接着~~CC发现瓶子实在太多了,于是他决定保留不超过K个瓶子.每次他选择两个当前含水量相同的瓶子,把 ...

  4. 洛谷P1582 倒水

    P1582 倒水 题目描述 一天,CC买了N个容量可以认为是无限大的瓶子,开始时每个瓶子里有1升水.接着~~CC发现瓶子实在太多了,于是他决定保留不超过K个瓶子.每次他选择两个当前含水量相同的瓶子,把 ...

  5. 洛谷P1582 倒水 二进制 lowbit __builtin_popcount

    P1582 倒水:https://www.luogu.org/problemnew/show/P1582 题意: 给定n瓶装有1升的水瓶,每次可以把两瓶装水量相同的水和成一瓶,问最少还要增加几瓶装有1 ...

  6. 洛谷P1582 倒水题解

    题目 分析 这个题并不难,只是需要仔细思考我们首先可以很轻松的把这个题给疏通一下题意. 1:首先我们最后每个瓶子中装的水一定是一个$2^x$,因为每次都是$2$倍的加,这个应该很好理解. 2:我们要明 ...

  7. 洛谷 - P1582 - 倒水 - 位运算

    https://www.luogu.org/problemnew/show/P1582 要求用最少的瓶子,那肯定不能有两个一样的瓶子,否则合并更优. 枚举其二进制位,每次加上lowbit,将最后一个1 ...

  8. 洛谷 P1582 倒水

    题目描述 一天,CC买了N个容量可以认为是无限大的瓶子,开始时每个瓶子里有1升水.接着~~CC发现瓶子实在太多了,于是他决定保留不超过K个瓶子.每次他选择两个当前含水量相同的瓶子,把一个瓶子的水全部倒 ...

  9. 洛谷 P1582 倒水 (二进制)

    这道题实际上是考二进制 很容易看出杯子水量一定是2的i次方 所以n杯水最后剩下的水一定是n用二进制表示中1的个数 所以就枚举n来求什么时候1的个数小于k 那么这里有个优化,不然会超时 因为每次加的目的 ...

随机推荐

  1. P2345 奶牛集会andP2657 低头一族

    做法是一样的 题目背景 MooFest, Open 题目描述 约翰的N 头奶牛每年都会参加“哞哞大会”.哞哞大会是奶牛界的盛事.集会上的活动很 多,比如堆干草,跨栅栏,摸牛仔的屁股等等.它们参加活动时 ...

  2. SQL系列函数——数学函数

    1.abs函数取数值表达式的绝对值 ) 结果是40 2.ceiling函数取大于等于指定表达式的最小整数 select CEILING(40.5) 结果是41 3.floor函数取小于等于指定表达式的 ...

  3. 使用gitblit 在windows平台搭建git服务器

    1.下载jdk,安装并且配置好环境变量 2.下载gitblit 直接解压无需安装 3.配置gitblit 1.修改gitblit安装目录下的data文件下的gitblit.properties.将in ...

  4. 洛谷P2770 航空路线问题(费用流)

    题意 $n$个点从左向右依次排列,有$m$条双向道路 问从起点到终点,再从终点回到起点,在经过的点不同的情况下最多能经过几个点 Sol 首先,问题可以转化为求两条互不相交的路径,使得点数最多 为了满足 ...

  5. 《精通css》笔记

    第2章    选择器,注释 1.要知道常用选择器(id选择器,类选择器,类型选择器,后代选择器,伪类选择器(文档结构之外)) 通用选择器(*{    }) 高级选择器(子选择器,相邻同胞选择器,属性选 ...

  6. 关于Android发送短信获取送达报告的问题

    最近公司开发一个项目,要求app能够发送短信并获取送达报告.这本不是一个什么难题,实现这一功能的代码一搜一大把,那么这么简单的一个问题,为什么我要在这里提出来呢?那是因为我在写代码的时候掉入了一个坑, ...

  7. sql server 收缩日志文件

    USE 数据库名称 GO ALTER DATABASE 数据库名称 SET RECOVERY SIMPLE WITH NO_WAIT GO ALTER DATABASE 数据库名称 SET RECOV ...

  8. java生成饼图svg

    package com.tellhow.svg; import java.io.File;import java.io.FileOutputStream; /** *  * @author 风絮NO. ...

  9. DB9串口引脚定义

    在单片机串口通信中,使用3根信号线就能够实现通信:RXD,TXD,GND. 经常使用的RS232串口线使用DB9端子. DB9分为公头和母头两种: 一般使用时,引脚定义如下: 连接方式: 注:RXD- ...

  10. postman使用--接口的关联

    前戏 在实际接口测试过程中,接口经常会有关联,比如需要取上一个接口的返回值,然后作为参数传递给下一个接口作为参数,假设我们要获取A接口返回的userid值作为B接口的请求参数 先设置环境,所有接口在一 ...