(step4.2.5)hdu 1495(非常可乐——BFS)

快来打我* 2021-12-13 14:47 277阅读 0赞

题目大意:输入三个整数 a,b,c. a : 可乐瓶的容量,b: 甲杯的容量 ,c: 乙杯的容量。问能否用这三个被来实现饮料的平分???如果可以输出倒饮料的次数,

否则输出NO

解题思路:BFS

1)本题的考点其实在于将标记数组由二维数组变为三维数组。遍历状态由使用for()循环变为手动枚举,一个一个的if()

代码如下:

  1. /*
  2. * 1495_2.cpp
  3. *
  4. * Created on: 2013年8月16日
  5. * Author: Administrator
  6. */
  7. #include <iostream>
  8. #include <queue>
  9. using namespace std;
  10. const int maxn = 102;
  11. bool visited[maxn][maxn][maxn];
  12. int a, b, c;
  13. struct State {
  14. int a;
  15. int b;
  16. int c;
  17. int v;
  18. };
  19. bool checkState(State st) {
  20. if (!visited[st.a][st.b][st.c]) {
  21. return true;
  22. }
  23. return false;
  24. }
  25. void bfs() {
  26. queue<State> q;
  27. State st, now, next;
  28. st.a = a;
  29. st.b = 0;
  30. st.c = 0;
  31. st.v = 0;
  32. q.push(st);
  33. memset(visited,0,sizeof(visited) );
  34. visited[st.a][st.b][st.c] = 1;
  35. while (!q.empty()) {
  36. now = q.front();
  37. //有2个等于a/2就结束
  38. if ((now.a == a / 2 && now.b == a / 2)
  39. || (now.a == a / 2 && now.c == a / 2)
  40. || (now.c == a / 2 && now.b == a / 2)) {
  41. printf("%d\n", now.v);
  42. return ;
  43. }
  44. /**
  45. * 若a杯中的饮料的体积不为0,
  46. * 则枚举出将a杯中的饮料倒到其他杯中....
  47. */
  48. if (now.a != 0) {
  49. /**
  50. * 关键举得理解:now.a > b - now.b
  51. * now.a : now状态下a杯中的饮料的体积
  52. * b : b杯的体积
  53. * now.b :now状态下b杯中的饮料的体积
  54. *
  55. */
  56. if (now.a > b - now.b) {//now.a > b - now.b。且倒不完
  57. next.a = now.a - (b - now.b);
  58. next.b = b;
  59. next.c = now.c;
  60. next.v = now.v + 1;
  61. } else {//倒完了
  62. next.a = 0;
  63. next.b = now.b + now.a;
  64. next.c = now.c;
  65. next.v = now.v + 1;
  66. }
  67. if (checkState(next)) {
  68. q.push(next);
  69. visited[next.a][next.b][next.c] = 1;
  70. }
  71. if (now.a > c - now.c) {
  72. next.a = now.a - (c - now.c);
  73. next.b = now.b;
  74. next.c = c;
  75. next.v = now.v + 1;
  76. } else {
  77. next.a = 0;
  78. next.b = now.b;
  79. next.c = now.c + now.a;
  80. next.v = now.v + 1;
  81. }
  82. if (checkState(next)) {
  83. q.push(next);
  84. visited[next.a][next.b][next.c] = 1;
  85. }
  86. }
  87. if (now.b != 0) {
  88. if (now.b > a - now.a) {
  89. next.a = a;
  90. next.b = now.b - (a - now.a);
  91. next.c = now.c;
  92. next.v = now.v + 1;
  93. } else {
  94. next.a = now.a + now.b;
  95. next.b = 0;
  96. next.c = now.c;
  97. next.v = now.v + 1;
  98. }
  99. if (checkState(next)) {
  100. q.push(next);
  101. visited[next.a][next.b][next.c] = 1;
  102. }
  103. if (now.b > c - now.c) {
  104. next.a = now.a ;
  105. next.b = now.b - (c - now.c);
  106. next.c = c;
  107. next.v = now.v + 1;
  108. } else {
  109. next.a = now.a;
  110. next.b = 0;
  111. next.c = now.c + now.b;
  112. next.v = now.v + 1;
  113. }
  114. if (checkState(next)) {
  115. q.push(next);
  116. visited[next.a][next.b][next.c] = 1;
  117. }
  118. }
  119. if (now.c != 0) {
  120. if (now.c > b - now.b) {
  121. next.a = now.a ;
  122. next.b = b;
  123. next.c = now.c - (b - now.b);
  124. next.v = now.v + 1;
  125. } else {
  126. next.a = now.a;
  127. next.b = now.b + now.c;
  128. next.c = 0;
  129. next.v = now.v + 1;
  130. }
  131. if (checkState(next)) {
  132. q.push(next);
  133. visited[next.a][next.b][next.c] = 1;
  134. }
  135. if (now.c > a - now.a) {
  136. next.a = a;
  137. next.b = now.b;
  138. next.c = now.c - (a - now.a);
  139. next.v = now.v + 1;
  140. } else {
  141. next.a = now.a + now.c;
  142. next.b = now.b;
  143. next.c = 0;
  144. next.v = now.v + 1;
  145. }
  146. if (checkState(next)) {
  147. q.push(next);
  148. visited[next.a][next.b][next.c] = 1;
  149. }
  150. }
  151. q.pop();
  152. }
  153. printf("NO\n");
  154. }
  155. int main() {
  156. while(scanf("%d%d%d",&a,&b,&c)!=EOF,a+b+c){
  157. if(a%2 == 1){
  158. printf("NO\n");
  159. }else{
  160. bfs();
  161. }
  162. }
  163. }

转载于:https://www.cnblogs.com/james1207/p/3262733.html

发表评论

表情:
评论列表 (有 0 条评论,277人围观)

还没有评论,来说两句吧...

相关阅读

    相关 HDU 1728(BFS)

    问题描述: 给定一个m × n (m行, n列)的迷宫,迷宫中有两个位置,gloria想从迷宫的一个位置走到另外一个位置,当然迷宫中有些地方是空地,gloria可以穿越,有些