大家一定觉的运动以后喝可乐是一件很惬意的事情,但是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. 使用virtualBox安装CentOS 6.3的详细步骤

    由于前几天把系统升级到win7了,原先安装的Linux虚拟机都不存在了.基于学习,这次安装选择的是CentOS 6.3版本. 下面就看看具体的安装步骤: 名称可以随便填写,类型选择Linux,版本选择 ...

  2. Generated by NetworkManager、ubuntu DNS设置丢失(network-manager造成的情况)

    方法一:去掉重启 方法二:卸载network-manager 实测网络不稳,经常掉线(kalinux2.0环境)

  3. iOS基础教程:在建好的项目中加入CoreData[转]

    这几天在做一个ios的小项目,项目中需要对数据进行基本的增删改查操作.于是就想用一把CoreData.但在创建项目初期,没有包含进CoreData.于是就在已建好的项目中加入CoreData.由于第一 ...

  4. Python Selenium 之常用API

    Selenium WebDriver下提供许多用来与浏览器.元素.鼠标.键盘.弹框.下拉菜单和列表的交互和设置方法.这些是计算机模拟人工进行自动化测试所必要依赖的方法.下面将用列表的方式总结出常用的A ...

  5. Python 数据分析—第八章 画图

    一.matplotlib的用法 折线图+一些常用的设置 #显示中文 import matplotlib as mpl mpl.rcParams['font.sans-serif'] = [u'SimH ...

  6. MacOS VMware Function安装win的ghost系统(亲测成功!!!)

    1.下载WEPE工具生成可启动ISO 下载WEPE工具 WEPE工具下载地址 下载完成之后,双击打开文件(无需安装,可直接制作可启动ISO) 点击立即生成ISO之后 2.将生成的可启动ISO文件和wi ...

  7. Linq生成操作之DefautIfEmpty,Empty,Range,Repeat源码分析

    Linq生成操作之DefautIfEmpty,Empty,Range,Repeat源码分析 Linq的四种生成运算 DefautIfEmpty,Empty,Range,Repeat 也就是给我们初始化 ...

  8. MVC4 路由解析 同名Controller的解决方案

    通常我们在MVC中通过Area建立子站的时候会有 controller名称重复的情况,这是后如何区分路由优先级, 我们知道 在Route对象中存在RouteValueDictionary 类型的Dat ...

  9. C# Redis辅助类封装与简单聊天室的实现思路说明

    虽然redis api的功能比较齐全,但个人觉得为了更好的方便学习和使用,还是很有必有做一个类似DBHelper的帮助类 辅助类主要功能(代码会在最后放出来) 1. 事件监听: 重新配置广播时(主从同 ...

  10. C# redis 的简单应用

    region 准备参数 var connStr = "localhost:6379,password="; var db = 2; SiteRedisHelper redisHel ...