题解报告:hdu 2717 Catch That Cow(bfs)
Problem Description
* 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
Output
Sample Input
Sample Output
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.
#include<iostream>
#include<queue>
#include<string.h>
using namespace std;
const int maxn=1e5+;
int n,k,cnt;bool vis[maxn];//标记是否访问
struct node{int x,step;}nod;//标记达到当前位置的步数
queue<node> que;
void bfs(int x){
while(!que.empty())que.pop();//清空
memset(vis,false,sizeof(vis));
nod.x=n,nod.step=;vis[x]=true;//同时将初始位置x标记为true
que.push(nod);//先将初始位置入队
while(!que.empty()){
nod=que.front();que.pop();
if(nod.x==k){cout<<nod.step<<endl;return;}//这一步不能忘记,不然会出错,如果当前节点的值和k相等,则直接返回所花费的时间为0
for(int i=;i<;++i){//遍历三次操作,查看是否还有可以到达的地方
node next=nod;//每一次操作都从原来那个位置到另一个位置
if(i==)next.x-=;
else if(i==)next.x+=;
else next.x*=;
next.step++;//到达对应位置的时间在原来的基础上加1
if(next.x==k){cout<<next.step<<endl;return;}//如果到达终点,则直接返回所花费的时间
if(next.x>=&&next.x<maxn&&!vis[next.x]){//如果下一个位置在0~10^5范围内,并且还未访问,就可以将其入队
vis[next.x]=true;//将其标记为已访问状态
que.push(next);//将下一个位置入队
}
}
}
}
int main(){
while(cin>>n>>k){bfs(n);}
return ;
}
题解报告:hdu 2717 Catch That Cow(bfs)的更多相关文章
- 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,最先 ...
- 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 ...
- HDU 2717 Catch That Cow(常规bfs)
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2717 Catch That Cow Time Limit: 5000/2000 MS (Java/Oth ...
- 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 ...
- hdu 2717:Catch That Cow(bfs广搜,经典题,一维数组搜索)
Catch That Cow Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- hdu 2717 Catch That Cow(广搜bfs)
题目链接:http://i.cnblogs.com/EditPosts.aspx?opt=1 Catch That Cow Time Limit: 5000/2000 MS (Java/Others) ...
- 杭电 HDU 2717 Catch That Cow
Catch That Cow Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- hdu 2717 Catch That Cow(BFS,剪枝)
题目 #include<stdio.h> #include<string.h> #include<queue> #include<algorithm> ...
- HDOJ/HDU 2717 Catch That Cow 一维广度优先搜索 so easy..............
看题:http://acm.hdu.edu.cn/showproblem.php?pid=2717 思路:相当于每次有三个方向,加1,减1,乘2,要注意边界条件,减1不能小于0,乘2不能超过最大值. ...
随机推荐
- [K/3Cloud] 分录行复制和新增行的冲突如何处理
新增行:执行AfterCreateNewEntryRow,这个函数里面对一些数据进行处理(比如字段给上默认值): 复制行:复制行过程中希望这些字段能够得到我修改行信息后的数据,如果不处理,执行到Aft ...
- 【NOIP2017练习】溢出(模拟)
题意: 思路: a*b<=c <====> b<=c div a var ch,maxs,s:ansistring; v,k,i,maxl,l,len,cnt,cas:long ...
- COGS2479(四维偏序)
题意:给定一个有n个元素的序列,元素编号为1~n,每个元素有三个属性a,b,c,求序列中满足i<j且ai<aj且bi<bj且ci<cj的数对(i,j)的个数. 分析:cdq分治 ...
- Spring在Java Filter注入Bean为Null的问题解决
在Spring的自动注入中普通的POJO类都可以使用@Autowired进行自动注入,但是除了两类:Filter和Servlet无法使用自动注入属性.(因为这两个归Web容器管理)可以用init(集承 ...
- JSP国际化设置
以下内容引用自http://wiki.jikexueyuan.com/project/jsp/internationalization.html: 国际化(i18n):这意味着可以使网站根据访问者的语 ...
- elk实时日志分析平台部署搭建详细实现过程
原文:http://blog.csdn.net/mchdba/article/details/52132663 1.ELK平台介绍 在搜索ELK资料的时候,发现这篇文章比较好,于是摘抄一小段:以下内容 ...
- 【转】深入理解javascript作用域——词法作用域和动态作用域
前面的话 大多数时候,我们对作用域产生混乱的主要原因是分不清楚应该按照函数位置的嵌套顺序,还是按照函数的调用顺序进行变量查找.再加上this机制的干扰,使得变量查找极易出错.这实际上是由两种作用域工作 ...
- 集成环信时遇到的问题file not found: libEaseMobClientSDK.a
集成环信时遇到的问题 build setting环信SDK集成libEaseMobClientSDKL file not found: libEaseMobClientSDK.a clang: er ...
- 【C语言】推断一个数是否为2的n次方
//推断一个数是否为2的n次方 #include <stdio.h> int is_two_n(int num) { if ((num&(num - 1))) //去掉一个1,推断 ...
- 『NSOperation、NSOperationQueue』详解
1. NSOperation.NSOperationQueue 简介 NSOperation.NSOperationQueue 是苹果提供给我们的一套多线程解决方案.实际上 NSOperation.N ...