牛客假日团队赛5 L Catch That Cow HDU 2717 (BFS)
链接:https://ac.nowcoder.com/acm/contest/984/L
来源:牛客网
Catch That Cow
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 32768K,其他语言65536K
64bit IO Format: %lld
题目描述
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?
输入描述:
Line 1: Two space-separated integers: N and K
输出描述:
Line 1: The least amount of time, in minutes, it takes for Farmer John to catch the fugitive cow.
示例1
输入
复制
5 17
输出
复制
4
说明
Farmer John starts at point 5 and the fugitive cow is at point 17.
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.
题意:
给你一个位置n,一个位置k,都在水平的坐标轴上,你每一次可以向左或者向右走一步,或者从当先的x移动到2*x 位置,问你从n到k最小需要多少步?
思路:
直接BFS搜,用一个数组vis 维护 每一个位置是否被访问,由于BFS的性质,一个位置如果已经被访问过了,就不需要再访问了,因为当前的步数一定大于等于访问时候的步数。
细节见代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define rt return
#define dll(x) scanf("%I64d",&x)
#define xll(x) printf("%I64d\n",x)
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) {ll ans = 1; while (b) {if (b % 2)ans = ans * a % MOD; a = a * a % MOD; b /= 2;} return ans;}
inline void getInt(int* p);
const int maxn = 2000010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
bool vis[maxn];
queue<pii> q;
int main()
{
//freopen("D:\\code\\text\\input.txt","r",stdin);
//freopen("D:\\code\\text\\output.txt","w",stdout);
int n,k;
cin>>n>>k;
vis[n]=1;
q.push(mp(n,0));
pii t;
while(!q.empty())
{
t=q.front();
q.pop();
if(t.fi==k)
{
cout<<t.se<<endl;
break;
}else
{
if(t.fi+1<maxn&&!vis[t.fi+1])
{
vis[t.fi+1]=1;
q.push(mp(t.fi+1,t.se+1));
}
if(t.fi-1>=0&&!vis[t.fi-1])
{
vis[t.fi-1]=1;
q.push(mp(t.fi-1,t.se+1));
}
if(t.fi*2<maxn&&!vis[t.fi*2])
{
vis[t.fi*2]=1;
q.push(mp(t.fi*2,t.se+1));
}
}
}
return 0;
}
inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '0');
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 - ch + '0';
}
}
else {
*p = ch - '0';
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 + ch - '0';
}
}
}
牛客假日团队赛5 L Catch That Cow HDU 2717 (BFS)的更多相关文章
- 牛客假日团队赛10 L 乘积最大 (dp,大数)
链接:https://ac.nowcoder.com/acm/contest/1072/L?&headNav=acm&headNav=acm 来源:牛客网 乘积最大 时间限制:C/C+ ...
- 牛客假日团队赛5 F 随机数 BZOJ 1662: [Usaco2006 Nov]Round Numbers 圆环数 (dfs记忆化搜索的数位DP)
链接:https://ac.nowcoder.com/acm/contest/984/F 来源:牛客网 随机数 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言6 ...
- P5200 [USACO19JAN]Sleepy Cow Sorting 牛客假日团队赛6 D 迷路的牛 (贪心)
链接:https://ac.nowcoder.com/acm/contest/993/E 来源:牛客网 对牛排序 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言 ...
- 牛客假日团队赛6 D 迷路的牛 (思维)
链接:https://ac.nowcoder.com/acm/contest/993/D 来源:牛客网 迷路的牛 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言 ...
- 牛客假日团队赛5J 护城河 bzoj 1670: [Usaco2006 Oct]Building the Moat护城河的挖掘 (凸包的周长)
链接:https://ac.nowcoder.com/acm/contest/984/J 来源:牛客网 护城河 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言6 ...
- 牛客假日团队赛5 K 金币馅饼 (DP 基础题)
链接:https://ac.nowcoder.com/acm/contest/984/K 来源:牛客网 金币馅饼 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言 ...
- 洛谷 P2866 [USACO06NOV]糟糕的一天Bad Hair Day 牛客假日团队赛5 A (单调栈)
链接:https://ac.nowcoder.com/acm/contest/984/A 来源:牛客网 题目描述 Some of Farmer John's N cows (1 ≤ N ≤ 80,00 ...
- 「BZOJ1669」D 饥饿的牛 [Usaco2006 Oct] Hungry Cows 牛客假日团队赛5 (LIS,离散化树状数组)
链接:https://ac.nowcoder.com/acm/contest/984/D 来源:牛客网 饥饿的牛 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言 ...
- 牛客假日团队赛2 C 修围栏 ( 哈夫曼树,贪心)
链接:https://ac.nowcoder.com/acm/contest/924/C 来源:牛客网 修围栏 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言6 ...
随机推荐
- Selenium 2自动化测试实战11(键盘事件)
一.键盘事件 1.Keys()类提供了键盘上几乎所有按键的方法,如下实例: #coding:utf-8 from selenium.webdriver.common.keys import Keys ...
- deepin的15.11中安装nvidia最新435版本驱动
换了一个电脑,跟随潮流,CPU是不带集显的,操作系统从原来的硬盘一通搬过来的 其中Deepin Linux更新到15.11. 显卡是2060的,在Deepin中目前只集成了390的nvidia驱动,无 ...
- Struts+Hibernate+Spring面试题合集及答案
Struts+Hibernate+Spring面试题合集及答案 Struts+Hibernate+Spring面试题合集 1 1. Hibernate部分 2 1.1. Hibernate工作原理 2 ...
- java: (正则表达式,XML文档,DOM和DOM4J解析方法)
常见的XML解析技术: 1.DOM(基于XML树结构,比较耗资源,适用于多次访问XML): 2.SAX(基于事件,消耗资源小,适用于数量较大的XML): 3.JDOM(比DOM更快,JDOM仅使用具体 ...
- MariaDB select
1.环境部署: syntax语法错误 查询基本使用(条件,排序,聚合函数,分组,分页) --创建学生表 create table students ( id int unsigned not null ...
- 关于liunx 机器脱机环境(netcore)Nuget包迁移的问题
首先nuget脱机环境是没办法加载第三方nuget包的,我这里的做法是使用nuget缓存文件(正确的做法还是推荐使用自己搭建的nuget服务器然后正常发布,这里只是做应急之需) 我们都知道项目的dot ...
- go的变量定义
package main //理解包的概念 import "fmt" var ( aa = 1 bb = "kkk" ss = true) func varia ...
- python 三元运算、列表推倒式、字典推倒式、生成器生成式
1.三元运算 name=input('姓名>>: ') res='SB' if name == 'alex' else 'NB' print(res) 2.列表推倒式 #1.示例 egg_ ...
- vue--过滤器(私有,全局)
过滤器 概念:Vue.js 允许你自定义过滤器,可被用作一些常见的文本格式化.过滤器可以用在两个地方:mustache 插值和 v-bind 表达式.过滤器应该被添加在 JavaScript 表达式的 ...
- 第十四周课程总结&&实验总结
课程总结: 1.Java实现跨平台操作的工具:JDBC. 它的意思是java数据库连接,可以方便的实现多种关系型数据库的统一操作,它由一组用java语句编写的类和接口组成. 2.JDBC驱动分类: J ...