HDU1495 非常可乐
来源:互联网 发布:保险库升级数据大全 编辑:程序博客网 时间:2024/05/17 06:14
非常可乐
时间限制:Java: 2000 ms / Others: 1000 ms内存限制: Java: 32768 KB / Others: 32768 KB问题描述
大家一定觉的运动以后喝可乐是一件很惬意的事情,但是seeyou却不这么认为。因为每次当seeyou买了可乐以后,阿牛就要求和seeyou一起分享这一瓶可乐,而且一定要喝的和seeyou一样多。但seeyou的手中只有两个杯子,它们的容量分别是N 毫升和M 毫升 可乐的体积为S (S<101)毫升 (正好装满一瓶) ,它们三个之间可以相互倒可乐 (都是没有刻度的,且 S==N+M,101>S>0,N>0,M>0) 。聪明的ACMER你们说他们能平分吗?如果能请输出倒可乐的最少的次数,如果不能输出"NO"。
输入说明
三个整数 : S 可乐的体积 , N 和 M是两个杯子的容量,以"0 0 0"结束。
输出说明
如果能平分的话请输出最少要倒的次数,否则输出"NO"。
输入样例
7 4 34 1 30 0 0
输出样例
NO3
来源
seeyou
解题思路:本题用广度优先搜索解答,就是3个容器,容量为S,N,M,其中S中装满可乐,要把可乐平分到2个容器,求最短路劲。
用标记数组flag[105][105][105]标记已出现过的状态,再次出现时不入队,剪枝。搜索时就把3个容器里的可乐倒来倒去就行了,一但遇到符合条件的,直接跳出搜索,输出答案即可。
解题思路:本题用广度优先搜索解答,就是3个容器,容量为S,N,M,其中S中装满可乐,要把可乐平分到2个容器,求最短路劲。
用标记数组flag[105][105][105]标记已出现过的状态,再次出现时不入队,剪枝。搜索时就把3个容器里的可乐倒来倒去就行了,一但遇到符合条件的,直接跳出搜索,输出答案即可。
#include<cstdio>#include<cstring>#include<queue>using namespace std;int flag[105][105][105];int s,n,m;double av;struct node{ int x; int y; int z; int step;};void dao(int &x,int max_x,int &y,int max_y) //把x中得可乐倒到y中{ if(x==0||y==max_y) //无法倒,不处理 return ; y+=x; //全部倒进去 x=0; if(y>max_y) //y溢出 { x=y-max_y; y=max_y; }}bool ok(int x,int y,int z){ int k=0; if(x==av) k++; if(y==av) k++; if(z==av) k++; if(k==2) return true; return false;}int bfs(){ node first,next; queue<node> q; first.x=s; first.y=0; first.z=0; first.step=0; q.push(first); flag[first.x][first.y][first.z]=1; while(!q.empty()) { //printf("first.x=%d first.y=%d first.z=%d first.step\n",first.x,first.y,first.z,first.step); first=q.front(); q.pop(); next.step=first.step+1; //x->y next.x=first.x; next.y=first.y; next.z=first.z; dao(next.x,s,next.y,n); if(ok(next.x,next.y,next.z)) //已经平分到2个容器 return next.step; if(!flag[next.x][next.y][next.z]) //该情况为现过 flag[next.x][next.y][next.z]=1,q.push(next); //x->z next.x=first.x; next.y=first.y; next.z=first.z; dao(next.x,s,next.z,m); if(ok(next.x,next.y,next.z)) return next.step; if(!flag[next.x][next.y][next.z]) flag[next.x][next.y][next.z]=1,q.push(next); //y->x next.x=first.x; next.y=first.y; next.z=first.z; dao(next.y,n,next.x,s); if(ok(next.x,next.y,next.z)) return next.step; if(!flag[next.x][next.y][next.z]) flag[next.x][next.y][next.z]=1,q.push(next); //y->z next.x=first.x; next.y=first.y; next.z=first.z; dao(next.y,n,next.z,m); if(ok(next.x,next.y,next.z)) return next.step; if(!flag[next.x][next.y][next.z]) flag[next.x][next.y][next.z]=1,q.push(next); //z->x next.x=first.x; next.y=first.y; next.z=first.z; dao(next.z,m,next.x,s); if(ok(next.x,next.y,next.z)) return next.step; if(!flag[next.x][next.y][next.z]) flag[next.x][next.y][next.z]=1,q.push(next); //z->y next.x=first.x; next.y=first.y; next.z=first.z; dao(next.z,m,next.y,n); if(ok(next.x,next.y,next.z)) return next.step; if(!flag[next.x][next.y][next.z]) flag[next.x][next.y][next.z]=1,q.push(next); } return -1;}int main(){ while(scanf("%d%d%d",&s,&n,&m)&&s) { memset(flag,0,sizeof(flag)); av=(double)s/2; if(s%2!=0) printf("NO\n"); else { int k=bfs(); if(k==-1) printf("NO\n"); else printf("%d\n",k); } } return 0;}
- HDU1495:非常可乐(BFS)
- HDU1495 非常可乐
- hdu1495非常可乐
- HDU1495:非常可乐(BFS)
- HDU1495 非常可乐 【BFS】
- HDU1495-非常可乐-BFS
- hdu1495非常可乐bfs
- HDU1495 非常可乐
- hdu1495 非常可乐--BFS
- HDU1495 非常可乐
- hdu1495非常可乐
- 非常可乐HDU1495
- HDU1495:非常可乐
- HDU1495-非常可乐
- HDU1495非常可乐
- hdu1495 非常可乐【bfs】
- 非常可乐 HDU1495
- hdu1495非常可乐(bfs)
- phpExcel在封装
- Android tp的虚拟按键(virtual key)处理
- JRTP和jthread在linux下的编译测试
- C语言的复合文字
- QT 声明全局变量
- HDU1495 非常可乐
- 免费学习android开发技术分享
- 程序员必备的基础知识及书目推荐
- HDU1963 && POJ2063:Investment(完全背包)
- Poj题不完全分类
- iOS解决两个静态库的冲突 duplicate symbol
- Linux 固件子系统----如何更新固件
- [C#]读PSD图片
- 字符串的各种hash函数对比