洛谷P1432 倒水问题(CODEVS.1226)
题目背景
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列开始输出,行末没有空格。
输入输出样例
3 5 4
5 7 3
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
思路:
广搜,注意状态即可。
代码:
1.洛谷.1432
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
const string oper[]=
{
"fill A","fill B","empty A",
"empty B","pour A B","pour B A"
};
struct state
{
int x,y,step,pre,op;
}q[*];
int a,b,c,head,tail,ans[];
bool vis[][];
void add(int xn,int yn,int stepn,int pre,int op)
{
if(vis[xn][yn]) return;
vis[xn][yn]=;
q[++tail].x=xn;
q[tail].y=yn;
q[tail].step=stepn+;
q[tail].pre=pre;
q[tail].op=op;
}
void output(int x)
{
if(x==) return;
output(q[x].pre);
cout<<oper[q[x].op]<<endl;
}
void print(int x)
{
//printf("%d\n",q[x].step);
output(x);
puts("success");
}
void bfs()
{
q[++head].x=;
q[head].y=;
q[head].step=;
q[head].pre=;
vis[][]=;
while(head<=tail)
{
int xx=q[head].x;
int yy=q[head].y;
int st=q[head].step;
if(yy==c)
{
print(head);
return;
}
add(a,yy,st,head,);
add(xx,b,st,head,);
add(,yy,st,head,);
add(xx,,st,head,);
int tmp=min(xx,b-yy);
add(xx-tmp,yy+tmp,st,head,);
tmp=min(a-xx,yy);
add(xx+tmp,yy-tmp,st,head,);
head++;
}
}
int main()
{
while(scanf("%d%d%d",&a,&b,&c)!=EOF)
{
memset(vis,,sizeof(vis));
head=;tail=;
bfs();
}
return ;
}
洛谷
2.CODEVS.1226
#include<cstdio>
#include<iostream>
using namespace std;
struct node
{
int x,y,step;
}q[*];
int a,b,c,head,tail=;
bool vis[][];
void add(int xn,int yn,int stepn)
{
if(vis[xn][yn]) return;
vis[xn][yn]=;
q[++tail].x=xn;
q[tail].y=yn;
q[tail].step=stepn+;
}
void bfs()
{
q[++head].x=;
q[head].y=;
q[head].step=;
vis[][]=;
while(head<=tail)
{
int xx=q[head].x;
int yy=q[head].y;
int st=q[head].step;
if(xx==c||yy==c)
{
printf("%d",q[head].step);
return;
}
add(a,yy,st);
add(xx,b,st);
add(,yy,st);
add(xx,,st);
int tmp=min(xx,b-yy);
add(xx-tmp,yy+tmp,st);
tmp=min(a-xx,yy);
add(xx+tmp,yy-tmp,st);
head++;
}
printf("impossible");
}
int main()
{
scanf("%d%d%d",&a,&b,&c);
if(a==c||b==c)
{
printf("");return ;
}
bfs();
return ;
}
CODEVS
洛谷P1432 倒水问题(CODEVS.1226)的更多相关文章
- 洛谷P1432 倒水问题
题目背景 In the movie "Die Hard 3", Bruce Willis and Samuel L. Jackson were confronted with th ...
- 洛谷 P1432 倒水问题
目录 题目 思路 \(Code\) 题目 戳 思路 \(bfs\) 第一遍提交\(50\),第二遍就\(100\)了,qwq \(Code\) #include<iostream> #in ...
- 洛谷P1395 会议(CODEVS.3029.设置位置)(求树的重心)
To 洛谷.1395 会议 To CODEVS.3029 设置位置 题目描述 有一个村庄居住着n个村民,有n-1条路径使得这n个村民的家联通,每条路径的长度都为1.现在村长希望在某个村民家中召开一场会 ...
- 洛谷 P2155 BZOJ 2186 codevs 2301 [SDOI2008]沙拉公主的困惑
题目描述 大富翁国因为通货膨胀,以及假钞泛滥,政府决定推出一项新的政策:现有钞票编号范围为1到N的阶乘,但是,政府只发行编号与M!互质的钞票.房地产第一大户沙拉公主决定预测一下大富翁国现在所有真钞票的 ...
- 洛谷P1650赛马与codevs 2181 田忌赛马
洛谷P1650 赛马 题目描述 我国历史上有个著名的故事: 那是在2300年以前.齐国的大将军田忌喜欢赛马.他经常和齐王赛马.他和齐王都有三匹马:常规马,上级马,超级马.一共赛三局,每局的胜者可以从负 ...
- 洛谷 P1582 倒水 解题报告
P1582 倒水 题目描述 一天,CC买了N个容量可以认为是无限大的瓶子,开始时每个瓶子里有1升水.接着~~CC发现瓶子实在太多了,于是他决定保留不超过K个瓶子.每次他选择两个当前含水量相同的瓶子,把 ...
- 洛谷 P1262 间谍网络==Codevs 4093 EZ的间谍网络
4093 EZ的间谍网络 时间限制: 10 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 由于外国间谍的大量渗入,国家安全正处于高度的危机之中.如果A间谍手中掌握着关于B ...
- 洛谷P1582 倒水
P1582 倒水 题目描述 一天,CC买了N个容量可以认为是无限大的瓶子,开始时每个瓶子里有1升水.接着~~CC发现瓶子实在太多了,于是他决定保留不超过K个瓶子.每次他选择两个当前含水量相同的瓶子,把 ...
- 洛谷——P1002 过河卒||codevs——T1010 过河卒
https://www.luogu.org/problem/show?pid=1002#sub||http://codevs.cn/problem/1010/ 题目描述 棋盘上A点有一个过河卒,需要走 ...
随机推荐
- Python3学习笔记24-操作文件和目录
环境变量 在操作系统中定义的环境变量,全部保存在os.environ这个变量中,可以直接查看: import os print(os.environ) 操作文件和目录 操作文件和目录的函数一部分放在o ...
- 如何解决OpenStack创建虚拟机或删除虚拟机时一直处于deleting或者creating状态的问题(转载)
原文地址:http://www.cnblogs.com/robertoji/p/4968280.html 在OpenStack使用时,有时候会遇到创建虚拟机或者删除虚拟机无法成功创建或者删除的时候,一 ...
- [BZOJ3295] [Cqoi2011]动态逆序对(带修改主席树)
题目描述 对于序列A,它的逆序对数定义为满足i<j,且Ai>Aj的数对(i,j)的个数.给1到n的一个排列,按照某种顺序依次删除m个元素,你的任务是在每次删除一个元素之前统计整个序列的逆序 ...
- eclipse 反编译
Eclipse Class Decompiler安装此插件,可以编译源代码且调试
- eclipse总是自动跳到ThreadPoolExecutor解决办法
出现这种状况是因为Eclipse默认开启挂起未捕获的异常(Suspend execution on uncaught exceptions),只要关闭此项就可以了. 解决方法:在eclipse中选择W ...
- Ex 5_32 一台服务器当前有n个等待服务的顾客...第八次作业
设第i个客户需要等待的时间为ti,则n个客户需要总的等待时间为 ,因此,要使T最小,则要使 即可,所以,对所有的ti按升序进行排序和服务将得到最小的等待时间. package org.xiu68.ch ...
- Java中日期格式化SimpleDateFormat类包含时区的处理方法
1.前言 需要把格式为“2017-02-23T08:04:02+01:00”转化成”23-02-2017-T15:04:02“格式(中国时区为+08:00所以是15点),通过网上查找答案,发现没有我需 ...
- Sudo的用法和Visudo设置
身为程序员,你可以活在一个没有Windows的世界,当你离不开Unix(Linux,Mac...).而在Unix下面,你离不开terminal,离不开sudo. 你知道sudo command,然后输 ...
- 如何通过编译Linux内核打开IPVS(LVS)的debug模式
前言 为了定位keepalived VIP的问题, 一步一步定位到IPVS, IPVS默认是没有打开Debug模式的, 若需要打开Debug模式需要重新编译IPVS模块加载后才行, 最好的方式当然是仅 ...
- 《剑指offer》-青蛙跳台阶II
一只青蛙一次可以跳上1级台阶,也可以跳上2级--它也可以跳上n级.求该青蛙跳上一个n级的台阶总共有多少种跳法. 其实题目很水...就是一个等比数列通项公式嘛 f(0)=1 f(1)=1 f(n)=f( ...