HDU 1495 非常可乐 BFS搜索
题意:有个为三个杯子(杯子没有刻度),体积为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搜索的更多相关文章
- HDU 1495 非常可乐 BFS 搜索
http://acm.hdu.edu.cn/showproblem.php?pid=1495 题目就不说了, 说说思路! 倒可乐 无非有6种情况: 1. S 向 M 倒 2. S 向 N 倒 3. N ...
- HDU 1495 非常可乐 bfs 难度:1
http://acm.hdu.edu.cn/showproblem.php?pid=1495 第三个杯子的盛水量可由前两个杯子得到,而前两个杯子状态总数在100*100以内,穷举可实现 #includ ...
- (step4.2.5)hdu 1495(非常可乐——BFS)
题目大意:输入三个整数 a,b,c. a : 可乐瓶的容量,b: 甲杯的容量 ,c: 乙杯的容量.问能否用这三个被来实现饮料的平分???如果可以输出倒饮料的次数, 否则输出NO 解题思路:BFS ...
- HDU 1495 非常可乐 BFS
题目大意:中文题不说了. 题目思路:我有同学用GCD数论写出来的代码很简洁,但是很抱歉,数论蒟蒻,我觉得比赛的时候我没办法推出.如果用BFS的话思路很简单的,就是6方向广搜,只不过稍微麻烦点.具体看代 ...
- HDU - 1495 非常可乐 bfs互倒三杯水
非常可乐 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- BFS(倒水问题) HDU 1495 非常可乐
题目传送门 /* BFS:倒水问题,当C是奇数时无解.一共有六种情况,只要条件符合就入队,我在当该状态vised时写了continue 结果找了半天才发现bug,泪流满面....(网上找份好看的题解都 ...
- HDU 1495 非常可乐(数论,BFS)
非常可乐 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submi ...
- 非常可乐---hdu 1495(BFS)
http://acm.hdu.edu.cn/showproblem.php?pid=1495 题意: 有3个杯子a b c:a=b+c:然后刚开始时只有a是满的,其它为空的,然后a b c三个之间互相 ...
- HDU 1495 非常可乐(BFS倒水问题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1495 题目大意:只有两个杯子,它们的容量分别是N 毫升和M 毫升 可乐的体积为S (S<101) ...
随机推荐
- ORA-12514 TNS 监听程序当前无法识别连接描述符中请求服务 的解决方法
今天用PL/SQL连接虚拟机中的Oracle数据库,发现报了“ORA-12514 TNS 监听程序当前无法识别连接描述符中请求服务”错误,也许你也遇到过,原因如下: oracle安装成功后,一直未停止 ...
- 第一章 Spring整体框架和环境搭建
1.Spring 的整体架构 Spring框架主要由7大模块组成,它们提供了企业级开发需要的所有功能,而且每个模块都可以单独使用,也可以和其他模块组合使用,灵活且方便的部署可以使开发的程序更加简洁灵活 ...
- spring +hibernate 启动优化【转】
最近在负责一个大项目,项目组成员包括项目经理大概10个人左右.项目技术用struts+spring+hibernate实现.项目的规模相对来说是比较大的,总共有10大模块,每个大模块又分为有十几个.甚 ...
- bzoj 1242: Zju1015 Fishing Net 弦图判定
1242: Zju1015 Fishing Net弦图判定 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 214 Solved: 81[Submit ...
- [转载]再谈iframe自适应高度
Demo页面:主页面 iframe_a.html ,被包含页面 iframe_b.htm 和 iframe_c.html 下面开始讲: 通过Google搜索iframe 自适应高度,结果5W多条,搜索 ...
- PHP中CURL方法curl_setopt()函数的一些参数 (转)
bool curl_setopt (int ch, string option, mixed value) curl_setopt()函数将为一个CURL会话设置选项.option参数是你想要的设置, ...
- Firefly Http通信简单介绍
原地址:http://www.9miao.com/question-15-54042.html 首先创建firefly工程,firefly-admin.py createproject httptes ...
- UIWebView和UIWebViewDelegate的基本用法
UIWebView和UIWebViewDelegate的基本用法 一.UIWebView主要有三种方法实现页面的装载,分别是: 1. (void)loadRequest:(NSURLRequest * ...
- 非常好的Demo网站
http://www.xdemo.org/
- [Unity菜鸟] Unity读XML
1. 在Unity中调试可行,发布成exe可行,发布成web不行 Application.dataPath 在Unity中调试是在“..Assets”文件夹下, 发布成exe文件是在“..yourNa ...