BFS --- 模板题
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 36079 | Accepted: 11123 |
Description
Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and the cow is at a point K (0 ≤ K ≤ 100,000) on the same number line. Farmer John has two modes of transportation: walking and teleporting.
* Walking: FJ can move from any point X to the points X - 1 or X + 1 in a single minute
* Teleporting: FJ can move from any point X to the point 2 × X in a single minute.
If the cow, unaware of its pursuit, does not move at all, how long does it take for Farmer John to retrieve it?
Input
N
and
K
Output
Sample Input
5 17
Sample Output
4
Hint
The fastest way for Farmer John to reach the fugitive cow is to move along the following path: 5-10-9-18-17, which takes 4 minutes.
【题目来源】
#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
int N,K; struct now
{
int x;
int step;
};
bool vis[]; int BFS(int x)
{
int tx;
now ans,tp;
queue<now> que;
ans.x=x,ans.step=;
vis[x]=true;
que.push(ans);
while(!que.empty())
{
tp=que.front();
que.pop();
tx=tp.x+; //no.1
if(tx<||tx>);
else
{
if(!vis[tx])
{
vis[tx]=true;
ans.x=tx,ans.step=tp.step+,que.push(ans);
if(ans.x==K) return ans.step;
}
}
tx=tp.x-; //no.2
if(tx<||tx>);
else
{
if(!vis[tx])
{
vis[tx]=true;
ans.x=tx,ans.step=tp.step+,que.push(ans);
if(ans.x==K) return ans.step;
}
}
tx=tp.x*; //no.3
if(tx<||tx>);
else
{
if(!vis[tx])
{
vis[tx]=true;
ans.x=tx,ans.step=tp.step+,que.push(ans);
if(ans.x==K) return ans.step;
}
}
}
} int main()
{
while(cin>>N>>K)
{
if(N==K)
{
cout<<""<<endl;
continue;
}
memset(vis,false,sizeof(vis));
cout<<BFS(N)<<endl;
}
return ;
}
当然还可以剪枝一下,比如说:如果人的坐标大于牛的坐标,这时就只需要做-1这一步就可以了。
BFS --- 模板题的更多相关文章
- POJ-2251 Dungeon Master (BFS模板题)
You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of un ...
- POJ:Dungeon Master(三维bfs模板题)
Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16748 Accepted: 6522 D ...
- hdu1242 又又又是逃离迷宫(bfs模板题)
题目链接:http://icpc.njust.edu.cn/Problem/Hdu/1242/ 这次的迷宫是有守卫的,杀死一个守卫需要花费1个单位的时间,所以以走的步数为深度,在每一层进行搜索,由于走 ...
- Saving Princess claire_(hdu 4308 bfs模板题)
http://acm.hdu.edu.cn/showproblem.php?pid=4308 Saving Princess claire_ Time Limit: 2000/1000 MS (Jav ...
- Knight Moves(hdu1372 bfs模板题)
http://acm.hdu.edu.cn/showproblem.php?pid=1372 Knight Moves Time Limit: 2000/1000 MS (Java/Others) ...
- 用一道模板题理解多源广度优先搜索(bfs)
题目: //多元广度优先搜索(bfs)模板题详细注释题解(c++)class Solution { int cnt; //新鲜橘子个数 int dis[10][10]; //距离 int dir_x[ ...
- HDU-3549 最大流模板题
1.HDU-3549 Flow Problem 2.链接:http://acm.hdu.edu.cn/showproblem.php?pid=3549 3.总结:模板题,参考了 http://ww ...
- HDU 4280:Island Transport(ISAP模板题)
http://acm.hdu.edu.cn/showproblem.php?pid=4280 题意:在最西边的点走到最东边的点最大容量. 思路:ISAP模板题,Dinic过不了. #include & ...
- AC自动机 - 多模式串匹配问题的基本运用 + 模板题 --- HDU 2222
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
随机推荐
- bugku——普通的二维码(进制转换)
题目地址:http://ctf.bugku.com/files/5e480ecb178711e82bc847a208e15b32/misc80.zip 就一张二维码图片,用一些在线工具识别是乱码,用Q ...
- 实验吧——忘记密码了(vim备份文件,临时文件(交换文件))
题目地址:http://ctf5.shiyanbar.com/10/upload/step1.php 前些天突然发现个游戏,于是浪费了好多时间,终于还是忍住了,现在专心学习,从今天开始正式写些学习笔记 ...
- (转)牛牛牌型判定(五小牛 > 五花牛 > 炸弹 > 银牛 > 牛牛 > 有牛>没牛)
牌型大小: 五小牛 > 五花牛 > 炸弹 > 银牛 > 牛牛 > 有牛(牛987654321) > 没牛,K > Q > J ……2 > A, 黑 ...
- MarkDown中如何加入上标和下标
上标 使用<sup></sup>标签包裹的部分就是上标,例如:A<sup>T</sup> 显示效果就是 AT . 下标 使用<sub>< ...
- qr.h
创建二维码 QRCodeCreate vc++
- 如何将MultipartFile转换成based4
public String test(MultipartFile file) throws Exception{ BASE64Encoder base64Encoder =new BASE64Enco ...
- actuator beans不展示
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring- ...
- Spring和SpringMVC总结篇
作者:肥宅兜链接:https://www.cnblogs.com/doudouxiaoye/p/5693399.html 1.为什么使用Spring ? 方便解耦,简化开发;通过Spring提供的Io ...
- LOJ P10022 埃及分数 题解
每日一题 day62 打卡 Analysis 这道题一看感觉很像搜索,但是每次枚举x∈(1,10000000)作为分母显然太蠢了. 所以我们要想办法优化代码. 优化一:迭代加深 优化二: 我们确定了搜 ...
- Visual Studio源服务器缓存
您是否想过Visual Studio 2008/2010在哪里存储从源服务器下载的源文件?默认情况下,它们会放在Local Settings\Applications Data\SourceServe ...