题目:

给你两个容器,分别能装下A升水和B升水,并且可以进行以下操作
FILL(i)        将第i个容器从水龙头里装满(1 ≤ i ≤ 2);
DROP(i)        将第i个容器抽干
POUR(i,j)      将第i个容器里的水倒入第j个容器(这次操作结束后产生两种结果,一是第j个容器倒满并且第i个容器依旧有剩余,二是第i个容器里的水全部倒入j中,第i个容器为空)
现在要求你写一个程序,来找出能使其中任何一个容器里的水恰好有C升,找出最少操作数并给出操作过程

输入:

有且只有一行,包含3个数A,B,C(1<=A,B<=100,C<=max(A,B))

输出:

第一行包含一个数表示最小操作数K
随后K行每行给出一次具体操作,如果有多种答案符合最小操作数,输出他们中的任意一种操作过程,如果你不能使两个容器中的任意一个满足恰好C升的话,输出“impossible”

样例:

分析:简单的BFS,难点在于回溯,给每个状态用数组记录路径

  1. #include<iostream>
  2. #include<sstream>
  3. #include<cstdio>
  4. #include<cstdlib>
  5. #include<string>
  6. #include<cstring>
  7. #include<algorithm>
  8. #include<functional>
  9. #include<iomanip>
  10. #include<numeric>
  11. #include<cmath>
  12. #include<queue>
  13. #include<vector>
  14. #include<set>
  15. #include<cctype>
  16. #define PI acos(-1.0)
  17. const int INF = 0x3f3f3f3f;
  18. const int NINF = -INF - ;
  19. typedef long long ll;
  20. using namespace std;
  21. int a, b, c;
  22. int used[][];
  23. struct node
  24. {
  25. int x, y;
  26. int flag;
  27. int path[];//数组中0-5分别表示6种不同操作
  28. }st;
  29. string print[] = {"FILL(1)", "FILL(2)", "DROP(1)", "DROP(2)", "POUR(1,2)", "POUR(2,1)"};
  30. void bfs()
  31. {
  32. queue<node> q;
  33. for (int i = ; i <= a; ++i)
  34. {
  35. for (int j = ; j <= b; ++j)
  36. used[i][j] = INF;
  37. }
  38. memset(used, , sizeof(used));
  39. st.x = , st.y = ;
  40. st.flag = ;
  41. memset(st.path, -, sizeof(st.path));
  42. q.push(st);
  43. used[st.x][st.y] = ;
  44. while (q.size())
  45. {
  46. node temp = q.front();
  47. q.pop();
  48. if (temp.x == c || temp.y == c)
  49. {
  50. cout << temp.flag << endl;
  51. for (int i = ; i < temp.flag; ++i)
  52. cout << print[temp.path[i]] << endl;
  53. return;
  54. }
  55. for (int i = ; i < ; ++i)
  56. {
  57. node now = temp;
  58. now.flag++;
  59. if (i == && now.x != a)
  60. {
  61. now.x = a;
  62. if (!used[now.x][now.y])
  63. {
  64. used[now.x][now.y] = ;
  65. now.path[temp.flag] = ;
  66. q.push(now);
  67. }
  68. }
  69. else if (i == && now.y != b)
  70. {
  71. now.y = b;
  72. if (!used[now.x][now.y])
  73. {
  74. used[now.x][now.y] = ;
  75. now.path[temp.flag] = ;
  76. q.push(now);
  77. }
  78. }
  79. else if (i == && now.x != )
  80. {
  81. now.x = ;
  82. if (!used[now.x][now.y])
  83. {
  84. used[now.x][now.y] = ;
  85. now.path[temp.flag] = ;
  86. q.push(now);
  87. }
  88. }
  89. else if (i == && now.y != )
  90. {
  91. now.y = ;
  92. if (!used[now.x][now.y])
  93. {
  94. used[now.x][now.y] = ;
  95. now.path[temp.flag] = ;
  96. q.push(now);
  97. }
  98. }
  99. else if (i == )
  100. {
  101. if (now.x + now.y > b)
  102. {
  103. now.x -= b - now.y;
  104. now.y = b;
  105. }
  106. else
  107. {
  108. now.y += now.x;
  109. now.x = ;
  110. }
  111. if (!used[now.x][now.y])
  112. {
  113. used[now.x][now.y] = ;
  114. now.path[temp.flag] = ;
  115. q.push(now);
  116. }
  117. }
  118. else if (i == )
  119. {
  120. if (now.x + now.y > a)
  121. {
  122. now.y -= a - now.x;
  123. now.x = a;
  124. }
  125. else
  126. {
  127. now.x += now.y;
  128. now.y = ;
  129. }
  130. if (!used[now.x][now.y])
  131. {
  132. used[now.x][now.y] = ;
  133. now.path[temp.flag] = ;
  134. q.push(now);
  135. }
  136. }
  137. }
  138. }
  139. cout << "impossible" << endl;
  140. }
  141. int main()
  142. {
  143. cin >> a >> b >> c;
  144. bfs();
  145. return ;
  146. }

POJ3414 Pots的更多相关文章

  1. POJ3414—Pots(bfs加回溯)

    http://poj.org/problem?id=3414                                       Pots Time Limit: 1000MS   Memor ...

  2. POJ-3414 Pots (BFS)

    Description You are given two pots, having the volume of A and B liters respectively. The following ...

  3. 快速切题 poj3414 Pots

    Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10042   Accepted: 4221   Special J ...

  4. poj3414 Pots (BFS)

    Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12198   Accepted: 5147   Special J ...

  5. POJ3414 Pots —— BFS + 模拟

    题目链接:http://poj.org/problem?id=3414 Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions ...

  6. POJ3414 Pots BFS搜素

    题意:通过题目给出的三种操作,让任意一个杯子中的水到达一定量 分析:两个杯子最大容量是100,所以开个100*100的数组记录状态,最多1w个状态,所以复杂度很低,然后记录一下路径就好 注:代码写残了 ...

  7. POJ-3414.Pots.(BFS + 路径打印)

    这道题做了很长时间,一开始上课的时候手写代码,所以想到了很多细节,但是创客手打代码的时候由于疏忽又未将pair赋初值,导致一直输出错误,以后自己写代码可以专心一点,可能会在宿舍图书馆或者Myhome, ...

  8. poj3414 Pots(BFS)

    题目链接 http://poj.org/problem?id=3414 题意 有两个杯子,容量分别为A升,B升,可以向杯子里倒满水,将杯子里的水倒空,将一个杯子里的水倒到另一个杯子里,求怎样倒才能使其 ...

  9. 【BFS】Pots

    [poj3414]Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16925   Accepted: 7168   ...

随机推荐

  1. SqlServer 导出指定表数据 生成Insert脚本

    版权声明:本文为博主原创文章,未经博主允许不得转载.

  2. 关于angular双向绑定的一个问题,百度无果,还请帮忙解惑。

    用了一段时间anjular蛮好用的.其实用的功能不多.主要用于列表数据绑定以及一些简单效果的绑定,但是最近出现一个现象,百度无果,居然没有人遇到.现在描述一下,截图不方便,希望有人解惑. 列表ng-r ...

  3. HDFS 处理命令记录

    hdfs dfs -ls hdfs dfs -mkdir hdfs dfs -put hdfs dfs -get hdfs dfs -cat hadoop 执行jar  输出的目录 必须要不存在的 y ...

  4. python3:语法变动 及新特性

    python3.0 对python2.x 升级后重大语法变动,幸好留下2.7.6及后续2版本,保持一些语法兼容. 原始地址:http://hi.baidu.com/jxq61/item/3a24883 ...

  5. react基础篇三

    事件处理 React事件绑定属性的命名采用驼峰式写法,而不是小写. 如果采用 JSX 的语法你需要传入一个函数作为事件处理函数,而不是一个字符串(DOM元素的写法) 例如,传统的 HTML: < ...

  6. js 请求单个文件 并验证扩展名

    function suffix(file_name) { var three=file_name.split("."); ]; return last; } $('#btnSear ...

  7. Memcached 之取模与哈希算法命中率实验

    当5台memcache服务器中有一台宕机时的命中率实验. 一.php实现代码 1. config.php $server = array( "A" => array(&quo ...

  8. tomcat8版本实现虚拟主机

    vim /etc/hosts192.168.30.21   www.crushlinux.com192.168.30.21   www.cloud.com [root@localhost ~]# cd ...

  9. 软件工程1916|W(福州大学)_助教博客】团队Beta冲刺作业(第9次)成绩公示

    1. 作业链接: 项目Beta冲刺(团队) 2. 评分准则: 本次作业包括现场Beta答辩评分(映射总分为100分)+团队互评分数(总分40分)+博客分(总分130分)+贡献度得分,其中博客分由以下部 ...

  10. Problem 48

    Problem 48 The series, 11 + 22 + 33 + ... + 1010 = 10405071317. Find the last ten digits of the seri ...