题解报告: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不能超过最大值. ...
随机推荐
- 【BZOJ3925】地震后的幻想乡(期望概率DP,状压DP)
题意:给定一张点数不超过10的无向连通图,每条边有一个[0,1]之间的随机权值,求最小生成树上最大边的期望值 提示:对于n个[0,1]之间的随机变量x1,x2,...,xn,第k小的那个的期望值是k/ ...
- vim状态栏的扩充
将以下内容添加到~/.vimrc文件中: set statusline= set statusline+=%7*\[%n] " ...
- 封装HttpURLConnection
package com.pingyijinren.test; import java.io.BufferedReader; import java.io.InputStream; import jav ...
- Word Search(深度搜索DFS,参考)
Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...
- MongoDB学习day02--数据库增删改查
(window系统,在cmd命令提示符中使用) 一.数据库使用 管理mongodb数据库:mongo,连接本地数据库,或mongo 127.0.0.1:27017,连接其他服务器:mongo ip: ...
- Nginx与HAProxy的区别
对于做软负载,我们都知道主流的方案有LVS.Haproxy.Nginx!那么对于Haproxy和Nginx,我们如何选择呢?回答这个问题之前,我根据个人使用经验来讲下它们的特点! Haproxy特点 ...
- WebLogic(12C)——简单公布和JDBC
一,简单公布应用 1.点击"安装".開始部署应用: 2,找到要部署的项目路径: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaGFue ...
- VMware实用技巧
1.VM快照管理 这个功能实在太常用,不用我多废话.这里只是提醒一下还没有用过快照的同学,赶紧的给自己的VM保存点快照吧,这样VM里的系统出了问题或是有其它需要很容易让你还原到原来的某个点,这功能可比 ...
- oracle initialization or shutdown in progress 问题解决
今天登录oracle时遇到oracle initialization or shutdown in progress 这个错误提示,在网上搜了下,试了非常多方法,最后结合几种方法结合,成功攻克了问题! ...
- 开拓新途径找出新方法,上海SEO公司分享3个操作看看是否可行
开拓新途径找出新方法,上海SEO公司分享3个操作看看是否可行 内容收录,外链公布,流量点击.用户体验.这是SEO优化的几个核心和重点.也是SEO站长每天都在绞尽脑汁进行操作的SEO重心,影响着非常多人 ...