广搜的灵活应用题:

非常可乐

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3633    Accepted Submission(s): 1514

Problem Description
大家一定觉的运动以后喝可乐是一件很惬意的事情,但是seeyou却不这么认为。因为每次当seeyou买了可乐以后,阿牛就要求和seeyou一起分享这一瓶可乐,而且一定要喝的和seeyou一样多。但seeyou的手中只有两个杯子,它们的容量分别是N 毫升和M 毫升 可乐的体积为S (S<101)毫升 (正好装满一瓶) ,它们三个之间可以相互倒可乐 (都是没有刻度的,且 S==N+M,101>S>0,N>0,M>0) 。聪明的ACMER你们说他们能平分吗?如果能请输出倒可乐的最少的次数,如果不能输出"NO"。
 
Input
三个整数 : S 可乐的体积 , N 和 M是两个杯子的容量,以"0 0 0"结束。
 
Output
如果能平分的话请输出最少要倒的次数,否则输出"NO"。
 
Sample Input
7 4 3
4 1 3
0 0 0
 
Sample Output
NO 3
  1. //一共6种情况;all->big,all->small,big->small,big->all,small->all,small->big进行bfs搜索,如果未被搜到过,
  2. //就入队列,如果出现两个瓶子里水相同就返回输出。(总水量为奇数时无解)
  3. #include<stdio.h>
  4. #include<string.h>
  5. #include<queue>
  6. using namespace std;
  7. struct node
  8. {
  9. int s,n,m;
  10. int step;
  11. };
  12. int hash[][][];
  13. int s,n,m;
  14.  
  15. int BFS()
  16. {
  17. queue<node>q;
  18. node cur,next;
  19. cur.s=s;
  20. cur.n=;
  21. cur.m=;
  22. cur.step=;
  23. hash[cur.s][cur.n][cur.m]=;
  24. q.push(cur);
  25. while(!q.empty())
  26. {
  27. cur=q.front();
  28. q.pop();
  29. if((cur.n== && cur.m==cur.s)||(cur.m== && cur.n==cur.s)||( cur.s== && cur.n==cur.m)) return cur.step;
  30. if(cur.n!=)
  31. {
  32. if(cur.n>s-cur.s)
  33. {
  34. next.n=cur.n-(s-cur.s);
  35. next.s=s;
  36. next.m=cur.m;
  37. if(hash[next.s][next.n][next.m]==)
  38. {
  39. next.step=cur.step+;
  40. q.push(next);
  41. hash[next.s][next.n][next.m]=;
  42. }
  43. }
  44. else
  45. {
  46. next.n=;
  47. next.s=cur.s+cur.n;
  48. next.m=cur.m;
  49. if(hash[next.s][next.n][next.m]==)
  50. {
  51. next.step=cur.step+;
  52. q.push(next);
  53. hash[next.s][next.n][next.m]=;
  54. }
  55. }
  56. if(cur.n>m-cur.m)
  57. {
  58. next.n=cur.n-(m-cur.m);
  59. next.m=m;
  60. next.s=cur.s;
  61. if(hash[next.s][next.n][next.m]==)
  62. {
  63. next.step=cur.step+;
  64. q.push(next);
  65. hash[next.s][next.n][next.m]=;
  66. }
  67. }
  68. else
  69. {
  70. next.n=;
  71. next.m=cur.m+cur.n;
  72. next.s=cur.s;
  73. if(hash[next.s][next.n][next.m]==)
  74. {
  75. next.step=cur.step+;
  76. q.push(next);
  77. hash[next.s][next.n][next.m]=;
  78. }
  79. }
  80. }
  81.  
  82. if(cur.m)
  83. {
  84. if(cur.m>s-cur.s)
  85. {
  86. next.m=cur.n-(s-cur.s);
  87. next.s=s;
  88. next.n=cur.n;
  89. if(hash[next.s][next.n][next.m]==)
  90. {
  91. next.step=cur.step+;
  92. q.push(next);
  93. hash[next.s][next.n][next.m]=;
  94. }
  95. }
  96. else
  97. {
  98. next.m=;
  99. next.s=cur.s+cur.m;
  100. next.n=cur.n;
  101. if(hash[next.s][next.n][next.m]==)
  102. {
  103. next.step=cur.step+;
  104. q.push(next);
  105. hash[next.s][next.n][next.m]=;
  106. }
  107. }
  108. if(cur.m>n-cur.n)
  109. {
  110. next.m=cur.m-(n-cur.n);
  111. next.n=n;
  112. next.s=cur.s;
  113. if(hash[next.s][next.n][next.m]==)
  114. {
  115. next.step=cur.step+;
  116. q.push(next);
  117. hash[next.s][next.n][next.m]=;
  118. }
  119. }
  120. else
  121. {
  122. next.m=;
  123. next.n=cur.m+cur.n;
  124. next.s=cur.s;
  125. if(hash[next.s][next.n][next.m]==)
  126. {
  127. next.step=cur.step+;
  128. q.push(next);
  129. hash[next.s][next.n][next.m]=;
  130. }
  131. }
  132. }
  133.  
  134. if(cur.s)
  135. {
  136. if(cur.s>n-cur.n)
  137. {
  138. next.s=cur.s-(n-cur.n);
  139. next.n=n;
  140. next.m=cur.m;
  141. if(hash[next.s][next.n][next.m]==)
  142. {
  143. next.step=cur.step+;
  144. q.push(next);
  145. hash[next.s][next.n][next.m]=;
  146. }
  147. }
  148. else
  149. {
  150. next.s=;
  151. next.n=cur.s+cur.n;
  152. next.m=cur.m;
  153. if(hash[next.s][next.n][next.m]==)
  154. {
  155. next.step=cur.step+;
  156. q.push(next);
  157. hash[next.s][next.n][next.m]=;
  158. }
  159. }
  160. if(cur.s>m-cur.m)
  161. {
  162. next.s=cur.s-(m-cur.m);
  163. next.m=m;
  164. next.n=cur.n;
  165. if(hash[next.s][next.n][next.m]==)
  166. {
  167. next.step=cur.step+;
  168. q.push(next);
  169. hash[next.s][next.n][next.m]=;
  170. }
  171. }
  172. else
  173. {
  174. next.s=;
  175. next.m=cur.m+cur.s;
  176. next.n=cur.n;
  177. if(hash[next.s][next.n][next.m]==)
  178. {
  179. next.step=cur.step+;
  180. q.push(next);
  181. hash[next.s][next.n][next.m]=;
  182. }
  183. }
  184. }
  185. }
  186.  
  187. return ;
  188. }
  189. int main()
  190. {
  191. int ans;
  192. while(scanf("%d%d%d",&s,&n,&m),s||n||m)
  193. {
  194. memset(hash,,sizeof(hash));
  195.  
  196. if(s%) {printf("NO\n");continue;}
  197.  
  198. ans=BFS();
  199. if(ans) printf("%d\n",ans);
  200. else printf("NO\n");
  201. }
  202. return ;
  203. }

HDU-1495 非常可乐(BFS)的更多相关文章

  1. HDU 1495 非常可乐 BFS 搜索

    http://acm.hdu.edu.cn/showproblem.php?pid=1495 题目就不说了, 说说思路! 倒可乐 无非有6种情况: 1. S 向 M 倒 2. S 向 N 倒 3. N ...

  2. HDU 1495 非常可乐 bfs 难度:1

    http://acm.hdu.edu.cn/showproblem.php?pid=1495 第三个杯子的盛水量可由前两个杯子得到,而前两个杯子状态总数在100*100以内,穷举可实现 #includ ...

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

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

  4. HDU 1495 非常可乐 BFS搜索

    题意:有个为三个杯子(杯子没有刻度),体积为s,n,m,s=m+n, 刚开始只有体积为s的杯子装满可乐,可以互相倒,问你最少的次数使可乐均分,如果没有结果,输出-1; 分析:直接互相倒就完了,BFS模 ...

  5. HDU 1495 非常可乐 BFS

    题目大意:中文题不说了. 题目思路:我有同学用GCD数论写出来的代码很简洁,但是很抱歉,数论蒟蒻,我觉得比赛的时候我没办法推出.如果用BFS的话思路很简单的,就是6方向广搜,只不过稍微麻烦点.具体看代 ...

  6. HDU - 1495 非常可乐 bfs互倒三杯水

    非常可乐 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  7. BFS(倒水问题) HDU 1495 非常可乐

    题目传送门 /* BFS:倒水问题,当C是奇数时无解.一共有六种情况,只要条件符合就入队,我在当该状态vised时写了continue 结果找了半天才发现bug,泪流满面....(网上找份好看的题解都 ...

  8. HDU 1495 非常可乐(数论,BFS)

    非常可乐 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  9. 非常可乐---hdu 1495(BFS)

    http://acm.hdu.edu.cn/showproblem.php?pid=1495 题意: 有3个杯子a b c:a=b+c:然后刚开始时只有a是满的,其它为空的,然后a b c三个之间互相 ...

  10. HDU 1495 非常可乐(BFS倒水问题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1495 题目大意:只有两个杯子,它们的容量分别是N 毫升和M 毫升 可乐的体积为S (S<101) ...

随机推荐

  1. SCNU省选校赛第二场B题题解

    今晚的校赛又告一段落啦,终于"开斋"了! AC了两题,还算是满意的,英语还是硬伤. 来看题目吧! B. Array time limit per test 2 seconds me ...

  2. gcc命令以及makefile文件

    (一)makefile里涉及到的gcc命令 gcc -I./inc:指定头文件寻找目录 将按照 ./inc --> /usr/include --> /usr/local/include的 ...

  3. C++类继承内存布局(二)

    转自:http://blog.csdn.net/jiangyi711/article/details/4890889# (二 )成员变量 前面介绍完了类布局,接下来考虑不同的继承方式下,访问成员变量的 ...

  4. OpenJudge/Poj 1631 Bridging signals

    1.链接地址: http://poj.org/problem?id=1631 http://bailian.openjudge.cn/practice/1631 2.题目: Bridging sign ...

  5. 为什么aspx这么“慢”

    首先你要明白什么viewstate:由系统生成的一个隐藏域,用来进行页面状态保持的 里面存放着关于判断页面是否提交的Ispostback,和一些关于服务器控件的状态和数据: (说明下 ,ViewSta ...

  6. 服务器卡死,重启报错: INFO: task blocked for more than 120 seconds

    问题:服务器负载很高,但是CPU利用率不高.服务器经常夯住,网站打不开,SSH连接非常不稳定,输入命令夯住. 重启服务器报错: INFO: task blocked for more than 120 ...

  7. 配置php连接apache

    配置php连接apache 1.安装php所需要的库 yum install zlib-devel libxml2-devel libjpeg-devel libjpeg-turbo-devel li ...

  8. 使用SqlBulkCopy批量插入多条数据进入表中

    由于工作中项目需求结算一次生成一批相同批次号的数据插入一个表中,然后再通过另一页面展示出来,所以需要用到一次性插入一批数据,所以就采用了SqlBulkCopy插入一批数据 1 public stati ...

  9. Python中 if __name__ == '__main__': 详解

    一个python文件就可以看作是一个python的模块,这个python模块(.py文件)有两种使用方式:直接运行和作为模块被其他模块调用. __name__:每一个模块都有一个内置属性__name_ ...

  10. Linux文件权限学习总结

    一.用户对文件或目录都有哪些权限? 四种:读.写.执行.没有权限 二.如何表示这四种权限? 如果用十进制数字表示,分别为:4.2.1.0:如果用字符表示,分别为:r.w.x.-.个人觉得,使用chmo ...