Catch That Cow (简单BFS+剪枝)
Problem 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
Output
Sample Input
5 17
Sample Output
4
Hint
#include <algorithm>
#include <bitset>
//#include <bits/extc++.h>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue> using namespace std;
//using namespace __gnu_pbds #define ll long long
#define maxn 105 int s, e, step[100005];
bool vis[100005]; void bfs()
{
queue<int> q;
q.push(s);
vis[s] = 0;
step[s] = 0;
while (!q.empty())
{
int now = q.front();
q.pop();
int next = now + 1;
if (next <= e && vis[next])
{
vis[next] = false;
step[next] = step[now] + 1;
q.push(next);
}
if (next == e)
{
return;
}
next = now - 1;
if (next >= 0 && vis[next])
{
vis[next] = false;
step[next] = step[now] + 1;
q.push(next);
}
if (next == e)
{
return;
}
next = now << 1;
if ((next <= e || next - e + 1 < e - now) && next <= 100000 && vis[next])
{
vis[next] = false;
step[next] = step[now] + 1;
q.push(next);
}
if (next == e)
{
return;
}
}
} int main()
{
while (~scanf("%d%d", &s, &e))
{
memset(vis, true, sizeof(vis));
if (s >= e)
{
printf("%d\n", s - e); //剪枝
}
else
{
bfs();
printf("%d\n", step[e]);
}
}
return 0;
}
Catch That Cow (简单BFS+剪枝)的更多相关文章
- POJ 3278 Catch That Cow(BFS 剪枝)
题目链接:http://poj.org/problem?id=3278 这几次都是每天的第一道题都挺顺利,然后第二道题一卡一天. = =,今天的这道题7点40就出来了,不知道第二道题在下午7点能不能出 ...
- POJ 3278 Catch That Cow(简单BFS)
题目链接:http://poj.org/problem?id=3278 题目大意:给你两个数字n,k.可以对n执行操作(n+1,n-1,n*2),问最少需要几次操作使n变成k. 解题思路:bfs,每次 ...
- poj3278 Catch That Cow(简单的一维bfs)
http://poj.org/problem?id=3278 ...
- hdu 2717:Catch That Cow(bfs广搜,经典题,一维数组搜索)
Catch That Cow Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- poj 3278(hdu 2717) Catch That Cow(bfs)
Catch That Cow Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- POJ 3278 Catch That Cow(bfs)
传送门 Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 80273 Accepted: 25 ...
- HDU 2717 Catch That Cow (bfs)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2717 Catch That Cow Time Limit: 5000/2000 MS (Java/Ot ...
- HDU 2717 Catch That Cow(BFS)
Catch That Cow Farmer John has been informed of the location of a fugitive cow and wants to catch he ...
- hdoj 2717 Catch That Cow【bfs】
Catch That Cow Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- Catch That Cow(BFS)
Catch That Cow Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
随机推荐
- 【36.11%】【codeforces 725C】Hidden Word
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 三、解析class文件
一.class文件 https://blog.csdn.net/tyyj90/article/details/78472986 https://blog.csdn.net/sinat_38259539 ...
- 搭建高可用的flink JobManager HA
JobManager协调每个flink应用的部署,它负责执行定时任务和资源管理. 每一个Flink集群都有一个jobManager, 如果jobManager出现问题之后,将不能提交新的任务和运行新任 ...
- JavaScript之DOM操作,事件操作,节点操作
1,DOM操作 1.1 概念 所谓DOM,全称Document Object Model 文档对象模型,DOM是W3C(World Wide Web Consortuum )标准,同时也定义了访问诸 ...
- centos6.x安装yum及python依赖包
步骤1:清空pythonrpm -qa|grep python|xargs rpm -e --allmatches --nodeps whereis python|xargs rm -fr 步骤2:清 ...
- 20191024-3 互评Alpha阶段作品
此作业要求参见https://edu.cnblogs.com/campus/nenu/2019fall/homework/9860 本组对构建之法组评价的博客链接:https://www.cnblog ...
- 敏捷开发:代码Review
热情高涨 代码走查作为一种流程形式,起初大家的参与热情非常高涨. 因为,自己可以学习到别人一些巧妙的思想,自己的代码和习惯都暴漏出来. 这个过程中不断地吸收和改正. 但是...... 我们一开始组织的 ...
- Linux入门系列1--环境准备及Linux安装
"工欲善其事.必先利其器",本文作为"Linux零基础入门系列"开篇,将完整演示整个开发环境的安装和配置过程,为后续的开发和实验做好基础准备.如果您已安装好环境 ...
- 学海无涯-php
- MySQL保存 emoji 表情(微信昵称表情)
问题分析 在微信开发过程中,总是会遇到带有emoji表情昵称的微信用户无法自动登录的问题. 后台代码抛出类似下面的异常信息. java.sql.SQLException: Incorrect stri ...