Catch That Cow (BFS)
题目:
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?InputLine 1: Two space-separated integers: N and KOutputLine 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.
- 题意:
人抓牛,数轴上人在点N处,牛在点K处,通过前进1,后退1,当前位置乘以2三种方式到达K处,牛在K处不动,求人从N处到牛的K处所经过的最少步数;- 分析:
求最小路径,用BFS;
分为+1,-1,*2三种情况;
分为N在K之前或者在K上的情况和N在K之后的情况,N在K之前就只能通过-1的操作到达K
定义一个数组用于标记经过的位置坐标;
定义一个数组用于记录到达当前位置所需要的最少步数;
在三种情况下,满足范围即入队列,对当前的坐标进行标记,步数加1;- AC代码:
- #include<iostream>
- #include<cstdio>
- #include<cstring>
- #include<queue>
- using namespace std;
- const int Max=;
- int n,k;
- int f[Max];
- int step[Max];
- bool cmp(int x)
- {
- return (x>=&&x<=Max);
- }
- int a,b;
- void bfs()
- {
- queue<int>s;
- s.push(n);
- f[n]=;
- step[n]=;
- while (!s.empty())
- {
- a=s.front();
- s.pop();
- if (a==k)
- break;
- b=a-;
- if (cmp(b)&&!f[b])
- {
- s.push(b);
- f[b]=;
- step[b]=step[a]+;
- }
- b=a+;
- if (cmp(b)&&!f[b])
- {
- s.push(b);
- f[b]=;
- step[b]=step[a]+;
- }
- b=a*;
- if (cmp(b)&&!f[b])
- {
- s.push(b);
- f[b]=;
- step[b]=step[a]+;
- }
- }
- }
- int main()
- {
- while (scanf("%d %d",&n,&k)==)
- {
- memset(f,,sizeof(f));
- if (n>=k)
- printf("%d\n",n-k);
- else
- {
- bfs() ;
- printf("%d\n",step[k]);
- }
- }
- return ;
- }
Catch That Cow (BFS)的更多相关文章
- 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 ...
- POJ 3278 Catch That Cow(bfs)
传送门 Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 80273 Accepted: 25 ...
- 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 ...
- Catch That Cow(BFS)
Catch That Cow Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- ***参考Catch That Cow(BFS)
Catch That Cow Time Limit : 4000/2000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) Tot ...
- Catch That Cow (bfs)
Catch That Cow bfs代码 #include<cstdio> #include<cstring> #include<algorithm> #inclu ...
- hdu 2717:Catch That Cow(bfs广搜,经典题,一维数组搜索)
Catch That Cow Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- 【OpenJ_Bailian - 4001】 Catch That Cow(bfs+优先队列)
Catch That Cow Descriptions: Farmer John has been informed of the location of a fugitive cow and wan ...
- poj 3278(hdu 2717) Catch That Cow(bfs)
Catch That Cow Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- POJ3279 Catch That Cow(BFS)
本文来源于:http://blog.csdn.net/svitter 意甲冠军:给你一个数字n, 一个数字k.分别代表主人的位置和奶牛的位置,主任能够移动的方案有x+1, x-1, 2*x.求主人找到 ...
随机推荐
- 关于Weblogic的知识点
一.解决Weblogic域创建.启动.进入控制台慢问题 搭建Weblogic 11g和12c环境时发现,安装正常,以默认组件安装,但是创建域的时候特别慢,一般需要几分钟至10分钟,卡在“创建域安全信息 ...
- Myeclipse 10/2014 配置插件(svn、maven、properties、velocity)的方法
一.配置SVN详细图解 什么是SVN? 管理软件开发过程中的版本控制工具. 下面会以两种方式来介绍怎么安装svn,myeclipse安装SVN插件步骤,以myeclipse 2014为例,第一种是最常 ...
- NSPredicate 应用
//查询单词里面包含“ang”的字符串 NSArray *array = [[NSArray alloc]initWithObjects:@"beijing",@"sha ...
- Linux基础篇七:Linux的命令执行
首选区分内置命令和外置命令: 内置命令:shell程序自带的命令,系统内核一启动就可以使用的命令 外置命令:在系统PATH变量路径下的命令 如何查看一个命令是内置命令还是外置命令: type -a c ...
- day21-双下eq方法
class Goods: def __init__(self,name): self.name = name def __eq__(self,other): #self = apple1, other ...
- 常见字体图标库——font-awesome
1.简介 FontAwesome一种带有网页功能的象形文字语言,并收集在一个集合里.字库中有675个图标,只支持英文搜索,中文地址:http://www.fontawesome.com.cn/ 2.使 ...
- tomcat端口占用异常
错误记录--更改tomcat端口号方法,Several ports (8005, 8080, 8009) 2011年01月18日 01:34:00 阅读数:202700 启动Tomcat服务器报错: ...
- Socket设置超时时间
主要有以下两种方式,我们来看一下方式1: Socket s=new Socket(); s.connect(new InetSocketAddress(host,port),10000); 方式2: ...
- 关于使用MyEclipse自动生成Hibernate和Struts出现的jar不兼容的问题(antlr.collections.AST.getLine()I)
今天做Hibernate和Struts2结合的练习,使用MyEclipse自动创建Hibernate和Struts的相关配置文件和jar包,写完一个查询方法后,准备测试一下结果,简单的查询那种,很诡异 ...
- 吴裕雄--天生自然python学习笔记:爬取我国 1990 年到 2017年 GDP 数据并绘图显示
绘制图形所需的数据源通常是不固定的,比如,有时我们会需要从网页抓取, 也可能需从文件或数据库中获取. 利用抓取网页数据技术,把我国 1990 年到 2016 年的 GDP 数据抓取出来 ,再利用 Ma ...