牛客假日团队赛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 ...
随机推荐
- RGB-D显著性突出物体(学习)
论文阅读:Adaptive Fusion for RGB-D Salient Object Detection 这篇代码的创新点在于使用了SW层,使用SW_logits * img_logits + ...
- T78748 【lcez模拟赛】机场Ⅰ
T78748 [lcez模拟赛]机场Ⅰ 其实这就是最小生成树的题辣 注意输入毒瘤 输入的话要避免记录中间这个‘ , ’ 如下操作可以解决 特别注意%d之间的‘ , ’ 边的权值要现算 存点的话存横纵坐 ...
- Error-ASP.NET:在从服务器接收结果时发生传输级错误。 (provider: Session Provider, error: 19 - 物理连接不可用)
ylbtech-Error-ASP.NET:在从服务器接收结果时发生传输级错误. (provider: Session Provider, error: 19 - 物理连接不可用) 1.返回顶部 1 ...
- Android地图开发获取sHA1值方法
public static String sHA1(Context context) { try { PackageInfo info = context.getPackageManager().ge ...
- nodejs之流数据读取与写入
1.(fs.createReadStream)当文件比较大时,建议使用文件流读取,不会出现卡顿现象,demo如下. const fs = require('fs'); //流的方式读取文件 var r ...
- Selenium 2自动化测试实战11(键盘事件)
一.键盘事件 1.Keys()类提供了键盘上几乎所有按键的方法,如下实例: #coding:utf-8 from selenium.webdriver.common.keys import Keys ...
- 在DBGrid中,按ctrl+Delete不让删除,怎么实现
DBGrid的Options中的dgConfirmDelete改为:False;在DBGrid所连接的DataSet的BeforeDelete事件中写:Abort; ^_^
- UniEAP V4 开发实践说明文档
一.开发环境搭建 1. 前期准备 Java jdk1.6 ,Oralce数据库,plsql客户端,tomcat6.0,开发样例数据库脚本,unieap脚本,unieap工程,unieap worksh ...
- 自己实现一个list比较器 实现Comparator()接口
一:一个实体类 成员变量有名字,年龄,分数 )))))); List<User> list = new ArrayList<>(); list.add(user1); list ...
- Django ModelChoiceField:过滤查询集并将默认值设置为对象
我有一个Django Form类定义喜欢这个在Models: class AccountDetailsForm(forms.Form): ... adminuser = forms.ModelChoi ...