非常可乐

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 22067 Accepted Submission(s): 8968

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

【题意】:给三个数字 s n m s=n+m s在1到100之间

就是个倒水问题,可以互相倒来倒去,一共有六种倒法,现在要求最少经过多少步就能平分那么多水 。

【分析】:之前因为min的顺序没写到最前面导致ed没有初始化,以及120行有个笔误把st写成ed

【代码】:

#include<cstdio>
#include<string>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<cstring>
#include<set>
#include<queue>
#include<algorithm>
#include<vector>
#include<map>
#include<cctype>
#include<stack>
#include<sstream>
#include<list>
#include<assert.h>
#include<bitset>
#include<numeric>
#define debug() puts("++++")
#define gcd(a,b) __gcd(a,b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a,b,sizeof(a))
#define sz size()
#define be begin()
#define pu push_up
#define pd push_down
#define cl clear()
#define lowbit(x) -x&x
#define all 1,n,1
#define rep(i,x,n) for(int i=(x); i<(n); i++)
#define in freopen("in.in","r",stdin)
#define out freopen("out.out","w",stdout)
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e18;
const int maxn = 500; //数组之前开小了...但是一直显示TLE而不是RE???我爆哭
const int maxm = 1e6 + 10;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int dx[] = {-1,1,0,0};
const int dy[] = {0,0,1,-1};
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int a,b,c;
int v[105][105][105]; struct node
{
int x,y,z,step;
}st,ed;
int check(int x,int y,int z)
{
return (x==a/2 && y==a/2) || (x==a/2 && z==a/2) || (y==a/2 && z==a/2);
}
queue<node> q;
void bfs()
{
ms(v,0);
while(!q.empty()) q.pop();
//初始化三个杯子:可乐杯满的;b、c杯空的
st.x = a;//可乐杯子满的
st.y = 0;//b杯子空
st.z = 0;//c杯子空
st.step = 0;
v[a][0][0] = 1; //标记杯子状态
q.push(st);
while(!q.empty())
{
st = q.front();
q.pop();
if(check(st.x,st.y,st.z)) //满足条件直接输出
{
printf("%d\n",st.step);
return ;
} //a——>b
if(st.x>0 && st.y<b)
{
//两种情况,一种是把A里的水全倒到B还没有把B倒满
//另一种是,A里的水还没倒完B就满了,所以取两者最小值
ed.y = min(b,st.x+st.y);
ed.x = st.x - (ed.y-st.y);
ed.z = st.z;
ed.step = st.step + 1;
if(!v[ed.x][ed.y][ed.z])
{
v[ed.x][ed.y][ed.z]=1;
q.push(ed);
}
}
//a——>c
if(st.x>0 && st.z<c)
{
ed.z = min(c,st.x+st.z);
ed.x = st.x - (ed.z-st.z);
ed.y = st.y;
ed.step = st.step + 1;
if(!v[ed.x][ed.y][ed.z])
{
v[ed.x][ed.y][ed.z]=1;
q.push(ed);
}
}
//b——>a
if(st.y>0 && st.x<a)
{
ed.x = min(a,st.x+st.y);
ed.y = st.y - (ed.x-st.x);
ed.z = st.z;
ed.step = st.step + 1;
if(!v[ed.x][ed.y][ed.z])
{
v[ed.x][ed.y][ed.z]=1;
q.push(ed);
}
}
//b——>c
if(st.y>0 && st.z<c)
{
ed.z = min(c,st.y+st.z);
ed.x = st.x;
ed.y = st.y - (ed.z-st.z);
ed.step = st.step + 1;
if(!v[ed.x][ed.y][ed.z])
{
v[ed.x][ed.y][ed.z]=1;
q.push(ed);
}
}
//c——>a
if(st.z>0 && st.x<a)
{
ed.x = min(a,st.x+st.z);
ed.y = st.y;
ed.z = st.z - (ed.x-st.x);
ed.step = st.step + 1;
if(!v[ed.x][ed.y][ed.z])
{
v[ed.x][ed.y][ed.z]=1;
q.push(ed);
}
}
//c——>b
if(st.z>0 && st.y<b)
{
ed.y = min(b,st.y+st.z);
ed.x = st.x;
ed.z = st.z - (ed.y-st.y);
ed.step = st.step + 1;
if(!v[ed.x][ed.y][ed.z])
{
v[ed.x][ed.y][ed.z]=1;
q.push(ed);
}
}
}
printf("NO\n");
}
int main()
{
while(~scanf("%d%d%d",&a,&b,&c),a+b+c)
{
while(!q.empty()) q.pop();
if(a&1) {puts("NO"); continue;}
bfs();
}
}
/*
7 4 3
4 1 3
0 0 0
Sample Output
NO
3
*/

【可以使用数组装x y z,把倒水逻辑封装起来,就不要写6次只要写1次】

#include<bits/stdc++.h>
using namespace std;
#define ms(a,b) memset(a,b,sizeof(a))
int v[105][105][105];
int c[5];
struct node
{
int c[5];
int s;
}tmp,temp;
void pour(int a,int b)
{
int sum = temp.c[a] + temp.c[b];
if(sum>=c[b])
temp.c[b]=c[b];
else
temp.c[b]=sum;
temp.c[a] = sum - temp.c[b];
} int ok(int x,int y,int z)
{
return (x==c[0]/2 && y==c[0]/2) || (x==c[0]/2 && z==c[0]/2) || (y==c[0]/2 && z==c[0]/2);
} void bfs()
{
ms(v,0);
queue<node> q;
tmp.c[0]=c[0];
tmp.c[1]=tmp.c[2]=tmp.s=0;
v[c[0]][0][0]=1;
q.push(tmp);
while(!q.empty())
{
tmp = q.front();
q.pop();
if(ok(tmp.c[0],tmp.c[1],tmp.c[2]))
{
printf("%d\n",tmp.s);
return ;
}
for(int i=0;i<3;i++) //枚举倒水c0 、c1、c2代表三个杯子
{
if(tmp.c[i]>0) //不能倒完
{
for(int j=0;j<3;j++) //枚举接水
{
if(i==j) continue; //不能自己倒给自己!所以筛出6种状态(9种去掉0+0、1+1、2+2)
temp = tmp; //中间变量保存原始值,因为是平行而非覆盖
pour(i,j); //i——>j 变量要和temp保持一致,名字不能是别的
if(!v[temp.c[0]][temp.c[1]][temp.c[2]])
{
temp.s++;
v[temp.c[0]][temp.c[1]][temp.c[2]] = 1;
q.push(temp);
}
}
}
}
}
printf("NO\n");
}
int main()
{
while(~scanf("%d%d%d",&c[0],&c[1],&c[2]), c[0]+c[1]+c[2])
{
if(c[0]&1)
{
printf("NO\n");
continue;
}
bfs();
}
}

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倒水问题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1495 题目大意:只有两个杯子,它们的容量分别是N 毫升和M 毫升 可乐的体积为S (S<101) ...

  9. HDU 1495 非常可乐(数论,BFS)

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

随机推荐

  1. JDK的弃儿:Vector、Stack、Hashtable、Enumeration

    随着JDK的发展,一些设计缺陷或者性能不足的类库难免会被淘汰,最常见的就是Vector.Stack.HashTable和Enumeration了. Vector(@since 1.0) 首先看看Vec ...

  2. FAQ: SNMP on NetScaler Appliance

    FAQ: SNMP on NetScaler Appliance https://support.citrix.com/article/CTX122436 https://docs.citrix.co ...

  3. WIN8 打开图片内置管理员无法激活此应用

    1.运行 gpedit.msc 2.计算机配置.windows设置.安全设置.本地策略.安全选项.“用户账户控制用于内置管理员账户的管理员批准模式”  改为已启用 3.重启电脑

  4. [NOIP2018 TG D2T2]填数游戏

    题目大意:$NOIP2018\;TG\;D2T2$ 题解:在skip2004的博客基础上修改的,也是暴搜. 说明一下把vector改成数组并不可以通过此题,记录. 结论:在$m>n+1$时答案为 ...

  5. CFS/FQ/PQ调度与WRR负载均衡

    动机 五一临近,四月也接近尾声,五一节乃小长假的最后一天.今天是最后一天工作日,竟然感冒了,半夜里翻来覆去无法安睡,加上窗外大飞机屋里小飞机(也就是蚊子)的骚扰,实在是必须起来做点有意义的事了!    ...

  6. namesilo注册域名用来做域名邮箱

    重要的话说三遍: (一定不要再国内注册域名,不要买国内的空间) (一定不要再国内注册域名,不要买国内的空间) (一定不要再国内注册域名,不要买国内的空间) 使用的是腾讯企业邮箱,有一个缺点:不支持自定 ...

  7. Java中Class<T>与Class<?>的区别

    E - Element (在集合中使用,因为集合中存放的是元素) T - Type(Java 类) K - Key(键) V - Value(值) N - Number(数值类型) ? - 表示不确定 ...

  8. tomcat内存配置及配置参数详解

    1.jvm内存管理机制: 1)堆(Heap)和非堆(Non-heap)内存 按照官方的说法:“Java 虚拟机具有一个堆,堆是运行时数据区域,所有类实例和数组的内存均从此处分配.堆是在 Java 虚拟 ...

  9. 转载:Apache commons开源工具简介

    Apache Commons是一个非常有用的工具包,解决各种实际的通用问题,下面是一个简述表,详细信息访问http://jakarta.apache.org/commons/index.html Be ...

  10. HDU1013 Digital Roots

    http://acm.hdu.edu.cn/showproblem.php?pid=1013 #include<iostream> #include "cstdio" ...