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

To 洛谷.1432 倒水问题

题目背景

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 

思路:

  广搜,注意状态即可。

代码:

1.洛谷.1432

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cstring>
 4 using namespace std;
 5 const string oper[6]=
 6 {
 7     "fill A","fill B","empty A",
 8     "empty B","pour A B","pour B A"
 9 };
10 struct state
11 {
12     int x,y,step,pre,op;
13 }q[1024*1024];
14 int a,b,c,head,tail,ans[1024];
15 bool vis[1024][1024];
16 void add(int xn,int yn,int stepn,int pre,int op)
17 {
18     if(vis[xn][yn]) return;
19     vis[xn][yn]=1;
20     q[++tail].x=xn;
21     q[tail].y=yn;
22     q[tail].step=stepn+1;
23     q[tail].pre=pre;
24     q[tail].op=op;
25 }
26 void output(int x)
27 {
28     if(x==1) return;
29     output(q[x].pre);
30     cout<<oper[q[x].op]<<endl;
31 }
32 void print(int x)
33 {
34     //printf("%d\n",q[x].step);
35     output(x);
36     puts("success");
37 }
38 void bfs()
39 {
40     q[++head].x=0;
41     q[head].y=0;
42     q[head].step=0;
43     q[head].pre=1;
44     vis[0][0]=1;
45     while(head<=tail)
46     {
47         int xx=q[head].x;
48         int yy=q[head].y;
49         int st=q[head].step;
50         if(yy==c)
51         {
52             print(head);
53             return;
54         }
55         add(a,yy,st,head,0);
56         add(xx,b,st,head,1);
57         add(0,yy,st,head,2);
58         add(xx,0,st,head,3);
59         int tmp=min(xx,b-yy);
60         add(xx-tmp,yy+tmp,st,head,4);
61         tmp=min(a-xx,yy);
62         add(xx+tmp,yy-tmp,st,head,5);
63         head++;
64     }
65 }
66 int main()
67 {
68     while(scanf("%d%d%d",&a,&b,&c)!=EOF)
69     {
70         memset(vis,0,sizeof(vis));
71         head=0;tail=1;
72         bfs();
73     }
74     return 0;
75 }

洛谷

2.CODEVS.1226

 1 #include<cstdio>
 2 #include<iostream>
 3 using namespace std;
 4 struct node
 5 {
 6     int x,y,step;
 7 }q[300*300];
 8 int a,b,c,head,tail=1;
 9 bool vis[300][300];
10 void add(int xn,int yn,int stepn)
11 {
12     if(vis[xn][yn]) return;
13     vis[xn][yn]=1;
14     q[++tail].x=xn;
15     q[tail].y=yn;
16     q[tail].step=stepn+1;
17 }
18 void bfs()
19 {
20     q[++head].x=0;
21     q[head].y=0;
22     q[head].step=0;
23     vis[0][0]=1;
24     while(head<=tail)
25     {
26         int xx=q[head].x;
27         int yy=q[head].y;
28         int st=q[head].step;
29         if(xx==c||yy==c)
30         {
31             printf("%d",q[head].step);
32             return;
33         }
34         add(a,yy,st);
35         add(xx,b,st);
36         add(0,yy,st);
37         add(xx,0,st);
38         int tmp=min(xx,b-yy);
39         add(xx-tmp,yy+tmp,st);
40         tmp=min(a-xx,yy);
41         add(xx+tmp,yy-tmp,st);
42         head++;
43     }
44     printf("impossible");
45 }
46 int main()
47 {
48     scanf("%d%d%d",&a,&b,&c);
49     if(a==c||b==c)
50     {
51         printf("0");return 0;
52     }
53     bfs();
54     return 0;
55 }

CODEVS

				
时间: 2024-08-03 00:10:25

洛谷P1432 倒水问题(CODEVS.1226)的相关文章

洛谷 P2155 BZOJ 2186 codevs 2301 [SDOI2008]沙拉公主的困惑

题目描述 大富翁国因为通货膨胀,以及假钞泛滥,政府决定推出一项新的政策:现有钞票编号范围为1到N的阶乘,但是,政府只发行编号与M!互质的钞票.房地产第一大户沙拉公主决定预测一下大富翁国现在所有真钞票的数量.现在,请你帮助沙拉公主解决这个问题,由于可能张数非常大,你只需计算出对R取模后的答案即可.R是一个质数.//codevs这里有坑,R是合数 输入输出格式 输入格式: 第一行为两个整数T,R.R<=10^9+10,T<=10000,表示该组中测试数据数目,R为模 后面T行,每行一对整数N,M,

洛谷P1650赛马与codevs 2181 田忌赛马

洛谷P1650 赛马 题目描述 我国历史上有个著名的故事: 那是在2300年以前.齐国的大将军田忌喜欢赛马.他经常和齐王赛马.他和齐王都有三匹马:常规马,上级马,超级马.一共赛三局,每局的胜者可以从负者这里取得200银币.每匹马只能用一次.齐王的马好,同等级的马,齐王的总是比田忌的要好一点.于是每次和齐王赛马,田忌总会输600银币. 田忌很沮丧,直到他遇到了著名的军师――孙膑.田忌采用了孙膑的计策之后,三场比赛下来,轻松而优雅地赢了齐王200银币.这实在是个很简单的计策.由于齐王总是先出最好的马

洛谷 P1262 间谍网络==Codevs 4093 EZ的间谍网络

4093 EZ的间谍网络 时间限制: 10 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 由于外国间谍的大量渗入,国家安全正处于高度的危机之中.如果A间谍手中掌握着关于B间谍的犯罪证据,则称A可以揭发B.有些间谍收受贿赂,只要给他们一定数量的美元,他们就愿意交出手中掌握的全部情报.所以,如果我们能够收买一些间谍的话,我们就可能控制间谍网中的每一分子.因为一旦我们逮捕了一个间谍,他手中掌握的情报都将归我们所有,这样就有可能逮捕新的间谍,掌握新的情报. 我们的反间谍机关

洛谷P1582 倒水

题目描述 一天,CC买了N个容量可以认为是无限大的瓶子,开始时每个瓶子里有1升水.接着~~CC发现瓶子实在太多了,于是他决定保留不超过K个瓶子.每次他选择两个当前含水量相同的瓶子,把一个瓶子的水全部倒进另一个里,然后把空瓶丢弃.(不能丢弃有水的瓶子) 显然在某些情况下CC无法达到目标,比如N=3,K=1.此时CC会重新买一些新的瓶子(新瓶子容量无限,开始时有1升水),以到达目标. 现在CC想知道,最少需要买多少新瓶子才能达到目标呢? 输入输出格式 输入格式: 一行两个正整数, N,K(1<=N<

洛谷 P1383 高级打字机==codevs 3333 高级打字机

P1383 高级打字机 18通过 118提交 题目提供者yeszy 标签倍增图论高级数据结构福建省历届夏令营 难度省选/NOI- 提交该题 讨论 题解 记录 最新讨论 暂时没有讨论 题目描述 早苗入手了最新的高级打字机.最新款自然有着与以往不同的功能,那就是它具备撤销功能,厉害吧. 请为这种高级打字机设计一个程序,支持如下3种操作: 1.T x:在文章末尾打下一个小写字母x.(type操作) 2.U x:撤销最后的x次修改操作.(Undo操作) (注意Query操作并不算修改操作) 3.Q x:

洛谷 P1209 修理牛棚== Codevs 2079 修理牛棚

时间限制: 1 s   空间限制: 128000 KB   题目等级 : 黄金 Gold 题目描述 Description 在一个夜黑风高,下着暴风雨的夜晚,farmer John的牛棚的屋顶.门被吹飞了. 好在许多牛正在度假,所以牛棚没有住满. 牛棚一个紧挨着另一个被排成一行,牛就住在里面过夜. 有些牛棚里有牛,有些没有. 所有的牛棚有相同的宽度. 自门遗失以后,farmer John必须尽快在牛棚之前竖立起新的木板. 他的新木材供应商将会供应他任何他想要的长度,但是吝啬的供应商只能提供有限数

洛谷 P1525 关押罪犯==codevs 1069 关押罪犯[NOIP 2010]

P1525 关押罪犯 513通过 1.4K提交 题目提供者该用户不存在 标签图论并查集NOIp提高组2010 难度普及+/提高 提交该题 讨论 题解 记录 最新讨论 咳咳.竟MLE了. 囧.运行时错误,错误编号-… 点2是何方神圣? 题目描述 S 城现有两座监狱,一共关押着N 名罪犯,编号分别为1~N.他们之间的关系自然也极不和谐.很多罪犯之间甚至积怨已久,如果客观条件具备则随时可能爆发冲突.我们用“怨气值”(一个正整数值)来表示某两名罪犯之间的仇恨程度,怨气值越大,则这两名罪犯之间的积怨越多.

codevs 1048/洛谷 1880:石子归并

题目描述 Description 有n堆石子排成一列,每堆石子有一个重量w[i], 每次合并可以合并相邻的两堆石子,一次合并的代价为两堆石子的重量和w[i]+w[i+1].问安排怎样的合并顺序,能够使得总合并代价达到最小. 输入描述 Input Description 第一行一个整数n(n<=100) 第二行n个整数w1,w2...wn  (wi <= 100) 输出描述 Output Description 一个整数表示最小合并代价 样例输入 Sample Input 4 4 1 1 4 样

洛谷P1019 单词接龙

P1019 单词接龙 题目描述 单词接龙是一个与我们经常玩的成语接龙相类似的游戏,现在我们已知一组单词,且给定一个开头的字母,要求出以这个字母开头的最长的“龙”(每个单词都最多在“龙”中出现两次),在两个单词相连时,其重合部分合为一部分,例如 beast和astonish,如果接成一条龙则变为beastonish,另外相邻的两部分不能存在包含关系,例如at 和 atide 间不能相连. 输入输出格式 输入格式: 输入的第一行为一个单独的整数n (n<=20)表示单词数,以下n 行每行有一个单词,