大家一定觉的运动以后喝可乐是一件很惬意的事情,但是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
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <queue> using namespace std; typedef long long LL;
#define Mem0(x) memset(x, 0, sizeof(x))
#define MemI(x) memset(x, -1, sizeof(x))
#define MemM(x) memset(x, 0x3f, sizeof(x)) const int MAXN = ;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + ; //vis -> all of case of n and m
int vis[][], s, n, m;
struct Node
{
int s, n, m, cnt;
};
queue<Node> q; int bfs()
{
while(!q.empty())
q.pop();
Mem0(vis);
Node now, next;
now.s = s, now.n = , now.m = , now.cnt = ;
q.push(now);
vis[now.n][now.m] = ;
while(!q.empty())
{
now = q.front();
q.pop();
// cout << now.s << " " << now.n << " " << now.m << " " << now.cnt << endl;
int cnt = ;
if( * now.s == s)
cnt++;
if( * now.n == s)
cnt++;
if( * now.m == s)
cnt++;
if(cnt == )
return now.cnt;
// s -> n
next.cnt = now.cnt + ;
if(now.s && now.n != n)
{
int d = n - now.n;
next.s = max(, now.s - d);
next.n = min(n, now.n + now.s);
next.m = now.m;
if(!vis[next.n][next.m])
{
vis[next.n][next.m] = ;
q.push(next);
}
}
// s -> m
if(now.s && now.m != m)
{
int d = m - now.m;
next.s = max(, now.s - d);
next.m = min(m, now.m + now.s);
next.n = now.n;
if(!vis[next.n][next.m])
{
vis[next.n][next.m] = ;
q.push(next);
}
}
// n -> s
if(now.n && now.s != s)
{
int d = s - now.s;
next.n = max(, now.n - d);
next.s = min(s, now.s + now.n);
next.m = now.m;
if(!vis[next.n][next.m])
{
vis[next.n][next.m] = ;
q.push(next);
}
}
// n -> m
if(now.n && now.m != m)
{
int d = m - now.m;
next.n = max(, now.n - d);
next.m = min(m, now.m + now.n);
next.s = now.s;
if(!vis[next.n][next.m])
{
vis[next.n][next.m] = ;
q.push(next);
}
}
//m -> s
if(now.m && now.s != s)
{
int d = s - now.s;
next.m = max(, now.m - d);
next.s = min(s, now.s + now.m);
next.n = now.n;
if(!vis[next.n][next.m])
{
vis[next.n][next.m] = ;
q.push(next);
}
}
// m -> n
if(now.m && now.n != n)
{
int d = n - now.n;
next.m = max(, now.m - d);
next.n = min(n, now.n + now.m);
next.s = now.s;
if(!vis[next.n][next.m])
{
vis[next.n][next.m] = ;
q.push(next);
}
}
}
return ;
} int main()
{
while(cin >> s >> n >> m)
{
if(!s && !n && !m)
break;
if(s % )
{
cout << "NO" << endl;
continue;
}
else
{
int ans = bfs();
if(ans)
cout << ans << endl;
else
cout << "NO" << endl;
}
}
return ;
}
												

hdu1495 bfs搜索、模拟的更多相关文章

  1. hdu 1240:Asteroids!(三维BFS搜索)

    Asteroids! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  2. hiho_1139_二分+bfs搜索

    题目 给定N个点和M条边,从点1出发,到达点T.寻找路径上边的个数小于等于K的路径,求出所有满足条件的路径中最长边长度的最小值. 题目链接:二分     最小化最大值,考虑采用二分搜索.对所有的边长进 ...

  3. hdu--1026--Ignatius and the Princess I(bfs搜索+dfs(打印路径))

    Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  4. BFS搜索

    参考博客:[算法入门]广度/宽度优先搜索(BFS) 适用问题:一个解/最优解 重点:我们怎么运用队列?怎么记录路径? 假设我们要找寻一条从V0到V6的最短路径.(明显看出这条最短路径就是V0-> ...

  5. 【NOIP 2011】Mayan游戏(搜索+模拟)

    描述 Mayan puzzle是最近流行起来的一个游戏.游戏界面是一个7行5列的棋盘,上面堆放着一些方块,方块不能悬空堆放,即方块必须放在最下面一行,或者放在其他方块之上.**游戏通关是指在规定的步数 ...

  6. BFS、模拟:UVa1589/POJ4001/hdu4121-Xiangqi

    Xiangqi Xiangqi is one of the most popular two-player board games in China. The game represents a ba ...

  7. Horse Pro(带负坐标的bfs搜索)

    Horse Pro bfs搜索,但图中存在负值坐标,两种方法解决. 用数组标记,将原点设为300,300 用map标记 http://oj.jxust.edu.cn/contest/Problem?i ...

  8. 天梯赛练习 L3-008 喊山 (30分) bfs搜索

    题目分析: 本题是一题比较简单的bfs搜索题,首先由于数据给的比较多不能直接开二维数组存放,而是用了vector的动态的二维数组的形式存放,对于每个出发点,我们bfs向四周搜索,标记搜索过的点,遇到搜 ...

  9. ICE CAVE(BFS搜索(模拟))

    Description You play a computer game. Your character stands on some level of a multilevel ice cave. ...

随机推荐

  1. [原创]COCOS2DX 3.8 增加protobuf为external库

    此处为记录防止忘记,最近使用COCOS练习一些新的库,所以需要把PROTOBUF编译进去,看上去很麻烦,这里记录一下,以防忘记. 首先下载protobuf 2.5.0版本,下载地址请自行百度. 准备工 ...

  2. 1045 Bode Plot

    题目链接:http://poj.org/problem?id=1045 一道数学物理题, 推理公式:http://www.cnblogs.com/rainydays/archive/2013/01/0 ...

  3. [转]修改github已提交的用户名和邮箱

    改变作者信息 为改变已经存在的 commit 的用户名和/或邮箱地址,你必须重写你 Git repo 的整个历史. 警告:这种行为对你的 repo 的历史具有破坏性.如果你的 repo 是与他人协同工 ...

  4. HDU 4352 XHXJ's LIS (数位DP+LIS+状态压缩)

    题意:给定一个区间,让你求在这个区间里的满足LIS为 k 的数的数量. 析:数位DP,dp[i][j][k] 由于 k 最多是10,所以考虑是用状态压缩,表示 前 i 位,长度为 j,状态为 k的数量 ...

  5. iOS Programming Recipe 6: Creating a custom UIView using a Nib

    iOS Programming Recipe 6: Creating a custom UIView using a Nib JANUARY 7, 2013 BY MIKETT 12 COMMENTS ...

  6. poj—1753 (DFS+枚举)

                                                                                                        ...

  7. memcached整理の分布式集群算法

    memcached如何实现分布式? memcached是一个“分布式内存对象缓存系统”,然而memcached并不像mongodb那样,允许配置多个节点,且节点之间“自动分配数据”,就是说memcac ...

  8. sqlite数据库的char,varchar,text,nchar,nvarchar,ntext的区别(转)

    sqlite数据库存储table1.CHAR.CHAR存储定长数据很方便,CHAR字段上的索引效率级高,比如定义char(10),那么不论你存储的数据是否达到了10个字节,都要占去10个字节的空间,不 ...

  9. var 在linq中的使用

    一:掌握linq,写出超炫的代码 1. var关键字 [隐式类型] 隐式类型 和 匿名类型的不同叫法. 特性 和 属性 2.隐式类型 就是让编译器来推断的一种语法糖. 二:隐式类型的应用场景 1. 简 ...

  10. vcenter安装错误The DSN is pointing to anunspported ODBC driver...

    在安装vcenter server中采用现有独立sql server数据库时出现下列错误. 这是由于当前独立数据库版本和当前系统的客户端驱动不匹配.导致我们在odbc中配置dsn无法正常运行. 如sq ...