hdu1495 bfs搜索、模拟
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搜索、模拟的更多相关文章
- hdu 1240:Asteroids!(三维BFS搜索)
Asteroids! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- hiho_1139_二分+bfs搜索
题目 给定N个点和M条边,从点1出发,到达点T.寻找路径上边的个数小于等于K的路径,求出所有满足条件的路径中最长边长度的最小值. 题目链接:二分 最小化最大值,考虑采用二分搜索.对所有的边长进 ...
- 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 ...
- BFS搜索
参考博客:[算法入门]广度/宽度优先搜索(BFS) 适用问题:一个解/最优解 重点:我们怎么运用队列?怎么记录路径? 假设我们要找寻一条从V0到V6的最短路径.(明显看出这条最短路径就是V0-> ...
- 【NOIP 2011】Mayan游戏(搜索+模拟)
描述 Mayan puzzle是最近流行起来的一个游戏.游戏界面是一个7行5列的棋盘,上面堆放着一些方块,方块不能悬空堆放,即方块必须放在最下面一行,或者放在其他方块之上.**游戏通关是指在规定的步数 ...
- BFS、模拟:UVa1589/POJ4001/hdu4121-Xiangqi
Xiangqi Xiangqi is one of the most popular two-player board games in China. The game represents a ba ...
- Horse Pro(带负坐标的bfs搜索)
Horse Pro bfs搜索,但图中存在负值坐标,两种方法解决. 用数组标记,将原点设为300,300 用map标记 http://oj.jxust.edu.cn/contest/Problem?i ...
- 天梯赛练习 L3-008 喊山 (30分) bfs搜索
题目分析: 本题是一题比较简单的bfs搜索题,首先由于数据给的比较多不能直接开二维数组存放,而是用了vector的动态的二维数组的形式存放,对于每个出发点,我们bfs向四周搜索,标记搜索过的点,遇到搜 ...
- ICE CAVE(BFS搜索(模拟))
Description You play a computer game. Your character stands on some level of a multilevel ice cave. ...
随机推荐
- 基于AspNet Core2.0(测试版) 开发框架,包含简单的个人博客Demo
大家好,最近离职了,利用闲暇时间就捣鼓了一个基于AspNet Core开发框架,分享出来希望能给AspNet Core学者带来一些帮助,同时也能跟大家一起学习.当然了,由于我的个人技术及经验的有限,框 ...
- mybatis 传参是 list<string> 的注意事项
<!--付款 批量 修改账单状态--><update id="editbillpayALL" parameterType="java.util.List ...
- Inno Setup创建快捷方式跟快速运行栏快捷方式
[Tasks] Name: "desktopicon"; Description: "{cm:CreateDesktopIcon}"; GroupDescrip ...
- Appium 简介及工作原理
申请:本文介绍主要是针对Android. 1.什么是Appium: Appium是一个开源.跨平台的测试框架,可以用来测试原生及混合的移动端应用.Appium支持IOS.Android及Firefox ...
- 减少C盘空间占用的技巧
1.搜索C盘中大小大于某个值的文件:C:\Windows\SoftwareDistribution这个文件夹下很多大文件 2.搜索*.log文件 3.C:\Users\Guangshan\AppDat ...
- python 批量创建文件
# coding:utf8 import os path = "D:/Python_mkfile" os.chdir(path)#切换到该目录 ysyl = u"验收文件 ...
- ML.NET---.NET下的机器学习引擎(简介)
ML.NET 是一个跨平台的开源机器学习框架,它可以使 .NET 开发人员更容易的开展机器学习工作. ML.NET 允许 .NET 开发人员开发自己的模型,即使没有机器学习的开发经验,也可以很容易的将 ...
- .CHM文件使用问题
1.查看时内容为空---解除锁定 解决方案:选中xx.CHM,右键点击属性,常规选项卡中点击“解除锁定”.OK了 2.内容显示乱码---修改浏览器编码 IE中的查看 > 编码 > 自动选择 ...
- docker运行nginx
docker run --name nginx(容器名) -p 80:80 -v 项目文件路径:/usr/share/nginx/html -v 配置文件路径:/etc/nginx/nginx.con ...
- 阿里java开发规范学习(附P3C IDEA插件 帮助规范的养成)
浅析 阿里巴巴 Java 开发规约 (未完成) 更加优秀的页面展现请到浅析 阿里巴巴 Java 开发规约 contents 为什么要学 编程规约 P3C IDEA 插件 why-use 我们知道,一般 ...