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. android 开发(百度地图)

    百度地图开放平台:http://lbsyun.baidu.com/ 百度地图API:http://developer.baidu.com/map/reference/ Android地图SDK 百度地 ...

  2. 在 VirtualBox 中 CentOS 网络设置

    转自:本文发表于水景一页.永久链接:<http://cnzhx.net/blog/minimal-centos-in-virtualbox/>.转载请保留此信息及相应链接. 4. 设置¶ ...

  3. php中method_exists()和is_callable()如何进行语句判断

    method_exists()和is_callable()方法进行判断.那么两则区别是什么呢? 已知类文件如下: class Student{private $alias=null;private $ ...

  4. 提高PHP性能的实用方法+40个技巧优化您的PHP代码

    1.用单引号代替双引号来包含字符串,这样做会更快一些.因为PHP会在双引号包围的字符串中搜寻变量,单引号则不会,注意:只有echo能这么做,它是一种可以把多个字符串当作参数的"函数" ...

  5. HTTP Live Streaming直播(iOS直播)技术分析与实现

    本文转载自:http://www.cnblogs.com/haibindev/archive/2013/01/30/2880764.html 不经意间发现,大半年没写博客了,自觉汗颜.实则2012后半 ...

  6. Nginx使用手册目录

    Nginx学习总结[第一篇]: Nginx简介 Nginx第二篇:Nginx部署及使用 Nginx第三篇:Nginx日志处理 Nginx第四篇:Nginx优化 Nginx第五篇:Nginx日常管理

  7. codeigniter中base_url和site_url

    首先在网站中使用如下的语句: site_url(‘manage/articleAdd’): 1 <?php echo site_url('manage/articleAdd');?> ba ...

  8. ecshop编辑器fckeditor换百度ueditor编辑器教程

    1.下载uediter编辑器,解压上传目录uediter到根目录/includes/下   2.修改admin/includes/lib_main.php         /**   * 生成编辑器  ...

  9. 《Linux/Unix系统编程手册》

    TCP的TIME_WAIT状态,超时时间为2倍MSL(IP报文超过TTL前最大生存时间,BSD规范为30s,RFC1122建议为2分钟).

  10. ACM题目————星际之门(一)

    描述 公元3000年,子虚帝国统领着N个星系,原先它们是靠近光束飞船来进行旅行的,近来,X博士发明了星际之门,它利用虫洞技术,一条虫洞可以连通任意的两个星系,使人们不必再待待便可立刻到达目的地. 帝国 ...