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

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

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

注:写的很丑

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cmath>
#include <set>
#include <queue>
#include <cstring>
using namespace std;
typedef long long LL;
const int maxn=+;
const int INF=0x3f3f3f3f;
int a[maxn][maxn][maxn];
struct asd
{
int x,y,z;
} o,t;
queue<asd>q;
int main()
{
int s,n,m;
while(~scanf("%d%d%d",&s,&n,&m),s)
{
if(s%)
{
printf("NO\n");
continue;
}
memset(a,-,sizeof(a));
a[s][][]=;
o.x=s,o.y=,o.z=;
while(!q.empty())q.pop();
q.push(o);
int ans=-;
while(!q.empty())
{
o=q.front();
q.pop();
int cnt=;
if(o.x==s/)cnt++;
if(o.y==s/)cnt++;
if(o.z==s/)cnt++;
if(cnt==)
{
ans=a[o.x][o.y][o.z];
break;
}
if(o.x)
{
if(o.y<n)
{
int k=n-o.y;
if(o.x<=k)t.y=o.y+o.x,t.x=;
else t.y=n,t.x=o.x-k;
t.z=o.z;
if(a[t.x][t.y][t.z]==-)
a[t.x][t.y][t.z]=a[o.x][o.y][o.z]+,q.push(t);
}
if(o.z<m)
{
int k=m-o.z;
if(o.x<=k)t.z=o.z+o.x,t.x=;
else t.z=m,t.x=o.x-k;
t.y=o.y;
if(a[t.x][t.y][t.z]==-)
a[t.x][t.y][t.z]=a[o.x][o.y][o.z]+,q.push(t);
}
}
if(o.y)
{
if(o.x<s)
{
int k=s-o.x;
if(o.y<=k)t.x=o.x+o.y,t.y=;
else t.x=s,t.y=o.y-k;
t.z=o.z;
if(a[t.x][t.y][t.z]==-)
a[t.x][t.y][t.z]=a[o.x][o.y][o.z]+,q.push(t);
}
if(o.z<m)
{
int k=m-o.z;
if(o.y<=k)t.z=o.z+o.y,t.y=;
else t.z=m,t.y=o.y-k;
t.x=o.x;
if(a[t.x][t.y][t.z]==-)
a[t.x][t.y][t.z]=a[o.x][o.y][o.z]+,q.push(t);
}
}
if(o.z)
{
if(o.y<n)
{
int k=n-o.y;
if(o.z<=k)t.y=o.y+o.z,t.z=;
else t.y=n,t.z=o.z-k;
t.x=o.x;
if(a[t.x][t.y][t.z]==-)
a[t.x][t.y][t.z]=a[o.x][o.y][o.z]+,q.push(t);
}
if(o.x<s)
{
int k=s-o.x;
if(o.z<=k)t.x=o.z+o.x,t.z=;
else t.x=s,t.z=o.z-k;
t.y=o.y;
if(a[t.x][t.y][t.z]==-)
a[t.x][t.y][t.z]=a[o.x][o.y][o.z]+,q.push(t);
}
}
}
if(ans==-)printf("NO\n");
else printf("%d\n",ans);
}
return ;
}

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. OC 数据类型之间的转换方法

      NSNumber转NSString: 假设现有一NSNumber的变量A,要转换成NSString类型的B 方法如下: NSNumberFormatter* numberFormatter = [ ...

  2. WEB开发人员必知的20+HTML5技巧(转)

    互联网科技发展的速度真可谓惊人的快,一个稍不留神,你就可能无法跟上它的步伐. HTML5的变化和更新也压倒不少人,这篇文章将向大家介绍一些最基本也非常必要的 HTML技巧. 1. 新的文档类型(Doc ...

  3. [转]LINQ操作数据库

    查询表达式(LINQ)简介 C#3.0新语特性和改进,这些新特性在我们编写程序时为我们提供了非常大的帮助.从这篇开始,我们开始一起来探讨LINQ. LINQ是Language Integrated Q ...

  4. UnitOfWork机制的实现和注意事项

    UnitOfWork机制 /*一点牢骚: * UnitOfWork机制的蛋疼之处: *    UnitOfWork机制,决定了插入新的实体前,要预先设置数据库中的主键Id,尽管数据库自己生产主键. * ...

  5. Cloud Insight 和 BearyChat 第一次合体,好紧张!

    说到 ChatOps 我们可能立刻想到是 Slack(啥?没听过?哦!),但是由于国内网络和语言的问题你可能无法拥有很好的体验了.那就把目光转回国内吧,国内的话就不得不提到 BearyChat 等 C ...

  6. JS 封装类

    function HighchartsObj(id, type) { var that = this; this.options = { chart : { renderTo : id, type : ...

  7. 97. Interleaving String

    题目: Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given: ...

  8. 对C#中的web访问mysql数据库的一些知识点进行了整理归纳总结

    基本对比 使用方式 使用场合 优缺点 是否需要安装 需要的dll网址 引用方式 程序内引用 程序初期确定使用MySql,前期添加引用 大多数情况下使用在类文件内,多数使用于aspx,ashx等带有后置 ...

  9. world符号大全

    ■特殊符号:·⊙①⊕◎Θ⊙●○¤㊣㈱@の■□★☆◆◇◣◢ ◤◥▲△▼▽⊿◢ ▂ ▃ ▄ ▅ ▆ ▇ █ ▉ ▊▋▌▍▎▏■ ▓ 回 □ 〓≡↑↓→←↘↙♀♂┇┅|$ @ * & # ※ 卍 卐 ...

  10. js标点符号全局匹配

    var modelCode = node.modelCode.replace(/\./g, '\_'); 注意后面的  "\" <script language=" ...