博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
洛谷P1432 倒水问题
阅读量:5015 次
发布时间:2019-06-12

本文共 4062 字,大约阅读时间需要 13 分钟。

题目背景

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 
#include
#include
#include
#include
#include
#include
#include
using namespace std;int a,b,n,vis[1010][1010];struct node{ int ca,cb; string op;};queue
q;void zhuangtai(int x,int y,string o,node p){ if (!vis[x][y]) { vis[x][y] = 1; 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 = 0; i < x.op.size(); i++) cout << x.op[i]; cout << "success" << endl; return;}void bfs(){ while (!q.empty()) q.pop(); memset(vis,0,sizeof(vis)); node t; t.ca = 0; t.cb = 0; t.op = ""; q.push(t); vis[0][0] = 1; 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(0,u.cb,"empty A\n",u); zhuangtai(u.ca,0,"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) == 3) bfs(); return 0;}

 

转载于:https://www.cnblogs.com/zbtrs/p/7484551.html

你可能感兴趣的文章
CocoaPods建立私有仓库
查看>>
HIVE中的order by操作
查看>>
Centos下新建用户及修改用户目录
查看>>
iOS开发IPhone以及iPad尺寸汇总
查看>>
Spring Boot RestTemplate文件上传
查看>>
myBatis自动生成mapping,dao和model
查看>>
Android Serivce 高级篇AIDL讲解
查看>>
SpringBoot学习笔记(2):引入Spring Security
查看>>
linq to entity DistinctBy && DefaultIfEmpty
查看>>
图片加水印 PDF取缩略图
查看>>
bzoj 4180: 字符串计数
查看>>
安卓--布局设计-计算器
查看>>
Java重写《C经典100题》 --27
查看>>
ABP中的拦截器之EntityHistoryInterceptor
查看>>
【oracle】oracle数据库建立序列、使用序列实现主键自增
查看>>
使用SQLiteDatabase操作SQLite数据库第二种方法
查看>>
vue,一路走来(12)--父与子之间传参
查看>>
css3 选择器的比较(一) -- 以字符串开头
查看>>
实现交换两个变量值的第二种方法
查看>>
英语单词学习备忘转载
查看>>