题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1495

题意:不解释

题解:BFS模拟,不过要细心,把所有情况都列举出来,开一个数组记录状态,代码有点长,比较重复,尝试优化了一下,不过WA了

  1. #include<cstdio>
  2. #include<queue>
  3. #include<cstring>
  4. using namespace std;
  5. struct dt{int s,m,n,t;};
  6. int s,m,n;
  7. bool v[][][];
  8. int bfs(){
  9. memset(v,,sizeof(v));
  10. v[s][][]=;
  11. dt tmp,o;tmp.s=s,tmp.m=,tmp.n=,tmp.t=;
  12. queue<dt>Q;Q.push(tmp);
  13. while(!Q.empty()){
  14. o=Q.front();Q.pop();
  15. if((o.s==o.m&&!o.n)||(o.s==o.n&&!o.m)||(o.n==o.m&&!o.s))return o.t;
  16. if(o.s){
  17. if(o.m<m){
  18. if(o.s+o.m>=m){
  19. tmp.m=m,tmp.s=o.s+o.m-m,tmp.n=o.n,tmp.t=o.t+;
  20. if(!v[tmp.s][tmp.m][tmp.n]){Q.push(tmp);v[tmp.s][tmp.m][tmp.n]=;}
  21. }else{
  22. tmp.m=o.m+o.s,tmp.s=,tmp.n=o.n,tmp.t=o.t+;
  23. if(!v[tmp.s][tmp.m][tmp.n]){Q.push(tmp);v[tmp.s][tmp.m][tmp.n]=;}
  24. }
  25. }
  26. if(o.n<n){
  27. if(o.s+o.n>=n){
  28. tmp.n=n,tmp.s=o.s+o.n-n,tmp.m=o.m,tmp.t=o.t+;
  29. if(!v[tmp.s][tmp.m][tmp.n]){Q.push(tmp);v[tmp.s][tmp.m][tmp.n]=;}
  30. }else{
  31. tmp.n=o.n+o.s,tmp.s=,tmp.m=o.m,tmp.t=o.t+;
  32. if(!v[tmp.s][tmp.m][tmp.n]){Q.push(tmp);v[tmp.s][tmp.m][tmp.n]=;}
  33. }
  34. }
  35. }
  36. if(o.m){
  37. if(o.s<s){
  38. if(o.s+o.m>=s){
  39. tmp.s=s,tmp.m=o.m+o.s-s,tmp.n=o.n,tmp.t=o.t+;
  40. if(!v[tmp.s][tmp.m][tmp.n]){Q.push(tmp);v[tmp.s][tmp.m][tmp.n]=;}
  41. }else{
  42. tmp.s=o.s+o.m,tmp.m=,tmp.n=o.n,tmp.t=o.t+;
  43. if(!v[tmp.s][tmp.m][tmp.n]){Q.push(tmp);v[tmp.s][tmp.m][tmp.n]=;}
  44. }
  45. }
  46. if(o.n<n){
  47. if(o.m+o.n>=n){
  48. tmp.n=n,tmp.m=o.m+o.n-n,tmp.s=o.s,tmp.t=o.t+;
  49. if(!v[tmp.s][tmp.m][tmp.n]){Q.push(tmp);v[tmp.s][tmp.m][tmp.n]=;}
  50. }else{
  51. tmp.n=o.n+o.m,tmp.m=,tmp.s=o.s,tmp.t=o.t+;
  52. if(!v[tmp.s][tmp.m][tmp.n]){Q.push(tmp);v[tmp.s][tmp.m][tmp.n]=;}
  53. }
  54. }
  55. }
  56. if(o.n){
  57. if(o.m<m){
  58. if(o.n+o.m>=m){
  59. tmp.m=m,tmp.n=o.n+o.m-m,tmp.s=o.s,tmp.t=o.t+;
  60. if(!v[tmp.s][tmp.m][tmp.n]){Q.push(tmp);v[tmp.s][tmp.m][tmp.n]=;}
  61. }else{
  62. tmp.m=o.m+o.n,tmp.n=,tmp.s=o.s,tmp.t=o.t+;
  63. if(!v[tmp.s][tmp.m][tmp.n]){Q.push(tmp);v[tmp.s][tmp.m][tmp.n]=;}
  64. }
  65. }
  66. if(o.s<s){
  67. if(o.n+o.s>=s){
  68. tmp.s=s,tmp.n=o.n+o.s-s,tmp.m=o.m,tmp.t=o.t+;
  69. if(!v[tmp.s][tmp.m][tmp.n]){Q.push(tmp);v[tmp.s][tmp.m][tmp.n]=;}
  70. }else{
  71. tmp.s=o.s+o.n,tmp.n=,tmp.m=o.m,tmp.t=o.t+;
  72. if(!v[tmp.s][tmp.m][tmp.n]){Q.push(tmp);v[tmp.s][tmp.m][tmp.n]=;}
  73. }
  74. }
  75. }
  76. }
  77. return -;
  78. }
  79. int main(){
  80. while(~scanf("%d%d%d",&s,&m,&n),s+n+m){
  81. if(s%){puts("NO");continue;}
  82. int ans=bfs();
  83. if(ans==-)puts("NO");
  84. else printf("%d\n",ans);
  85. }
  86. return ;
  87. }

hdu_1495_非常可乐(bfs模拟)的更多相关文章

  1. HDU1495 非常可乐 —— BFS + 模拟

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1495 非常可乐 Time Limit: 2000/1000 MS (Java/Others)    M ...

  2. BFS+模拟 ZOJ 3865 Superbot

    题目传送门 /* BFS+模拟:dp[i][j][p] 表示走到i,j,方向为p的步数为多少: BFS分4种情况入队,最后在终点4个方向寻找最小值:) */ #include <cstdio&g ...

  3. HDU 1495 非常可乐 BFS搜索

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

  4. Hdu 5336 XYZ and Drops (bfs 模拟)

    题目链接: Hdu 5336 XYZ and Drops 题目描述: 有一个n*m的格子矩阵,在一些小格子里面可能会有一些水珠,每个小水珠都有一个size.现在呢,游戏开始咯,在一个指定的空的小格子里 ...

  5. POJ 3414 Pots【bfs模拟倒水问题】

    链接: http://poj.org/problem?id=3414 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22009#probl ...

  6. HDU 1495 非常可乐 BFS 搜索

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

  7. BFS 模拟队列(水题)

    BFS 这道题 觉得比较适合BFS新手入门写,也许大家都以为最入门 的BFS题是在二维图上搜索,但是这道题是线性搜索,更加简单 POJ 3278 Catch That Cow Time Limit:  ...

  8. Vladik and Favorite Game CodeForces - 811D (思维+BFS+模拟+交互题)

    D. Vladik and Favorite Game time limit per test 2 seconds memory limit per test 256 megabytes input ...

  9. HDU-1459.非常可乐(BFS )

    这道题TLE了很多次,原来一直以为将数字化为最简可以让运算更快,但是去了简化之后才发现,真正耗时的就是化简....还和队友学到了用状态少直接数组模拟刚就能过... 本题大意:给出可乐的体积v1,给出两 ...

随机推荐

  1. android studio导入第三方源码模块

    从网上得到的但三方源码模块,如果直接导入到自己的项目里的时候,可能需要比较长的时间,甚至不成功. 在导入之间,还是应该将模块里的 build.gradle 编辑一下,使其与自己的android stu ...

  2. 记录下actionbar的翻译

    http://blog.csdn.net/xyz_lmn/article/details/8132420 嗯,actionbarSherLock不错,viewpagerIndicator也不错.

  3. 旋转数组中的最小数字,剑指offer,P70 二分查找来实现O(logn)的查找

    public class MinNumberInRotatedArray { public int getMinNumInRotatedArray(int[] array) { if(array == ...

  4. appium元素集合定位

    概念:某一类型元素的集合 list对象 来源:-控件复用 -相同的控件类型 -取名重复 语法:driver.findElements(By.id("text1")).get(0) ...

  5. log4j配置示例

    在配置文件中按包名或类名来定义Logger 在程序中按类名取Logger 定义: log4j.rootLogger=debug,stdout log4j.logger.com.mypkg=debug, ...

  6. LeetCode 之 Triangle

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  7. 给hexo添加评论系统

    默认主题 landscape 文件目录,comments为新建的 _config.yml layout -- _partial -- article.ejs |- comments -- disqus ...

  8. hdu1002

    //c//https://github.com/ssdutyuyang199401/hduacm/blob/master/1002.c#include<stdio.h>#include&l ...

  9. SublimeLinter 3中使用jshint

    这货得知已久,在sublime2时期对着教程按部就班的凑合用着...不明所以. 今天无意发现SublimeLinter终于出3了,果断装之,鼓捣1个多小时,磕磕绊绊终于成功工作了.期间看了无数英文文档 ...

  10. 一行一行分析JQ源码学习笔记-02

    1.防止冲突    设置新变量保存