大家一定觉的运动以后喝可乐是一件很惬意的事情,但是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. 实践作业3:白盒测试----第三次小组会DAY8

    我们小组于12月11日在东九教学楼召开了会议,小组第二次会议中就讨论了被测系统中风险最高的模块,于是选择管理员的教师添加模块,针对代码展开静态评审.这一次会议,小组主要讨论了应该从哪些方面来测评代码的 ...

  2. python基础借鉴

    这是一个链接 这个是egon的链接 吴沛其的博客

  3. atom插件记录

    ├── Zen@0.16.4 写作的时候用,很给力 ├── atom-beautify@0.29.9 美化一切代码 ├── autocomplete-paths@1.0.2 路径自动提示 ├── au ...

  4. dd命令使用

    一.dd命令的解释 dd:用指定大小的块拷贝一个文件,并在拷贝的同时进行指定的转换. 注意:指定数字的地方若以下列字符结尾,则乘以相应的数字:b=512:c=1:k=1024:w=2 参数注释: 1. ...

  5. Typora的图片根目录设置,

    需求:使Typora的图片,设置到指定的文件里. 方便上传与转移. 步骤: 1 位置: 编辑 ->图片工具->设置图片根目录. 2 .Preference -> Editor -&g ...

  6. SQL的Join语法

    对于SQL的Join,在学习起来可能是比较乱的.我们知道,SQL的Join语法有很多inner的,有outer的,有left的,有时候,对于Select出来的结果集是什么样子有点不是很清楚.Codin ...

  7. Hadoop(HDFS、YARN、HBase、Hive和Spark等)默认端口表

    端口 作用 9000 fs.defaultFS,如:hdfs://172.25.40.171:9000 9001 dfs.namenode.rpc-address,DataNode会连接这个端口 50 ...

  8. linux 命令学习-网络相关配置

    网络配置相关 网卡配置文件:etc/sysconfig/network-scripts/ifcfg-eth0 DNS 配置文件 etc/resolv.conf 主机配置文件 etc/sysconfig ...

  9. struts2使用验证文件实现校验

    原创 struts2框架提供了一种基于验证文件的输入验证方式,将验证规则保存在特定的验证文件中. 验证文件的命名规则 一般情况下,验证文件的命名规则是:Action类名-validation.xml. ...

  10. Docker 1.3.3/1.4.0 发布下载,Linux 容器引擎

    Docker 1.3.3 发布,下载地址: https://github.com/docker/docker/archive/v1.3.3.zip 改进记录包括: Security Fix path ...