博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C - 青铜五 HDU - 2717 Catch That Cow BFS
阅读量:7078 次
发布时间:2019-06-28

本文共 2060 字,大约阅读时间需要 6 分钟。

 

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.

题意:一维地图上有两个点,最少多少步可以 从起点到终点;人可以:1.向左或右走一步;2.直接从当前位置x传送到2*x

搜索,没啥好说的

#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
typedef long long LL;typedef long double LD;using namespace std;const int maxn=100000*2;int vis[maxn];int f[8][2]={ {-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}};int n,m;struct node{ int x; int step;};queue
q;void bfs(node st,node en){ q.push(st); //printf("*****\n"); while(!q.empty()) { node t=q.front(); //printf("%d %d\n",t.x,t.step); q.pop(); if(t.x==en.x) { printf("%d\n",t.step); return; } if(t.x+1<=en.x&&vis[t.x+1]==0) { q.push((node){t.x+1,t.step+1}); vis[t.x+1]=1; } if(t.x-1>=0&&vis[t.x-1]==0) { q.push((node){t.x-1,t.step+1}); vis[t.x-1]=1; } if(t.x*2<=2*en.x&&vis[t.x*2]==0) { q.push((node){t.x*2,t.step+1}); vis[t.x*2]=1; } }}int main(){ int N,K; while(~scanf("%d%d",&N,&K)) { //printf("$$$$$$$$$$$$\n"); node st,en; st.x=N; st.step=0; en.x=K; memset(vis,0,sizeof(vis)); vis[N]=1; while(!q.empty())q.pop(); bfs(st,en); } return 0;}

 

转载于:https://www.cnblogs.com/107acm/p/9428318.html

你可能感兴趣的文章
Windows8/Silverlight/WPF/WP7/HTML5周学习导读(1月28日-2月3日)
查看>>
/tmp分区满,把oracle rac弄死了
查看>>
深入浅出linux系统umask值及其对应的文件权限讲解
查看>>
企业生产一线管理应找怎样的好帮手?
查看>>
MySQL数据库常用基本命令应用分享01
查看>>
实现线上高性能接口方案nginx负载tornado后端lua数据
查看>>
IT项目中存储设备的选型
查看>>
zabbix proxy配置文件不能把DBhost设置成远程数据库?
查看>>
疯狂ios之疯狂打飞机游戏(3)
查看>>
我的友情链接
查看>>
AWS的十年发展之路-永远前行
查看>>
Windows 2008 R2之三十六ADCS实现跨森林注册(二)
查看>>
最全团队管理手册
查看>>
浅谈在Linux中磁盘超出2T的管理方式
查看>>
安装Office 2010时1402错误的处理
查看>>
个人笔记ORA-32017 ORA-16179
查看>>
MSDE2000与SQLExpress2005共存时如何远程访问
查看>>
跨域组播---BGP+MSDP
查看>>
Microsoft Dynamics CRM server 2015 开发 之 安装visual studio 2012
查看>>
监控利器Nagios之二:Nagios的细致介绍和监控外部服务器的私有信息
查看>>