HDU 2717 Catch That Cow (深搜)
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
HintThe 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,+1,*2
代码:
#include<iostream>
#include<stdio.h>
#include<string>
#include<cstring>
#include<math.h>
#include<queue>
using namespace std;
int N=1000000;
int a[1010000]={0};防止走重,即第一步走过了后面不应该再走一次
struct ft//结构体,包含数和步数
{
int x,y;
}p;
int k;
int bfs(int n)
{
queue<ft>Q;//建队
p.x=n,p.y=0;
a[n]=1;
Q.push(p);入队
ft cur, nex;
while(!Q.empty())
{
cur=Q.front();取队首元素
Q.pop();出队
if(cur.x==k)return cur.y;//判断是否达到目的
nex.x=cur.x+1;//判断+1的操作是否合法
if(nex.x>=0&&nex.x<N&&a[nex.x]==0)
{
nex.y=cur.y+1;
a[nex.x]=1;
Q.push(nex);合法则入队并步数+1
}
nex.x=cur.x-1;
if(nex.x>=0&&nex.x<N&&a[nex.x]==0)
{
nex.y=cur.y+1;
a[nex.x]=1;
Q.push(nex);
}
nex.x=cur.x*2;
if(nex.x>=0&&nex.x<N&&a[nex.x]==0)
{
nex.y=cur.y+1;
a[nex.x]=1;
Q.push(nex);
}
}
return -1;
}
int main()
{
int n;
while(~scanf("%d %d",&n,&k))
{
memset(a,0,sizeof(a));//多次运行,刷0;
int bs=bfs(n);
if(bs>=0)
printf("%d\n",bs);
}
return 0;
}
HDU 2717 Catch That Cow (深搜)的更多相关文章
- 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 --- 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 Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- 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
Catch That Cow Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- HDOJ/HDU 2717 Catch That Cow 一维广度优先搜索 so easy..............
看题:http://acm.hdu.edu.cn/showproblem.php?pid=2717 思路:相当于每次有三个方向,加1,减1,乘2,要注意边界条件,减1不能小于0,乘2不能超过最大值. ...
- 题解报告:hdu 2717 Catch That Cow(bfs)
Problem Description Farmer John has been informed of the location of a fugitive cow and wants to cat ...
随机推荐
- iOS-AFNetworking与ASIHTTPRequest的区别
一.底层实现 1.AFN的底层实现基于OC的NSURLConnection和NSURLSession 2.ASI的底层实现基于纯C语言的CFNetwork框架 3.因为NSURLConnectio ...
- Jenkins系列-Jenkins构建触发器
触发器说明 build whenever a snapshot dependency is built,当job依赖的快照版本被build时,执行本job. 触发远程构建 (例如,使用脚本):这里使用 ...
- Java字符串2
if(str!= null && str.isEmpty()){ dao.get(str); } str.isEmpty判断字符串是否为空字符串
- [清华集训2017]无限之环(infinityloop)
description 题面 solution 一开始的思路是插头\(DP\),然而复杂度太高 考虑将网格图黑白染色后跑费用流 流量为接口数,费用为操作次数 把一个方格拆成五个点,如何连边请自行脑补 ...
- BZOJ2816:[ZJOI2012]网络——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=2816 https://www.luogu.org/problemnew/show/P2173 有一 ...
- node egg.js使用superagent做文件转发
使用 egg.js + superagent 进行文件上传转发 // app/controller/file.js const Controller = require('egg').Controll ...
- [nginx]proxy_pass&rewrite知识点
While passing request nginx replaces URI part which corresponds to location with one indicated in pr ...
- javascript常用实例的实现与封装
地址:https://github.com/chenhuiYj/ec-do 2.字符串操作 2-1去除字符串空格 //去除空格 type 1-所有空格 2-前后空格 3-前空格 4-后空格 fu ...
- oracle重新编译失效对像
重新编译失效对像可执行utlrp.sql文件: SQL> @?/rdbms/admin/utlrp.sql TIMESTAMP --------------------------------- ...
- uboot的硬件驱动
1.uboot借用(移植)了linux驱动(1)linux驱动本身做了模块化设计.linux驱动本身和linux内核不是强耦合的,这是linux驱动可以被uboot借用(移植)的关键.(2)uboot ...