hdu 1495(BFS)
非常可乐
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 11034 Accepted Submission(s): 4458
家一定觉的运动以后喝可乐是一件很惬意的事情,但是seeyou却不这么认为。因为每次当seeyou买了可乐以后,阿牛就要求和seeyou一起分享这
一瓶可乐,而且一定要喝的和seeyou一样多。但seeyou的手中只有两个杯子,它们的容量分别是N 毫升和M 毫升 可乐的体积为S
(S<101)毫升 (正好装满一瓶) ,它们三个之间可以相互倒可乐 (都是没有刻度的,且 S==N+M,101>S>0,N>0,M>0)
。聪明的ACMER你们说他们能平分吗?如果能请输出倒可乐的最少的次数,如果不能输出"NO"。
4 1 3
0 0 0
3
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#include <stdlib.h>
using namespace std; struct Node{
int x,y,z,step;
};
int a,b,c;
bool vis[][][];
int bfs(){
memset(vis,false,sizeof(vis));
queue<Node> q;
Node s;
s.x = a,s.y = ,s.z = ,s.step = ;
vis[a][][]=true;
q.push(s);
while(!q.empty()){
Node now = q.front();
q.pop();
if(now.x==a/&&now.y==a/||now.x==a/&&now.z==a/||now.y==a/&&now.z==a/){
return now.step;
}
Node next;
if(now.x!=){ /// a -> b
if(now.x>b-now.y){ ///还可以倒满b
next.x =now.x-(b-now.y);
next.y = b;
next.z = now.z;
}else{
next.x = ;
next.y =now.x+now.y;
next.z = now.z;
}
next.step=now.step+;
if(!vis[next.x][next.y][next.z]){
vis[next.x][next.y][next.z] = true;
q.push(next);
}
}
if(now.x!=){ ///a->c
if(now.x>c-now.z){
next.x = now.x - (c-now.z);
next.y = now.y;
next.z = c;
}else{
next.x = ;
next.y = now.y;
next.z =now.z+now.x;
}
next.step=now.step+;
if(!vis[next.x][next.y][next.z]){
vis[next.x][next.y][next.z] = true;
q.push(next);
}
}
if(now.y!=){ ///b->a
if(now.y>a-now.x){
next.x = a;
next.y = now.y - (a-now.x);
next.z = now.z;
}else{
next.x = now.x+now.y;
next.y = ;
next.z =now.z;
}
next.step=now.step+;
if(!vis[next.x][next.y][next.z]){
vis[next.x][next.y][next.z] = true;
q.push(next);
}
}
if(now.y!=){ ///b->c
if(now.y>c-now.z){
next.x = now.x;
next.y = now.y - (c-now.z);
next.z = c;
}else{
next.x = now.x;
next.y = ;
next.z =now.z+now.y;
}
next.step=now.step+;
if(!vis[next.x][next.y][next.z]){
vis[next.x][next.y][next.z] = true;
q.push(next);
}
}
if(now.z!=){ ///c->a
if(now.z>a-now.x){
next.x = a;
next.y = now.y;
next.z = now.z - (a-now.x);
}else{
next.x = now.x+now.z;
next.y = now.y;
next.z = ;
}
next.step=now.step+;
if(!vis[next.x][next.y][next.z]){
vis[next.x][next.y][next.z] = true;
q.push(next);
}
}
if(now.z!=){ ///c->b
if(now.z>b-now.y){
next.x = now.x;
next.y = b;
next.z = now.z - (b-now.y);
}else{
next.x = now.x;
next.y = now.y+now.z;
next.z = ;
}
next.step=now.step+;
if(!vis[next.x][next.y][next.z]){
vis[next.x][next.y][next.z] = true;
q.push(next);
}
}
}
return -;
}
int main(){
while(scanf("%d%d%d",&a,&b,&c)!=EOF,a+b+c){
if(a%==){
printf("NO\n");
continue;
}
int ans = bfs();
if(ans==-){
printf("NO\n");
}else{
printf("%d\n",ans);
}
}
}
hdu 1495(BFS)的更多相关文章
- HDU - 1495 bfs [kuangbin带你飞]专题一
模拟倒水的过程,每次可以把第i个杯子的水向第j个杯子里面倒,这可能出现新的状态,不停的更新状态,指导某两个杯子的水等于S/2说明找到答案,如果所有状态搜索完毕仍然不能均分,则退出. 注意:如果S是奇数 ...
- 【BFS】HDU 1495
直达–> HDU 1495 非常可乐 相似题联动–>POJ 3414 Pots 题意:中文题,不解释. 思路:三个杯子倒来倒去,最后能让其中两个平分即可.可能性六种.判定的时候注意第三个杯 ...
- BFS(倒水问题) HDU 1495 非常可乐
题目传送门 /* BFS:倒水问题,当C是奇数时无解.一共有六种情况,只要条件符合就入队,我在当该状态vised时写了continue 结果找了半天才发现bug,泪流满面....(网上找份好看的题解都 ...
- hdu 4531 bfs(略难)
题目链接:点我 第一次不太清楚怎么判重,现在懂了,等下次再做 /* *HDU 4531 *BFS *注意判重 */ #include <stdio.h> #include <stri ...
- HDU 1495 非常可乐 BFS 搜索
http://acm.hdu.edu.cn/showproblem.php?pid=1495 题目就不说了, 说说思路! 倒可乐 无非有6种情况: 1. S 向 M 倒 2. S 向 N 倒 3. N ...
- 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) ...
- HDU - 1495 非常可乐 【BFS】
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1495 思路 首先 如果可乐的体积 是奇数 那么是无解的 然后 如果能够得到两杯 都是一般容量的可乐 那 ...
随机推荐
- 从零开始学Linux系统(五)用户管理和权限管理
权限管理: 常识: chmod U-所有者 g-所属组 O-其他人r-4-可读 w-2-可写 x-1-可执行 s-4-SetUID s-2-SetGID t-1-粘着位 注:目 ...
- jsp弹出新窗口代码
1.最基本的弹出窗口代码其实代码非常简单: <SCRIPT LANGUAGE="javascript"> <!-- window.open (page.html) ...
- tinyxml源码解析(上)
转载于:http://www.cnblogs.com/marchtea/archive/2012/11/09/2762669.html 前言: 前段时间做功能可行性的时候简单的使用了tinyxml.在 ...
- springboot的application.properties与.yml的区别
现在我们的application.properties文件内容是: server.port=8090 server.session-timeout=30 server.context-path= se ...
- HDU1507二分图
Uncle Tom's Inherited Land* Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- javascript功能封装
实现方式其实很简单,我在代码打上注释,大家就懂了! var _date=[],dateData=["1月","2月","3月",&qu ...
- git代码冲突
如果系统中有一些配置文件在服务器上做了配置修改,然后后续开发又新添加一些配置项的时候, 在发布这个配置文件的时候,会发生代码冲突: error: Your local changes to the f ...
- linux shell读取配置文件
配置文件CoverageInfo FTP_URL=ftp://svn-fb.sicent.com:21/jenkins/Jifei_Repo/OL-2/IDC_Platform/bar_seats_c ...
- c# “XXX::Invoke”类型的已垃圾回收委托进行了回调。这可能会导致应用程序崩溃、损坏和数据丢失。向非托管代码传递委托时,托管应用程序必须让这些委托保持活动状态,直到确信不会再次调用它们。
症状描述如下: 如果将一个委托作为函数指针从托管代码封送到非托管代码,并且在对该委托进行垃圾回收后对该函数指针发出了一个回调,则将激活 callbackOnCollectedDelegate 托管调试 ...
- 【BZOJ4816】【SDOI2017】数字表格 [莫比乌斯反演]
数字表格 Time Limit: 50 Sec Memory Limit: 128 MB[Submit][Status][Discuss] Description Doris刚刚学习了fibonac ...