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 ...
随机推荐
- 将Redis设置为后台启动
Linux 在执行redis-server /etc/redis.conf 时默认开启的是一个前台的进程,也就是说启动的redis 就做不了其他的操作了,只有关闭redis 才能做其他的操作.非常的 ...
- response.Close、response.End、response.Flush区别(下载文件)
今天在做文件下载功能用到的是response的方法,首先我们要了解这些方法的作用. 1.response.write():将信息写入http响应输出流. 2.response.Flush:向客户端发送 ...
- Composer更新与清除缓存命令
一.更新命令 composer self-update --preview 二.清除缓存命令 composer clearcache 操作如下图所示:
- PO设计模式
BasePage类: 在PO模式中抽象封装成一个BasePage类,该基类应该拥有一个只实现webdriver实例的属性. 基础页面类中包含公用方法:点击.输入.获取元素等 Page: 每个页面封装为 ...
- es更新说明(dsl)
一.旧版elasticsearch-dsl 很多同学在python搜索引擎视频中关于看到的第十章elasticsearch使用中使用python创建mapping老师使用的以下代码,这些代码对于ela ...
- 简明conda使用指南
目录 区分conda, anaconda, miniconda conda版本 虚拟环境 分享环境 查看某个环境的位置 列出软件包 安装软件包 删除软件包 查找软件包 conda配置 conda实践: ...
- Windows 2016 & Windows 10 中IIS安装和配置PHP的步骤
Windows 2016 和 Windows 10 内核是相同的,我们首先需要安装 Internet Information Services (IIS),当然 Win2016 跟 Win10 安装 ...
- Prometheus(三):Prometheus监控交换机(snmp)
默认已安装Prometheus服务,服务地址:192.168.56.200 一.获取交换机snmp信息 snmp服务IP(交换机IP):172.20.2.83 snmp community:dfete ...
- 主进程和服务进程通信调用Acrobat.AcroPDDoc时出现的问题
场景以及问题 主进程发送命令,服务进程接受到,然后执行转换pdf,调用Acrobat pdfDoc = (Acrobat.CAcroPDDoc)Microsoft.VisualBasic.Intera ...
- Oracle EBS 11i x86-64 的预安装
1.EBS11i的软件组成,28个zip包 Oracle ApplicationsOracle Application with NLSOracle Database technology stack ...