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

Line 1: Two space-separated integers: N and K

Output

Line 1: The least amount of time, in minutes, it takes for Farmer John to catch the fugitive cow.

Sample Input

5 17

Sample Output

4

HintThe 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.

Source

USACO 2007 Open Silver


思路

考验对状态的理解,每次走下一步有3种状态,bfs即可

代码

#include<bits/stdc++.h>
using namespace std;
const int d[3] = {1,-1,0};
struct node
{
int pos;
int step;
};
int n,k;
bool vis[200010]; bool judge(int x)
{
if(vis[x] || x<0 || x>100000)
return false;
return true;
}
int bfs(node st)
{
queue<node> q;
q.push(st);
node next,now;
memset(vis,false,sizeof(vis));
vis[st.pos] = true;
while(!q.empty())
{
now = q.front();
q.pop();
if(now.pos == k) return now.step;
for(int i=0;i<3;i++)
{
if(i==0 || i==1)
next.pos = now.pos + d[i];
else
next.pos = now.pos * 2;
next.step = now.step + 1;
if(judge(next.pos))
{
q.push(next);
vis[next.pos] = true;
}
}
}
} int main()
{
while(cin>>n>>k)
{
node t;
t.pos = n; t.step = 0;
int ans = bfs(t);
cout << ans << endl;
}
return 0;
}

Hdoj 2717.Catch That Cow 题解的更多相关文章

  1. hdoj 2717 Catch That Cow【bfs】

    Catch That Cow Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  2. hdoj 2717 Catch That Cow

    Problem Description Farmer John has been informed of the location of a fugitive cow and wants to cat ...

  3. 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 ...

  4. HDU 2717 Catch That Cow --- BFS

    HDU 2717 题目大意:在x坐标上,农夫在n,牛在k.农夫每次可以移动到n-1, n+1, n*2的点.求最少到达k的步数. 思路:从起点开始,分别按x-1,x+1,2*x三个方向进行BFS,最先 ...

  5. HDU 2717 Catch That Cow(常规bfs)

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2717 Catch That Cow Time Limit: 5000/2000 MS (Java/Oth ...

  6. 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 ...

  7. HUD 2717 Catch That Cow

    Catch That Cow Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...

  8. hdu 2717:Catch That Cow(bfs广搜,经典题,一维数组搜索)

    Catch That Cow Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  9. hdu 2717 Catch That Cow(广搜bfs)

    题目链接:http://i.cnblogs.com/EditPosts.aspx?opt=1 Catch That Cow Time Limit: 5000/2000 MS (Java/Others) ...

随机推荐

  1. python知识点及面试面试大集合

    题目来源:武sir--一个很有意思的人,点击这儿跳转 一.基础篇 为什么学习Python? 通过什么途径学习的Python? Python和Java.PHP.C.C#.C++等其他语言的对比? 简述解 ...

  2. html中怎么设置性别默认选择

    <html><body> <form action="/example/html/form_action.asp" method="get& ...

  3. Django组件之认证系统

      Django自带的用户认证 我们在开发一个网站的时候,无可避免的需要设计实现网站的用户系统.此时我们需要实现包括用户注册.用户登录.用户认证.注销.修改密码等功能,这还真是个麻烦的事情呢. Dja ...

  4. Individual Project

    这次我自己完成了一个小小的项目,课可以把这篇随笔当做一次实验报告,主要的内容是用JUnit进行单元测试.由于我的技术太弱了,就在博客园里“求师”,按照大神的方法慢慢把这些东西写了下啦来. 不知道怎么搞 ...

  5. 常用ASCII码对照表

        

  6. CMake--常用指令

    1 . ADD_DEFINITIONS 向 C/C++ 编译器添加 -D 定义,比如 在CMakeList.txt文件中添加: ADD_DEFINITIONS(-DENABLE_DEBUG -DABC ...

  7. Notepad++ 安装 NppFTP 插件

    How to install a plugin The plugin (in the DLL form) should be placed in the \plugins subfolder of t ...

  8. mac下php开发环境的搭建

    1.phpstorm 在官网:https://www.jetbrains.com/phpstorm/,下载最新版:phpstorm-2016.2.1 在http://15.idea.lanyus.co ...

  9. element-ui 源码解析 一

    Button组件 button.vue <template> <button class="el-button" @click="handleClick ...

  10. mktemp -t -d用法

    mktemp命令用于建立暂存文件或者文件夹,帮助文档如下: Usage: mktemp [OPTION]... [TEMPLATE] Create a temporary file or direct ...