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:

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

  1. fill B
  2. pour B A
  3. empty A
  4. pour B A
  5. fill B
  6. pour B A
  7. success
  8. fill A
  9. pour A B
  10. fill A
  11. pour A B
  12. empty B
  13. pour A B
  14. success

思路:

  广搜,注意状态即可。

代码:

1.洛谷.1432

  1. #include<cstdio>
  2. #include<iostream>
  3. #include<cstring>
  4. using namespace std;
  5. const string oper[]=
  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[*];
  14. int a,b,c,head,tail,ans[];
  15. bool vis[][];
  16. void add(int xn,int yn,int stepn,int pre,int op)
  17. {
  18. if(vis[xn][yn]) return;
  19. vis[xn][yn]=;
  20. q[++tail].x=xn;
  21. q[tail].y=yn;
  22. q[tail].step=stepn+;
  23. q[tail].pre=pre;
  24. q[tail].op=op;
  25. }
  26. void output(int x)
  27. {
  28. if(x==) 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=;
  41. q[head].y=;
  42. q[head].step=;
  43. q[head].pre=;
  44. vis[][]=;
  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,);
  56. add(xx,b,st,head,);
  57. add(,yy,st,head,);
  58. add(xx,,st,head,);
  59. int tmp=min(xx,b-yy);
  60. add(xx-tmp,yy+tmp,st,head,);
  61. tmp=min(a-xx,yy);
  62. add(xx+tmp,yy-tmp,st,head,);
  63. head++;
  64. }
  65. }
  66. int main()
  67. {
  68. while(scanf("%d%d%d",&a,&b,&c)!=EOF)
  69. {
  70. memset(vis,,sizeof(vis));
  71. head=;tail=;
  72. bfs();
  73. }
  74. return ;
  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[*];
  8. int a,b,c,head,tail=;
  9. bool vis[][];
  10. void add(int xn,int yn,int stepn)
  11. {
  12. if(vis[xn][yn]) return;
  13. vis[xn][yn]=;
  14. q[++tail].x=xn;
  15. q[tail].y=yn;
  16. q[tail].step=stepn+;
  17. }
  18. void bfs()
  19. {
  20. q[++head].x=;
  21. q[head].y=;
  22. q[head].step=;
  23. vis[][]=;
  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(,yy,st);
  37. add(xx,,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("");return ;
  52. }
  53. bfs();
  54. return ;
  55. }

CODEVS

  1.  

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

  1. 洛谷P1432 倒水问题

    题目背景 In the movie "Die Hard 3", Bruce Willis and Samuel L. Jackson were confronted with th ...

  2. 洛谷 P1432 倒水问题

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

  3. 洛谷P1395 会议(CODEVS.3029.设置位置)(求树的重心)

    To 洛谷.1395 会议 To CODEVS.3029 设置位置 题目描述 有一个村庄居住着n个村民,有n-1条路径使得这n个村民的家联通,每条路径的长度都为1.现在村长希望在某个村民家中召开一场会 ...

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

    题目描述 大富翁国因为通货膨胀,以及假钞泛滥,政府决定推出一项新的政策:现有钞票编号范围为1到N的阶乘,但是,政府只发行编号与M!互质的钞票.房地产第一大户沙拉公主决定预测一下大富翁国现在所有真钞票的 ...

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

    洛谷P1650 赛马 题目描述 我国历史上有个著名的故事: 那是在2300年以前.齐国的大将军田忌喜欢赛马.他经常和齐王赛马.他和齐王都有三匹马:常规马,上级马,超级马.一共赛三局,每局的胜者可以从负 ...

  6. 洛谷 P1582 倒水 解题报告

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

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

    4093 EZ的间谍网络 时间限制: 10 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 由于外国间谍的大量渗入,国家安全正处于高度的危机之中.如果A间谍手中掌握着关于B ...

  8. 洛谷P1582 倒水

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

  9. 洛谷——P1002 过河卒||codevs——T1010 过河卒

    https://www.luogu.org/problem/show?pid=1002#sub||http://codevs.cn/problem/1010/ 题目描述 棋盘上A点有一个过河卒,需要走 ...

随机推荐

  1. 一步步实现windows版ijkplayer系列文章之六——SDL2源码分析之OpenGL ES在windows上的渲染过程

    一步步实现windows版ijkplayer系列文章之一--Windows10平台编译ffmpeg 4.0.2,生成ffplay 一步步实现windows版ijkplayer系列文章之二--Ijkpl ...

  2. (常用)subprocess模块 详情官方

    subprocess包中定义有数个创建子进程的函数,这些函数分别以不同的方式创建子进程,所以我们可以根据需要来从中选取一个使用.另外subprocess还提供了一些管理标准流(standard str ...

  3. Zabbix Agent active批量调整客户端为主动模式监控

    Zabbix Agent active批量调整客户端为主动模式监控 zabbix_server端当主机数量过多的时候,由Server端去收集数据,Zabbix会出现严重的性能问题,主要表现如下: 1. ...

  4. GO-time.after 用法

    初学GO,time包里sleep是最常用,今天突然看到一个time.after,特记录time.after用法笔记如下: 首先是time包里的定义 // After waits for the dur ...

  5. Numpy详解

    NumPy 简介 Python并没有提供数组功能.虽然列表可以完成基本的数组功能,但它不是真正的数组,而且在数据量比较大时,使用列表的速度会很慢.为此,Numpy提供了真正的数组功能,以及对数据进行快 ...

  6. Vue源码

    参考文章:http://hcysun.me/2017/03/03/Vue%E6%BA%90%E7%A0%81%E5%AD%A6%E4%B9%A0/?utm_source=qq&utm_medi ...

  7. vue-cli 搭建的项目处理不同环境下请求不同域名的问题

    使用 vue-cli 开发项目过程中, 根据开发环境和正式环境不同, 我们往往需要请求不同域名下的后台接口, 这时候, 该怎么去设置, 达到同一种写法可以根据环境不同而自动切换请求域名呢? 本文将会介 ...

  8. 【linux】tar压缩不包含路径

    -C 参数 文件路径  /home/test/files tar zcvf file.tar.gz -C /home/test files 这样压缩后,就是可以得当一个相对路径的压缩包了,直接排除掉/ ...

  9. ubuntu git hub 建立仓库

    https://www.cnblogs.com/woider/p/6533709.html 1.安装git apt-get install git 2.配置 Git 用户信息 把用户名和邮箱换成你自己 ...

  10. MVC开发中的常见错误-03-System.Data.Entity.Validation.DbEntityValidationException: 对一个或多个实体的验证失败。有关详细信息,请参见“EntityValidationErrors”属性。

    return Db.SaveChanges()>0; return CurrentDBSession.SaveChanges(); RoleInfoService.EditEntity(role ...