题意:有个为三个杯子(杯子没有刻度),体积为s,n,m,s=m+n,

刚开始只有体积为s的杯子装满可乐,可以互相倒,问你最少的次数使可乐均分,如果没有结果,输出-1;

分析:直接互相倒就完了,BFS模拟

注:写的很丑

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <algorithm>
  4. #include <string>
  5. #include <cmath>
  6. #include <set>
  7. #include <queue>
  8. #include <cstring>
  9. using namespace std;
  10. typedef long long LL;
  11. const int maxn=+;
  12. const int INF=0x3f3f3f3f;
  13. int a[maxn][maxn][maxn];
  14. struct asd
  15. {
  16. int x,y,z;
  17. } o,t;
  18. queue<asd>q;
  19. int main()
  20. {
  21. int s,n,m;
  22. while(~scanf("%d%d%d",&s,&n,&m),s)
  23. {
  24. if(s%)
  25. {
  26. printf("NO\n");
  27. continue;
  28. }
  29. memset(a,-,sizeof(a));
  30. a[s][][]=;
  31. o.x=s,o.y=,o.z=;
  32. while(!q.empty())q.pop();
  33. q.push(o);
  34. int ans=-;
  35. while(!q.empty())
  36. {
  37. o=q.front();
  38. q.pop();
  39. int cnt=;
  40. if(o.x==s/)cnt++;
  41. if(o.y==s/)cnt++;
  42. if(o.z==s/)cnt++;
  43. if(cnt==)
  44. {
  45. ans=a[o.x][o.y][o.z];
  46. break;
  47. }
  48. if(o.x)
  49. {
  50. if(o.y<n)
  51. {
  52. int k=n-o.y;
  53. if(o.x<=k)t.y=o.y+o.x,t.x=;
  54. else t.y=n,t.x=o.x-k;
  55. t.z=o.z;
  56. if(a[t.x][t.y][t.z]==-)
  57. a[t.x][t.y][t.z]=a[o.x][o.y][o.z]+,q.push(t);
  58. }
  59. if(o.z<m)
  60. {
  61. int k=m-o.z;
  62. if(o.x<=k)t.z=o.z+o.x,t.x=;
  63. else t.z=m,t.x=o.x-k;
  64. t.y=o.y;
  65. if(a[t.x][t.y][t.z]==-)
  66. a[t.x][t.y][t.z]=a[o.x][o.y][o.z]+,q.push(t);
  67. }
  68. }
  69. if(o.y)
  70. {
  71. if(o.x<s)
  72. {
  73. int k=s-o.x;
  74. if(o.y<=k)t.x=o.x+o.y,t.y=;
  75. else t.x=s,t.y=o.y-k;
  76. t.z=o.z;
  77. if(a[t.x][t.y][t.z]==-)
  78. a[t.x][t.y][t.z]=a[o.x][o.y][o.z]+,q.push(t);
  79. }
  80. if(o.z<m)
  81. {
  82. int k=m-o.z;
  83. if(o.y<=k)t.z=o.z+o.y,t.y=;
  84. else t.z=m,t.y=o.y-k;
  85. t.x=o.x;
  86. if(a[t.x][t.y][t.z]==-)
  87. a[t.x][t.y][t.z]=a[o.x][o.y][o.z]+,q.push(t);
  88. }
  89. }
  90. if(o.z)
  91. {
  92. if(o.y<n)
  93. {
  94. int k=n-o.y;
  95. if(o.z<=k)t.y=o.y+o.z,t.z=;
  96. else t.y=n,t.z=o.z-k;
  97. t.x=o.x;
  98. if(a[t.x][t.y][t.z]==-)
  99. a[t.x][t.y][t.z]=a[o.x][o.y][o.z]+,q.push(t);
  100. }
  101. if(o.x<s)
  102. {
  103. int k=s-o.x;
  104. if(o.z<=k)t.x=o.z+o.x,t.z=;
  105. else t.x=s,t.z=o.z-k;
  106. t.y=o.y;
  107. if(a[t.x][t.y][t.z]==-)
  108. a[t.x][t.y][t.z]=a[o.x][o.y][o.z]+,q.push(t);
  109. }
  110. }
  111. }
  112. if(ans==-)printf("NO\n");
  113. else printf("%d\n",ans);
  114. }
  115. return ;
  116. }

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

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. ORA-12514 TNS 监听程序当前无法识别连接描述符中请求服务 的解决方法

    今天用PL/SQL连接虚拟机中的Oracle数据库,发现报了“ORA-12514 TNS 监听程序当前无法识别连接描述符中请求服务”错误,也许你也遇到过,原因如下: oracle安装成功后,一直未停止 ...

  2. 第一章 Spring整体框架和环境搭建

    1.Spring 的整体架构 Spring框架主要由7大模块组成,它们提供了企业级开发需要的所有功能,而且每个模块都可以单独使用,也可以和其他模块组合使用,灵活且方便的部署可以使开发的程序更加简洁灵活 ...

  3. spring +hibernate 启动优化【转】

    最近在负责一个大项目,项目组成员包括项目经理大概10个人左右.项目技术用struts+spring+hibernate实现.项目的规模相对来说是比较大的,总共有10大模块,每个大模块又分为有十几个.甚 ...

  4. bzoj 1242: Zju1015 Fishing Net 弦图判定

    1242: Zju1015 Fishing Net弦图判定 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 214  Solved: 81[Submit ...

  5. [转载]再谈iframe自适应高度

    Demo页面:主页面 iframe_a.html ,被包含页面 iframe_b.htm 和 iframe_c.html 下面开始讲: 通过Google搜索iframe 自适应高度,结果5W多条,搜索 ...

  6. PHP中CURL方法curl_setopt()函数的一些参数 (转)

    bool curl_setopt (int ch, string option, mixed value) curl_setopt()函数将为一个CURL会话设置选项.option参数是你想要的设置, ...

  7. Firefly Http通信简单介绍

    原地址:http://www.9miao.com/question-15-54042.html 首先创建firefly工程,firefly-admin.py createproject httptes ...

  8. UIWebView和UIWebViewDelegate的基本用法

    UIWebView和UIWebViewDelegate的基本用法 一.UIWebView主要有三种方法实现页面的装载,分别是: 1. (void)loadRequest:(NSURLRequest * ...

  9. 非常好的Demo网站

    http://www.xdemo.org/

  10. [Unity菜鸟] Unity读XML

    1. 在Unity中调试可行,发布成exe可行,发布成web不行 Application.dataPath 在Unity中调试是在“..Assets”文件夹下, 发布成exe文件是在“..yourNa ...