BFS题

一共有六种状态转移 一一枚举就好 设置一个标记数组。 用二重循环可以很清晰的解决代码长的问题

#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
int v[];
int sign[][][];
struct cup//记录遍历中3个水杯容藏可乐情况
{
int v[];
int step;
}temp; void pour(int a,int b)//倒水函数,把a杯子中的可乐倒到b杯子中
{
int sum=temp.v[a]+temp.v[b];
if(sum>=v[b])
temp.v[b]=v[b];
else
temp.v[b]=sum;
temp.v[a]=sum-temp.v[b];
} void bfs()
{
int i,j;
queue<cup>q;
cup cnt;
cnt.v[]=v[];
cnt.v[]=;
cnt.v[]=;
cnt.step=;
q.push(cnt);
memset(sign,,sizeof(sign));
sign[v[]][][]=;
while(!q.empty())
{
cnt=q.front();
q.pop();
if(cnt.v[]==cnt.v[]&&cnt.v[]==)
{
printf("%d\n",cnt.step);
return ;
} for(i=;i<;++i)
{
for(j=;j<;++j)
{
if(i!=j)//自己不倒水给自己
{
temp=cnt;//每个水位情况都要把所有操作枚举一遍,所以都要赋值为原始水位情况
pour(i,j);
if(!sign[temp.v[]][temp.v[]][temp.v[]])
{
temp.step++;
q.push(temp);
sign[temp.v[]][temp.v[]][temp.v[]]=;
}
}
}
}
}
printf("NO\n");
} int main()
{
while(scanf("%d%d%d",&v[],&v[],&v[])&&v[]||v[]||v[])
{
if(v[]>v[])
{
int t=v[];
v[]=v[];
v[]=t;
}
bfs();
}
return ;
}

非常可乐 HDU1495的更多相关文章

  1. HDU-1495 非常可乐(BFS)

    广搜的灵活应用题: 非常可乐 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  2. 非常可乐(杭电hdu1495)bfs

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

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

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

  4. HDU1495 非常可乐

    解题思路:简单的宽搜,见代码: #include<cstdio> #include<cstring> #include<algorithm> #include< ...

  5. HDU-1495 非常可乐 (嵌套结构体-广搜 对比 一般广搜)

    题意 大家一定觉的运动以后喝可乐是一件很惬意的事情,但是seeyou却不这么认为.因为每次当seeyou买了可乐以后,阿牛就要求和seeyou一起分享这一瓶可乐,而且一定要喝的和seeyou一样多.但 ...

  6. HDU1495 非常可乐(BFS/数论)

    大家一定觉的运动以后喝可乐是一件很惬意的事情,但是seeyou却不这么认为.因为每次当seeyou买了可乐以后,阿牛就要求和seeyou一起分享这一瓶可乐,而且一定要喝的和seeyou一样多.但see ...

  7. 【HDU - 1495】非常可乐

    -->非常可乐  Descriptions: 大家一定觉的运动以后喝可乐是一件很惬意的事情,但是seeyou却不这么认为.因为每次当seeyou买了可乐以后,阿牛就要求和seeyou一起分享这一 ...

  8. hdoj 1495 非常可乐【bfs隐式图】

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

  9. HDU1495(bfs)

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

随机推荐

  1. luogu 4401 矿工配餐 多维dp

    五维dp,记忆化搜索会MLE超内存,所以用滚动数组,十分经典 五维dp #include <bits/stdc++.h> using namespace std; ; ][][][],la ...

  2. Tomcat 配置目录

    TOMCAT 1.主目录下有bin,conf,lib,logs,temp,webapps,work 1.bin目录主要是用来存放tomcat的命令 2.conf目录主要是用来存放tomcat的一些配置 ...

  3. JavaScript之事件绑定多个序列执行方法

    //一种事件绑定多个方法:以加载事件为例 function addEventLoad(func,isLog) { var oldOnLoad = window.onload; if (typeof w ...

  4. 第16月第5天 performSelector afterDelay cancel dispatch_semaphore_wait

    1. //不延时,可能会导致界面黑屏并卡住一会 [self performSelector:@selector(startScan) withObject:nil afterDelay:0.3]; - ...

  5. Maxwell入门

    1 下载tar包 Download binary distro: https://github.com/zendesk/maxwell/releases/download/v1.19.4/maxwel ...

  6. css scrollbar样式设置

    参考链接:https://segmentfault.com/a/1190000012800450

  7. 解决Ubuntu的root账号无法登录SSH问题-Permission denied, please try again.

    http://www.cnblogs.com/yixius/articles/6971054.html

  8. numpy 中 shape_base提供的tile方法

    tile函数 来自于numpy.lib.shape_base 功能:重复某个数组. 比如说tile(A, n), 功能是将数组A重复n次,构成一个新的数组(行数只有1个) 比如说tile(A, n, ...

  9. 【逆向知识】VS程序反汇编找main函数

    工具:OllyICE 调试快捷键说明: F2键:设置断点,只要在光标定位的位置 F4键:程序运行到光标处 F7键:单步步入.功能同单步步过(F8)类似,区别是遇到 CALL 等子程序时会进入其中,进入 ...

  10. 【转】Python之列表生成式、生成器、可迭代对象与迭代器

    [转]Python之列表生成式.生成器.可迭代对象与迭代器 本节内容 语法糖的概念 列表生成式 生成器(Generator) 可迭代对象(Iterable) 迭代器(Iterator) Iterabl ...