M - 非常可乐HDU1495 BFS
来源:互联网 发布:飞龙签名设计软件 编辑:程序博客网 时间:2024/06/10 06:22
M - 非常可乐
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Submit Status Practice HDU 1495
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)为状态,之间的倒水关系为边,进行BFS//#include<bits/stdc++.h>#include<iostream>#include<cstdio>#include<vector>#include<stack>#include<cstring>#include<queue>#include<algorithm>using namespace std;const int maxn=105;const int inf=200000;#define lson rt<<1,l,m#define rson rt<<1|1,m+1,r#define For(i,n) for(int i=0;i<(n);i++)template<class T>inline T read(T&x){ char c; while((c=getchar())<=32); bool ok=false; if(c=='-')ok=true,c=getchar(); for(x=0; c>32; c=getchar()) x=x*10+c-'0'; if(ok)x=-x; return x;}template<class T> inline void read_(T&x,T&y){ read(x); read(y);}template<class T> inline void write(T x){ if(x<0)putchar('-'),x=-x; if(x<10)putchar(x+'0'); else write(x/10),putchar(x%10+'0');}template<class T>inline void writeln(T x){ write(x); putchar('\n');}// -------IO template------struct node{ int s,n,m; node(int a,int b,int c) { s=a;n=b;m=c; }};int N,M,S;bool ok(node a){ int cnt=0; if(a.s==S/2) cnt++; if(a.n==S/2) cnt++; if(a.m==S/2) cnt++; if(cnt>=2)return true; return false;}int vis[maxn][maxn][maxn];bool flag=0;void bfs(int s,int n,int m){ queue<node> q; q.push(node(s,n,m)); vis[s][n][m]=1; memset(vis,0,sizeof(vis)); while(!q.empty()) { node tmp=q.front();q.pop(); // printf("%d %d %d\n",tmp.s,tmp.n,tmp.m); if(ok(tmp)) { printf("%d\n",vis[tmp.s][tmp.n][tmp.m]); flag=false; return ; } ///s->n if(tmp.s>0&&tmp.n<N) { int t=min(N-tmp.n,tmp.s); if(!vis[tmp.s-t][tmp.n+t][tmp.m]) { q.push(node(tmp.s-t,tmp.n+t,tmp.m)); vis[tmp.s-t][tmp.n+t][tmp.m]=vis[tmp.s][tmp.n][tmp.m]+1; } } ///s->m if(tmp.s>0&&tmp.m<M) { int t=min(tmp.s,M-tmp.m); if(!vis[tmp.s-t][tmp.n][tmp.m+t]) { q.push(node(tmp.s-t,tmp.n,tmp.m+t)); vis[tmp.s-t][tmp.n][tmp.m+t]=vis[tmp.s][tmp.n][tmp.m]+1; } } ///n->s if(tmp.n>0&&tmp.s<S) { int t=min(tmp.n,S-tmp.s); if(!vis[tmp.s+t][tmp.n-t][tmp.m]) { q.push(node(tmp.s+t,tmp.n-t,tmp.m)); vis[tmp.s+t][tmp.n-t][tmp.m]=vis[tmp.s][tmp.n][tmp.m]+1; } } ///n->m if(tmp.n>0&&tmp.m<M) { int t=min(tmp.n,M-tmp.m); if(!vis[tmp.s][tmp.n-t][tmp.m+t]) { q.push(node(tmp.s,tmp.n-t,tmp.m+t)); vis[tmp.s][tmp.n-t][tmp.m+t]=vis[tmp.s][tmp.n][tmp.m]+1; } } ///m->s if(tmp.s<S&&tmp.m>0) { int t=min(tmp.m,S-tmp.s); if(!vis[tmp.s+t][tmp.n][tmp.m-t]) { q.push(node(tmp.s+t,tmp.n,tmp.m-t)); vis[tmp.s+t][tmp.n][tmp.m-t]=vis[tmp.s][tmp.n][tmp.m]+1; } } ///m->n if(tmp.m>0&&tmp.n<N) { int t=min(tmp.m,N-tmp.n); if(!vis[tmp.s][tmp.n+t][tmp.m-t]) { q.push(node(tmp.s,tmp.n+t,tmp.m-t)); vis[tmp.s][tmp.n+t][tmp.m-t]=vis[tmp.s][tmp.n][tmp.m]+1; } } }}int main(){#ifndef ONLINE_JUDGE freopen("in.txt","r",stdin);#endif // ONLINE_JUDGE int i,j,k,t; while(~scanf("%d%d%d",&S,&N,&M)&&(S||N||M)) { flag=true; if(S&1){printf("NO\n");continue;} bfs(S,0,0); if(flag)printf("NO\n"); } return 0;}
0 0
- M - 非常可乐HDU1495 BFS
- HDU1495:非常可乐(BFS)
- HDU1495:非常可乐(BFS)
- HDU1495 非常可乐 【BFS】
- HDU1495-非常可乐-BFS
- hdu1495非常可乐bfs
- hdu1495 非常可乐--BFS
- hdu1495 非常可乐【bfs】
- hdu1495非常可乐(bfs)
- hdu1495 非常可乐(bfs)
- HDU1495 非常可乐(BFS)
- hdu1495非常可乐(BFS)
- hdu1495 非常可乐 bfs基础
- HDU1495 非常可乐 BFS+模拟
- hdu1495 非常可乐(简单BFS)
- hdu1495——非常可乐(BFS)
- HDU1495 非常可乐(模拟,广搜BFS)
- HDU1495 非常可乐 —— BFS + 模拟
- 相对熵(KL散度)
- JVM 垃圾回收笔记
- MAC和PHY的区别
- 使用StarUML画类图
- 必须认真学习的算法书
- M - 非常可乐HDU1495 BFS
- Linked List Cycle
- 刘宝庆老师<快速打造中层高效执行力>助理:林静文 电话:18818552408
- HDOJ 1215七夕节
- Linux 下 make 命令 及 make 笔记
- devstack
- 编写一个函数,将一个数字字符串转换成这个字符串对应的数字(包括正浮点数、负浮点数);函数原型:double my_atof(char *str)
- Android 编程下 Touch 事件的分发和消费机制
- JDBC封装