广搜的灵活应用题:

非常可乐

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3633    Accepted Submission(s): 1514

Problem Description
大家一定觉的运动以后喝可乐是一件很惬意的事情,但是seeyou却不这么认为。因为每次当seeyou买了可乐以后,阿牛就要求和seeyou一起分享这一瓶可乐,而且一定要喝的和seeyou一样多。但seeyou的手中只有两个杯子,它们的容量分别是N 毫升和M 毫升 可乐的体积为S (S<101)毫升 (正好装满一瓶) ,它们三个之间可以相互倒可乐 (都是没有刻度的,且 S==N+M,101>S>0,N>0,M>0) 。聪明的ACMER你们说他们能平分吗?如果能请输出倒可乐的最少的次数,如果不能输出"NO"。
 
Input
三个整数 : S 可乐的体积 , N 和 M是两个杯子的容量,以"0 0 0"结束。
 
Output
如果能平分的话请输出最少要倒的次数,否则输出"NO"。
 
Sample Input
7 4 3
4 1 3
0 0 0
 
Sample Output
NO 3
 //一共6种情况;all->big,all->small,big->small,big->all,small->all,small->big进行bfs搜索,如果未被搜到过,
//就入队列,如果出现两个瓶子里水相同就返回输出。(总水量为奇数时无解)
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
struct node
{
int s,n,m;
int step;
};
int hash[][][];
int s,n,m; int BFS()
{
queue<node>q;
node cur,next;
cur.s=s;
cur.n=;
cur.m=;
cur.step=;
hash[cur.s][cur.n][cur.m]=;
q.push(cur);
while(!q.empty())
{
cur=q.front();
q.pop();
if((cur.n== && cur.m==cur.s)||(cur.m== && cur.n==cur.s)||( cur.s== && cur.n==cur.m)) return cur.step;
if(cur.n!=)
{
if(cur.n>s-cur.s)
{
next.n=cur.n-(s-cur.s);
next.s=s;
next.m=cur.m;
if(hash[next.s][next.n][next.m]==)
{
next.step=cur.step+;
q.push(next);
hash[next.s][next.n][next.m]=;
}
}
else
{
next.n=;
next.s=cur.s+cur.n;
next.m=cur.m;
if(hash[next.s][next.n][next.m]==)
{
next.step=cur.step+;
q.push(next);
hash[next.s][next.n][next.m]=;
}
}
if(cur.n>m-cur.m)
{
next.n=cur.n-(m-cur.m);
next.m=m;
next.s=cur.s;
if(hash[next.s][next.n][next.m]==)
{
next.step=cur.step+;
q.push(next);
hash[next.s][next.n][next.m]=;
}
}
else
{
next.n=;
next.m=cur.m+cur.n;
next.s=cur.s;
if(hash[next.s][next.n][next.m]==)
{
next.step=cur.step+;
q.push(next);
hash[next.s][next.n][next.m]=;
}
}
} if(cur.m)
{
if(cur.m>s-cur.s)
{
next.m=cur.n-(s-cur.s);
next.s=s;
next.n=cur.n;
if(hash[next.s][next.n][next.m]==)
{
next.step=cur.step+;
q.push(next);
hash[next.s][next.n][next.m]=;
}
}
else
{
next.m=;
next.s=cur.s+cur.m;
next.n=cur.n;
if(hash[next.s][next.n][next.m]==)
{
next.step=cur.step+;
q.push(next);
hash[next.s][next.n][next.m]=;
}
}
if(cur.m>n-cur.n)
{
next.m=cur.m-(n-cur.n);
next.n=n;
next.s=cur.s;
if(hash[next.s][next.n][next.m]==)
{
next.step=cur.step+;
q.push(next);
hash[next.s][next.n][next.m]=;
}
}
else
{
next.m=;
next.n=cur.m+cur.n;
next.s=cur.s;
if(hash[next.s][next.n][next.m]==)
{
next.step=cur.step+;
q.push(next);
hash[next.s][next.n][next.m]=;
}
}
} if(cur.s)
{
if(cur.s>n-cur.n)
{
next.s=cur.s-(n-cur.n);
next.n=n;
next.m=cur.m;
if(hash[next.s][next.n][next.m]==)
{
next.step=cur.step+;
q.push(next);
hash[next.s][next.n][next.m]=;
}
}
else
{
next.s=;
next.n=cur.s+cur.n;
next.m=cur.m;
if(hash[next.s][next.n][next.m]==)
{
next.step=cur.step+;
q.push(next);
hash[next.s][next.n][next.m]=;
}
}
if(cur.s>m-cur.m)
{
next.s=cur.s-(m-cur.m);
next.m=m;
next.n=cur.n;
if(hash[next.s][next.n][next.m]==)
{
next.step=cur.step+;
q.push(next);
hash[next.s][next.n][next.m]=;
}
}
else
{
next.s=;
next.m=cur.m+cur.s;
next.n=cur.n;
if(hash[next.s][next.n][next.m]==)
{
next.step=cur.step+;
q.push(next);
hash[next.s][next.n][next.m]=;
}
}
}
} return ;
}
int main()
{
int ans;
while(scanf("%d%d%d",&s,&n,&m),s||n||m)
{
memset(hash,,sizeof(hash)); if(s%) {printf("NO\n");continue;} ans=BFS();
if(ans) printf("%d\n",ans);
else printf("NO\n");
}
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搜索

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

  5. HDU 1495 非常可乐 BFS

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

  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. Codevs 2776 寻找代表元(二分图匹配)

    2776 寻找代表元 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 黄金 Gold 题目描述 Description 广州二中苏元实验学校一共有n个社团,分别用1到n编号. 广州二 ...

  2. 最近使用Qt遇到的一些小问题解决办法总结

    1. 我想获取当前星期几这样的,我没在API里面找到这样的函数,但是我找到了今天是第几天这样的,所以自己转换一下就OK了: typedef struct { int numInWeek; QStrin ...

  3. Linux系统的启动流程以及做个小小的Linux

    内核的作用     进程管理:进程间切换     内存管理:内存空间分割为内核空间和用户空间     IO管理:对底层硬件的使用必须由内来实现,不能由用户空间进程来实现     文件系统管理     ...

  4. 移动端开发(使用webuploader上传图片,客户端交互,修改alert弹窗等)

    之前实习做的一个移动端的页面 需要的功能有图片上传 点击客户端的返回按钮 有提示(即与客户端有交互) 遇到不少的坑 总结一下问题 1.图片上传功能  使用工具 百度的webuploader 暂时遇到的 ...

  5. data:image/png;base64是什么

    大家可能注意到了,网页上有些图片的src或css背景图片的url后面跟了一大串字符,比如: data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAkAAAAJ ...

  6. PHPCMS二次开发教程

    PHPCMS V9 结构设计 根目录|–api  结构文件目录|–caches 缓存文件目录   |– configs 系统配置文件目录   |– caches_* 系统缓存目录|–phpcms  p ...

  7. mongodb 非 admin 库 认证登陆失败 原因(百度好多都 是渣)db.addUser() 请走开。

    首先先晒一下log 日志错误信息 2016-07-13T22:19:43.667+0800 I ACCESS [conn4] authenticate db: finddemo { aut henti ...

  8. Sublime Text 3 LESS、SASS、SCSS高亮插件、提示插件

    为sublime text 添加LESS语法高亮 功能:LESS高亮插件   下载   https://packagecontrol.io/packages/LESS 简介:用LESS的同学都知道,s ...

  9. WPF 得一些问题汇总

    1.CallMethodAction <TextBox Height="30" Name="txtUserName" Width="160&qu ...

  10. 转载的在DOS下操作mysql

    转载原文地址:http://www.server110.com/mysql/201309/1070.html 一.连接MYSQL. 格式: mysql -h主机地址 -u用户名 -p用户密码 1.例1 ...