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

  本题大意:给出可乐的体积v1,给出两个杯子v2和v3,要求v2 + v3 == v1,每次只能从一个杯子倒到另一个杯子,问最少倒多少次可以将可乐平分。

  思路:最后可乐肯定在可乐瓶和大杯子里面,直接BFS暴搜就行了。

  参考代码:

  1. #include <cstdio>
  2. #include <queue>
  3. #include <cstring>
  4. using namespace std;
  5.  
  6. struct node {
  7. int cur[], step;
  8. } now, Next;
  9.  
  10. const int maxn = + ;
  11. int v[];
  12. int ans, t;
  13. bool flag;
  14. bool vis[maxn][maxn][maxn];
  15.  
  16. void bfs() {
  17. if(v[] & ) {
  18. flag = false;
  19. return;
  20. }
  21. queue <node> Q;
  22. now.cur[] = v[], now.cur[] = , now.cur[] = , now.step = ;
  23. vis[v[]][][] = true;
  24. Q.push(now);
  25. while(!Q.empty()) {
  26. now = Q.front();
  27. Q.pop();
  28. if(now.cur[] == now.cur[] && now.cur[] == ) {
  29. ans = now.step;
  30. return;
  31. }
  32. for(int i = ; i < ; i ++) {//i -> j
  33. for(int j = ; j < ; j ++) {
  34. if(i != j) {
  35. Next = now;
  36. t = now.cur[i] + now.cur[j];
  37. if(t > v[j]) Next.cur[j] = v[j];
  38. else Next.cur[j] = t;
  39. Next.cur[i] = t - Next.cur[j];
  40. if(!vis[Next.cur[]][Next.cur[]][Next.cur[]]) {
  41. Next.step = now.step + ;
  42. Q.push(Next);
  43. vis[Next.cur[]][Next.cur[]][Next.cur[]] = true;
  44. }
  45. }
  46. }
  47. }
  48. }
  49. flag = false;
  50. }
  51.  
  52. int main () {
  53. while(~scanf("%d %d %d", &v[], &v[], &v[])) {
  54. memset(vis, false, sizeof(vis));
  55. if(v[] == && v[] == && now.cur[] == ) break;
  56. flag = true, ans = ;
  57. if(v[] < v[]) {
  58. t = v[];
  59. v[] = v[];
  60. v[] = t;
  61. }
  62. bfs();
  63. if(flag) printf("%d\n", ans);
  64. else printf("NO\n");
  65. }
  66. return ;
  67. }

  下面这个代码的效率要高一点,区别在于倒水方式不同。

  1. #include <cstdio>
  2. #include <queue>
  3. #include <cstring>
  4. using namespace std;
  5.  
  6. struct node {
  7. int cur[], step;
  8. } now, Next;
  9.  
  10. const int maxn = + ;
  11. int v[];
  12. int ans, t;
  13. bool flag;
  14. bool vis[maxn][maxn][maxn];
  15.  
  16. void bfs() {
  17. if(v[] & ) {
  18. flag = false;
  19. return;
  20. }
  21. queue <node> Q;
  22. now.cur[] = v[], now.cur[] = , now.cur[] = , now.step = ;
  23. vis[v[]][][] = true;
  24. Q.push(now);
  25. while(!Q.empty()) {
  26. now = Q.front();
  27. Q.pop();
  28. if(now.cur[] == now.cur[] && now.cur[] == ) {
  29. ans = now.step;
  30. return;
  31. }
  32. for(int i = ; i < ; i ++) {//i -> j
  33. for(int j = ; j < ; j ++) {
  34. if(i != j) {
  35. Next = now;
  36. if(j == ) {
  37. Next.cur[j] = now.cur[j] + now.cur[i];
  38. Next.cur[i] = ;
  39. } else if(j == ) {
  40. t = v[] - now.cur[j];
  41. Next.cur[i] = now.cur[i] - t;
  42. Next.cur[i] = Next.cur[i] > ? Next.cur[i] : ;
  43. Next.cur[j] = now.cur[j] + now.cur[i];
  44. Next.cur[j] = Next.cur[j] < v[] ? Next.cur[j] : v[];
  45. } else {
  46. t = v[] - now.cur[j];
  47. Next.cur[i] = now.cur[i] - t;
  48. Next.cur[i] = Next.cur[i] > ? Next.cur[i] : ;
  49. Next.cur[j] = now.cur[j] + now.cur[i];
  50. Next.cur[j] = Next.cur[j] < v[] ? Next.cur[j] : v[];
  51. }
  52. if(!vis[Next.cur[]][Next.cur[]][Next.cur[]]) {
  53. Next.step = now.step + ;
  54. Q.push(Next);
  55. vis[Next.cur[]][Next.cur[]][Next.cur[]] = true;
  56. }
  57. }
  58. }
  59. }
  60. }
  61. flag = false;
  62. }
  63.  
  64. int main () {
  65. while(~scanf("%d %d %d", &v[], &v[], &v[])) {
  66. memset(vis, false, sizeof(vis));
  67. if(v[] == && v[] == && now.cur[] == ) break;
  68. flag = true, ans = ;
  69. if(v[] < v[]) {
  70. t = v[];
  71. v[] = v[];
  72. v[] = t;
  73. }
  74. bfs();
  75. if(flag) printf("%d\n", ans);
  76. else printf("NO\n");
  77. }
  78. return ;
  79. }

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

  1. HDU 1495 非常可乐 BFS 搜索

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

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

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

  3. HDU 1495 非常可乐 BFS搜索

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

  4. HDU 1495 非常可乐 BFS

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

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

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

  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. Centos7 Minimal 安装后 初始化配置

    安装完成后初始化配置 1:更新yum yum upgrade 2: 安装基础命令 #yum -y install vim* lrzsz gcc-c++ pcre pcre-devel zlib zli ...

  2. hive案例

    数据倾斜: 操作• Join on a.id=b.id• Group by• Count Distinct count(groupby)• 原因• key分布不均导致的• 人为的建表疏忽• 业务数据特 ...

  3. 25_ajax请求_使用fetch

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. Xcode 8 注释快捷键失效

    sudo /usr/libexec/xpccachectl 重启

  5. js 对象创建设计模式

    创建js对象可以使用多种模式,每种模式有着不同的特点:如下: 1.工厂模式:创建一个函数,在函数中实例化一个对象,当每次调用函数时,就实例化一个对象,并返回这个对象: 我们知道,对象是引用形式的,每次 ...

  6. vue:监听数据

    1:普通的监听: data () { return { watchNum:1, } }, watch: { watchNum(newValue, oldValue) { console.log(old ...

  7. Kotlin语言编程技巧集

    空语句 Kotlin 语言中的空语句有 {} Unit when (x) { 1 -> ... 2 -> ... else -> {} // else -> Unit } Wh ...

  8. [namespace]PHP命名空间的动态访问 & 使用技巧

    ----------------------------------------------------------------------------------------------- /* | ...

  9. java资源文件解读

    [1]从零开始建工程 最简单的:http://www.cnblogs.com/alipayhutu 其中d)换作: ApplicationContext context = new ClassPath ...

  10. 11.枚举类.md

    目录 1.定义: 2.枚举类和普通类的区别: 2.1枚举类的简单构建: 2.2枚举类的成员变量.方法和构造 2.3实现接口的枚举类 1.定义: 2.枚举类和普通类的区别: 枚举类的默认修饰符是 pub ...