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. ...
随机推荐
- easyui 获取当前页签选中的名称
parent.parent.$("#tabs").tabs('getSelected').panel('options').title == "收藏夹管理"
- 关于设置了setMaxAge(0)而浏览器未成功删除Cookie的注意事项
最近做了个系统,其中涉及到对Cookie的操作.当用户登录时,设置一些数据到Cookie中,用户登出系统的时候删除写入浏览器中的对应Cookie.问题就出在登出系统时,在firebug中看到需要删除的 ...
- JavaScript 与JQuery 常用方法比较
http://drupalchina.cn/content/javascript-yu-jquery-chang-yong-fang-fa-bi-jiao 1.加载DOM区别 JavaScript: ...
- CMDB小练习
为什么要用CMDB? 因为公司之前统计资产信息用的是excel表格,随着业务的增加和信息的变更,这个表格变得越来越乱,所以我们就想着编写出一套自动管理资产信息的系统,实现自动管理资产信息 三种方案? ...
- C#和C++语言使用方面的区别
本人觉得C#是世界上最优美的语言,也可以说是一门傻瓜语言,入门成本低,上手快得到许多人的青睐,但是C#并没有在行业内得到大家的首肯,反倒是C/C++人才比较紧俏:本人在学习过程中将C#和C++语言使用 ...
- css3系列之animation
在上次博文中已经讲了transition,其实animation与transition功能相同,都是通过改变元素 的属性来实现动画效果的.但是它们也有区别:transition是只能通过改变指定属性的 ...
- 设计模式23:Visitor 访问者模式(行为型模式)
Visitor 访问者模式(行为型模式) 动机(Motivation)在软件构造过程中,由于需求的改变,某些类层次结构中常常需要增加新的行为(方法),如果直接在基类中做这样的修改,将会给子类带来繁重的 ...
- 风尘浪子 只要肯努力,梦想总有一天会实现 WF工作流与Web服务的相互调用 —— 通过Web服务调用Workflow工作流(开发持久化工作流) _转
如果你曾经负责开发企业ERP系统或者OA系统,工作流对你来说一定并不陌生.工作流(Workflow)是对工作流程及其各操作步骤之间业务规则 的抽象.概括.描述.工作流要解决的主要问题是:为实现某个业务 ...
- 理解Javascript中的事件绑定与事件委托
最近在深入实践js中,遇到了一些问题,比如我需要为动态创建的DOM元素绑定事件,那么普通的事件绑定就不行了,于是通过上网查资料了解到事件委托,因此想总结一下js中的事件绑定与事件委托. 事件绑定 ...
- Delphi 中调用JS文件中的方法
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...