Catch That Cow

Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 8616    Accepted Submission(s): 2714

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

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.

 

经典BFS,常规思路。

注意:

题目所给5 17和17 5答案是不一样的。我曾天真地认为需要进行一步swap,实际上是不需要的。

遍历到某点应判断是否越界,否则也会ACCESS_VIOLATION。

数组也要开大一点,否则也会ACCESS_VIOLATION。

(PS:我忘记修改判断是否越界时界限的大小。)

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
int vis[];
int stp[];
int n, k; void swap(int& a, int& b)
{
a = a ^ b;
b = a ^ b;
a = a ^ b;
} /*
int move(int sgn, int cur)
{
if(sgn == 1)
{
return cur + 1;
}
if(sgn == -1)
{
return cur - 1;
}
else
{
return cur * 2;
}
}
*/ void BFS(int ini)
{
int cur, now = ; //init
queue<int> q;
vis[ini] = ;
stp[ini] = ;
q.push(ini);
while(!q.empty())
{
cur = q.front();
q.pop();
for(int i = ; i < ; i++)
{
if(i == )
{
now = cur + ;
}
else if(i == )
{
now = cur - ;
}
else
{
now = cur * ;
} if(!vis[now] && now >= && now <= )
{
vis[now] = ;
q.push(now);
stp[now] = stp[cur] + ;
}
if(now == k)
{
printf("%d\n", stp[now]);
return ;
}
}
}
}
int main()
{
while(scanf("%d %d", &n, &k) != EOF && n+k)
{
memset(vis, , sizeof(vis));
memset(stp, , sizeof(stp));
if (n == k)
{
printf("0\n");
}
else
{
BFS(n);
}
}
return ;
}

[HDOJ2717]Catch That Cow的更多相关文章

  1. POJ 3278 Catch That Cow(bfs)

    传送门 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 80273   Accepted: 25 ...

  2. poj3278 Catch That Cow

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 73973   Accepted: 23308 ...

  3. catch that cow (bfs 搜索的实际应用,和图的邻接表的bfs遍历基本上一样)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 38263   Accepted: 11891 ...

  4. poj 3278:Catch That Cow(简单一维广搜)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 45648   Accepted: 14310 ...

  5. 2016HUAS暑假集训训练题 B - Catch That Cow

    B - Catch That Cow Description Farmer John has been informed of the location of a fugitive cow and w ...

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

  7. BFS POJ 3278 Catch That Cow

    题目传送门 /* BFS简单题:考虑x-1,x+1,x*2三种情况,bfs队列练练手 */ #include <cstdio> #include <iostream> #inc ...

  8. Catch That Cow 分类: POJ 2015-06-29 19:06 10人阅读 评论(0) 收藏

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 58072   Accepted: 18061 ...

  9. 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,最先 ...

随机推荐

  1. C# 时间现实问题(12小时制与24小时制)

    最近在修改项目中遇到时间问题,12小时制与24小时制的问题,想再次跟各位同仁提个醒. yyyy-MM-dd HH:mm:ss------大写的HH为24小时制 yyyy-MM-dd hh:mm:ss- ...

  2. 什么是BI(Business Intelligence)【转】

    谈谈对BI的理解,从BI的定义.基本技术.专业名词.实例应用及扩展等方面进行重新描述,巩固对BI的理解. 一.BI的定义 BI是Business Intelligence的英文缩写,中文解释为商务智能 ...

  3. 一篇文章一张思维导图看懂Android学习最佳路线

    一篇文章一张思维导图看懂Android学习最佳路线 先上一张android开发知识点学习路线图思维导图 Android学习路线从4个阶段来对Android的学习过程做一个全面的分析:Android初级 ...

  4. loading等待载入正在加载的动画GIF图片圆形图标

    http://www.wtoutiao.com/p/GdfbdM.html

  5. LoadRunner并发用户和集合点的深入讨论

    看到51上三个高手Zee, 大漠飞鹰,xingcyx的一场非常精彩的关于并发用户数和集合点的讨论,很有意义.如果对这两个概念不清楚的朋友,一定要仔细领悟了. 故事开始于xingcyx的一番话: 声明: ...

  6. Linux 中open系统调用实现原理【转】

    转自:http://blog.chinaunix.net/uid-25968088-id-3426026.html 目录 OPEN系统调用过程 Open在内核里面的入口函数时sys_open Sys_ ...

  7. sql server 2012 自定义聚合函数(MAX_O3_8HOUR_ND) 计算最大的臭氧8小时滑动平均值

    采用c#开发dll,并添加到sql server 中. 具体代码,可以用visual studio的向导生成模板. using System; using System.Collections; us ...

  8. Java中常见数据结构:list与map

    1:集合 Collection(单列集合) List(有序,可重复) ArrayList 底层数据结构是数组,查询快,增删慢 线程不安全,效率高 Vector 底层数据结构是数组,查询快,增删慢 线程 ...

  9. WinCE Show App Icon

    找个图片,例如背景透明的jpg或png,上http://www.pic2icon.com/smartphone_wince_icon_generator.php这个网站转换下. 在项目属性中,设为图标 ...

  10. IOS5基础教程之一-----如何创建XCode项目

    一.IOS的基础知识 1.只有一个应用程序正在运行.在IOS上,每一段时间内只能激活一个应用程序并在屏幕上显示. 2.只有一个窗口.只允许应用程序操作的一个窗口. 3.访问受限.只能在IOS为应用程序 ...