HDU-2717 BFS
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
题意:
求X轴上n->k的最短步骤,(n->n-1,n->n+1,n->2*n)
解题思路:
BFS
代码:
#include<bits/stdc++.h>
using namespace std;
struct state{int x,step;
};
int vis[200001];
int bfs(int n,int k){memset(vis,0,sizeof(vis));state str,now,next;str.x=n,str.step=0;vis[n]=1;int tp;queue<state>q;q.push(str);while(!q.empty()){now=q.front(),q.pop();if(now.x==k)return now.step;for(int i=0;i<3;++i){if(i==0) tp=now.x-1;else if(i==1) tp=now.x+1;else tp=(now.x<<1);if(vis[tp]||tp<0||tp>100000) continue;vis[tp]=1;next.x=tp;next.step=now.step+1;q.push(next);}}return -1;
}
int main(){int n,k;while(~scanf("%d%d",&n,&k)){if(n>=k)printf("%d\n",n-k);else printf("%d\n",bfs(n,k));}return 0;
}
HDU-2717 BFS相关推荐
- HDU 2717(* bfs)
题意是在一个数轴上,每次可以一步到达当前位置数值的 2 倍的位置或者数值 +1 或数值 -1 的位置,给定 n 和 k,问从数值为 n 的位置最少多少步可以到达数值为 k 的位置. 用广搜的方法,把已 ...
- hdu 2717 bfs
Problem Description Farmer John has been informed of the location of a fugitive cow and wants to cat ...
- POJ 3278 / hdu 2717 Catch That Cow (广搜)
POJ 3278 HDU 2717 广搜题,用一个数组标记就可以过,不标记的话会超内存. 另外,poj的数据要比hdu强一些,比如0 100,这种数据.不特判的话会RE.不过如果不特判,在poj上用C ...
- HDU 2717 Catch That Cow(BFS)
题目链接 好裸,BFS.杭电多组..2A.. 1 #include <stdio.h> 2 #include <string.h> 3 int p[100001],o[1000 ...
- 牛客假日团队赛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++ 3 ...
- 【HDU - 2717】【POJ - 3278】Catch That Cow (经典bfs,类似dp)
题干: Farmer John has been informed of the location of a fugitive cow and wants to catch her immediate ...
- hdu 5433(bfs+dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5433 解题思路: dp[i][j][k]表示在(x,y)点,毅力为k时的最小体力.由于每个点可能会走多 ...
- hdu 2579 BFS
http://acm.hdu.edu.cn/showproblem.php?pid=2579 题目大意:给定 r * c 的迷宫,还有一个整数 k .迷宫中"."表示可以走,&qu ...
- D - Age of Moyu HDU - 6386 -BFS+DFS分层最短路
D - Age of Moyu HDU - 6386 题意:双向图,m条边,每条边有一个编号id,而花费就是根据这个id的变化来确定的求1-n的最短路 思路:全值为0,1的图直接bfs按层遍历图找到n ...
- hdu 5167 bfs
因为斐波那契数列增长很快(指数级),所以10Y以内只有不到50个斐波那契数,将这些数字所有可能的乘积存起来查询即可,这里采用bfs+set的方式. 1 #include <iostream> ...
最新文章
- 排序--直接插入排序
- ID3DXMesh的数据导出和导入
- 每日程序C语言6-判断某范围之间的素数
- Centos7忘记密码——转
- [置顶] 任务三 主题、样式
- 剖析Caffe源码之Net(上)---NetParameter参数
- 11门满分、10门99分、47门超95分……他却说自己是“学zha”
- 【Android】自己定义控件——仿天猫Indicator
- 每周一个 Python 模块 | array
- Mac OS X Tips
- R|ggplot2(七)|自定义主题
- Handler+MessageQueue等操作
- XeThru Sensors SW Module Connector-matlab
- MINI2440 TD35 P35触摸屏不能使用? 让我们来把一线触控改四线触控
- 习题:求1-n的阶乘之和,用一个非递归函数fac(n)求n!,n的值由主函数输入,最终得到的结果在主函数中输出。
- 火狐浏览器打开发现是2345的网站-----解决方法
- SpringBoot学习-Day1
- native vlan设置错误导致无法访问的问题排查过程
- C++关于float和double的有效位数
- Python 调试器 - ipdb
热门文章
- BCIduino社区|基于肌电图的机械手单个手指控制
- 【Jetson-Nano】jetson_nano安装环境配置及tensorflow和pytorch的安装
- 计算机在音乐教学的应用论文,【音乐教学论文】信息技术在音乐教学的应用(共2464字)...
- c语言计算器取百分数,简易计算器的代码--------我终于可以编40行以上的代码了...
- UI 自动化中如何处理上传文件事件
- R实战 | 文章第一表:三线表的绘制
- mysql中导入xml文件_xml文件导入MySQL数据库
- Codeforces 652C Foe Pairs 【dp】
- 用科技词汇润色下句:并将模型在中国西北地区应用,对水碳能耦合循环过程进行定量评估和时空格局分析,旨在优化区域生态系统管理策略。...
- 3DMAX里面的烘焙规范(一)