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) ...
随机推荐
- ajaxfileupload踩过的坑
首先ajaxfileupload-jQuery.handleError is not a function这个错误,百度之后发现解决办法就是把 handleError: function( s, xh ...
- hdu 4740 The Donkey of Gui Zhou(暴力搜索)
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4740 [题意]: 森林里有一只驴和一只老虎,驴和老虎互相从来都没有见过,各自自己走过的地方不能走第二次 ...
- java第三课:分支结构、循环结构
分支结构 1.if后面可以不加{},但是不建议,容易产生误解2.写程序的时候先防屌丝测试,再写逻辑,否则会出错3.if:一条分支 if else:两条分支 if...else if...:多条分支(结 ...
- winform访问url传参有返回值
using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Net;usi ...
- 【高斯消元】BZOJ 1770: [Usaco2009 Nov]lights 燈
Description 貝希和她的閨密們在她們的牛棚中玩遊戲.但是天不從人願,突然,牛棚的電源跳閘了,所有的燈都被關閉了.貝希是一個很膽小的女生,在伸手不見拇指的無盡的黑暗中,她感到驚恐,痛苦與絕望. ...
- 无法为请求的 Configuration 对象创建配置文件 错误原因
Configuration config = WebConfigurationManager.OpenWebConfiguration("~"); 无法为请求的 Configura ...
- PAT-乙级-1040. 有几个PAT(25)
1040. 有几个PAT(25) 时间限制 120 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CAO, Peng 字符串APPAPT中包含了两个单 ...
- SGU 186
总是拆最短的链子 连接长的链子 贪心.... #include <cstdio> #include <cstring> #include <cmath> #i ...
- 关于NGUI中的自适应和对齐机制
原地址:http://blog.sina.com.cn/s/blog_62df69790101gy7t.html 1 关于美术效果图 美术出效果图总是基于某个固定尺寸.移动设备的分辨率却有太多不同的方 ...
- linux Ubuntu12 设置root用户登录图形界面
Ubuntu 12.04默认是不允许root登录的,在登录窗口只能看到普通用户和访客登录.以普通身份登陆Ubuntu后我们需要做一些修改,普通用户登录后,修改系统配置文件需要切换到超级用户模式,在终端 ...