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 ...
随机推荐
- Debian常用软件
1. 有道词典 https://github.com/justzx2011/openyoudao
- iOS- 无处不在,详解iOS集成第三方登录(SSO授权登录<无需密码>)
1.前言 不多说,第三登录无处不在!必备技能,今天以新浪微博为例. 这是上次写的iOS第三方社交分享:http://www.cnblogs.com/qingche/p/3727559.html 可 ...
- asp.net 后台注册(调用)JS
1.使用Page.ClientScript.RegisterClientScriptBlock 使用 Page.ClientScript.RegisterClientScriptBlock可以防止ja ...
- 【week4】技术随笔psp
本周psp
- Python实现XML的操作
本文从以下两个方面, 用Python实现XML的操作: 一. minidom写入XML示例1 二. minidom写入XML示例2 三. ElementTree写入/修改示例 四. ElementTr ...
- FineCMS介绍
产品简介 FineCMS(简称免费版.企业版.公益版)是一款基于PHP+MySql+CI框架开发的高效简洁的中小型内容管理系统,面向多终端包括Pc端网页和移动端网页,支持自定义内容模型和会员模型, ...
- vs2015常用代码块与自定义代码块
常用代码块 代码段名 描 述 #if 该代码段用#if和#endif命令围绕代码 #region 该代码段用#region和#endregion命令围绕代码 ~ 该代码段插入一个析构函数 att ...
- Qt——树结点的搜索
一.Qt中的树 平时我们经常使用树的结构来组织和展示数据,比如文件系统等—— 在Qt中,我们可以使用Qt提供的便捷的QTreeWidget类,利用该类的接口,轻松地将已有数据显示在树中. 除此之外,还 ...
- hdu 1086 You can Solve a Geometry Problem too (几何)
You can Solve a Geometry Problem too Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/3 ...
- UVA.11464 Even Parity (思维题 开关问题)
UVA.11464 Even Parity (思维题 开关问题) 题目大意 给出一个n*n的01方格,现在要求将其中的一些0转换为1,使得每个方格的上下左右格子的数字和为偶数(如果存在的话),求使得最 ...