非常可乐

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. 使用XML传递数据

    HTML <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF- ...

  2. WebSocket添加事件监听器(6)

    WebSocket编程遵循异步编程模型;打开socket后,只需要等待事件发生,而不需要主动向服务器轮询,所以需要在WebSocket对象中添加回调函数来监听事件. WebSocket对象有三个事件: ...

  3. [UVA1625]Color Length

    题面在这里 description 输入两个长度分别为\(n\)和\(m\)的颜色序列,要求按顺序合并成同一个序列,即每次可以把一个序列开头的颜色放到新序列的尾部. 对于每个颜色\(c\)来说,其跨度 ...

  4. jsp电子商务 购物车实现之二 登录和分页篇

    登录页面核心代码 <div id="login"> <h2>用户登陆</h2> <form method="post" ...

  5. js保存用户名与密码

    <script>   window.onload = function(){     var oForm = document.getElementById('loginForm');   ...

  6. 2017福建省赛 FZU2272~2283

    1.FZU2272 Frog 传送门:http://acm.fzu.edu.cn/problem.php?pid=2272 题意:鸡兔同笼通解 题解:解一个方程组直接输出就行 代码如下: #inclu ...

  7. 新手如何更换自己喜欢的背景以及此背景的css码

    以下内容为转载(对于css码可以自己写当然也可以去网上搜现成的): 更换背景教学:https://jingyan.baidu.com/album/fc07f9897c730412ffe519c0.ht ...

  8. React 获取 url 参数 —— this.props.match

    在 react 组件的  componentDidMount 方法中打印一下 this.props,在浏览器控制台中查看输出如下: 其中页面的 url 信息全都包含在 match 字段中,以地址 lo ...

  9. 图论:2-SAT

    先象征性地描述一下问题:一组(或者一个)东西有且仅有两种选择,要么选这个,要么选那个,还有一堆的约束条件 图论问题,当然是建边跑图喽 给出模型: 模型一:两者(A,B)不能同时取 那么选择了A就只能选 ...

  10. 最适合初学者学习的idea教程

    https://github.com/judasn/IntelliJ-IDEA-Tutorial