HDU ACM 1495 非常可乐(广搜BFS)
非常可乐
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 42 Accepted Submission(s) : 21
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
Input
Output
Sample Input
7 4 3
4 1 3
0 0 0
Sample Output
NO
3
Author
Source
#include <iostream>
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
struct node
{
int a,b,c,av,bv,cv,step;
};
int ss,n,m,ans;
int f[][][];
int ok(int a,int b,int c)
{
if (a==b && c==) return ;
if (b==c && a==) return ;
if (a==c && b==) return ;
return ;
}
void pour(int* a,int* b,int* av,int* bv,int* step)
{
if (*a>)
{ if (*a>=*bv-*b)
{
*a=*a-(*bv-*b);
*b=*bv;
*step=*step+;
} else
{
*b=*b+*a;
*a=;
*step=*step+;
}
}
return;
}
int bfs()
{
node p;
queue<node> s;
p.a=ss;
p.b=;
p.c=;
p.av=ss;
p.bv=n;
p.cv=m;
p.step=;
s.push(p);
while(!s.empty())
{
node q=s.front();
s.pop();
//a->b
p=q;
pour(&p.a,&p.b,&p.av,&p.bv,&p.step);
if(!f[p.a][p.b][p.c])
{
s.push(p);
f[p.a][p.b][p.c]=;
}
if(ok(p.a,p.b,p.c)) return p.step;
//a->c
p=q;
pour(&p.a,&p.c,&p.av,&p.cv,&p.step);
if(!f[p.a][p.b][p.c])
{
s.push(p);
f[p.a][p.b][p.c]=;
}
if(ok(p.a,p.b,p.c)) return p.step;
//b->c
p=q;
pour(&p.b,&p.c,&p.bv,&p.cv,&p.step);
if(!f[p.a][p.b][p.c])
{
s.push(p);
f[p.a][p.b][p.c]=;
}
if(ok(p.a,p.b,p.c)) return p.step;
//b->a
p=q;
pour(&p.b,&p.a,&p.bv,&p.av,&p.step);
if(!f[p.a][p.b][p.c])
{
s.push(p);
f[p.a][p.b][p.c]=;
}
if(ok(p.a,p.b,p.c)) return p.step;
//c->a
p=q;
pour(&p.c,&p.a,&p.cv,&p.av,&p.step);
if(!f[p.a][p.b][p.c])
{
s.push(p);
f[p.a][p.b][p.c]=;
}
if(ok(p.a,p.b,p.c)) return p.step;
//c->b
p=q;
pour(&p.c,&p.b,&p.cv,&p.bv,&p.step);
if(!f[p.a][p.b][p.c])
{
s.push(p);
f[p.a][p.b][p.c]=;
}
if(ok(p.a,p.b,p.c)) return p.step;
}
return -;
}
int main()
{ while(~scanf("%d%d%d",&ss,&n,&m))
{
if(ss== && n== && m==) break;
if (ss%!=)
{
printf("NO\n");
continue;
}
memset(f,,sizeof(f));
ans=bfs();
if (ans>) printf("%d\n",ans);
else printf("NO\n");
}
return ;
}
HDU ACM 1495 非常可乐(广搜BFS)的更多相关文章
- hdu 1495 非常可乐 广搜
#include<iostream> #include<cstdio> #include<cstring> #include<queue> ][][]; ...
- hdu 1253:胜利大逃亡(基础广搜BFS)
胜利大逃亡 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...
- hdu 2717 Catch That Cow(广搜bfs)
题目链接:http://i.cnblogs.com/EditPosts.aspx?opt=1 Catch That Cow Time Limit: 5000/2000 MS (Java/Others) ...
- HDU 5652(二分+广搜)
题目链接:http://acm.hust.edu.cn/vjudge/contest/128683#problem/E 题目大意:给定一只含有0和1的地图,0代表可以走的格子,1代表不能走的格 子.之 ...
- Combine String HDU - 5707 dp or 广搜
Combine String HDU - 5707 题目大意:给你三个串a,b,c,问a和b是不是恰好能组成c,也就是a,b是不是c的两个互补的子序列. 根据题意就可以知道对于c的第一个就应该是a第一 ...
- HDU 1253 (简单三维广搜) 胜利大逃亡
奇葩!这么简单的广搜居然爆内存了,而且一直爆,一直爆,Orz 而且我也优化过了的啊,尼玛还是一直爆! 先把代码贴上睡觉去了,明天再来弄 //#define LOCAL #include <ios ...
- hdu 1175 连连看 (广搜,注意解题思维,简单)
题目 解析见代码 #define _CRT_SECURE_NO_WARNINGS //这是非一般的最短路,所以广搜到的最短的路不一定是所要的路线 //所以应该把所有的路径都搜索出来,找到最短的转折数, ...
- poj3126 Prime Path 广搜bfs
题目: The ministers of the cabinet were quite upset by the message from the Chief of Security stating ...
- HDU 1072 Nightmare (广搜)
题目链接 Problem Description Ignatius had a nightmare last night. He found himself in a labyrinth with a ...
随机推荐
- POJ 2083 Fractal 分形
去年校赛团队赛就有一道分形让所有大一新生欲生欲死…… 当时就想学了 结果一直拖到…… 今天上午…… 马上要省选了 才会一点基础分形…… 还是自己不够努力啊…… 分形主要是要找到递归点…… 还有深度…… ...
- 安装 sublime package control
import urllib.request,os; pf = 'Package Control.sublime-package'; ipp = sublime.installed_packages_p ...
- chapter 12_2 保存无环的table
保存table有几种方法,选用哪种方法取决于对table的结构作出了哪些限制性的假设 第一个方法: function serialize(o) if type(o) == "number&q ...
- crontab定时任务
使用cron服务,用 service crond status 查看 cron服务状态,如果没有启动则 service crond start启动它, cron服务是一个定时执行的服务,可以通过cro ...
- sql注入绕过union select过滤
# # # #WAF Bypassing Strings: /*!%55NiOn*/ /*!%53eLEct*/ ,,)-- - +union+distinct+select+ +union+dist ...
- MFC模态和非模态对话框编程
MFC中对话框有两种形式,一个是模态对话框(model dialog box),一个是非模态对话框(modeless dialog box). 一.模态对话框(model dialog box) 在程 ...
- 大数据量情况下求top N的问题
上周五的时候去参加了一个面试,被问到了这个问题.问题描述如下: 假如存在一个很大的文件,文件中的每一行是一个字符串.请问在内存有限的情况下(内存无法加载这个文件中的所有内容),如何计算出出现频率最高的 ...
- 第4章 流程控制----编写Java程序,使用while循环语句计算1+1/2!+1/3!+...+1/20!之和
package four; public class fouronetwo { public static void main(String args[]){ double sum = 0,a = 1 ...
- Java&&As3.0 中的final 关键字
Java和AS3.0关键字final有“这是无法改变的”或者“终态的”含义,它可以修饰非抽象类.非抽象类成员方法和变量.你可能出于两种理解而需要阻止改变:设计或效率. 可以修饰的对象: fin ...
- Mysql 锁机制
innodb引擎提供了针对表级锁和行级锁 这个文章说的是行级锁 Lock有两种类型: 1,s-lock(共享锁),拥有该锁的transaction可以对row进行read操作 2,x-lock(排它锁 ...