A广搜
<span style="color:#330099;">/*
A - 广搜 基础
Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Submit
Status
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
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.
By Grant Yuan
2014.7.13
*/
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<queue>
#include<cstdio>
using namespace std;
bool a[100003];
typedef struct{
int num;
int sum;
}dd;
queue<dd>q;
int n,k;
int res;
void slove()
{int m,count;
int x;
dd init;
while(!q.empty()){
if(q.front().num==n)
{ res=q.front().sum;
break;
}
m=q.front().num;
count=q.front().sum;
x=m-1;
if(a[x]==0)
{ init.num=x;
init.sum=count+1;
q.push(init);
a[x]=1;
}
x=m+1;
if(a[x]==0)
{
init.num=x;
init.sum=count+1;
q.push(init);
a[x]=1;
}
if(m%2==0){
x=m/2;
if(a[x]==0)
{
init.num=x;
init.sum=count+1;
q.push(init);
a[x]=1;
}}
q.pop();}
}
int main()
{
scanf("%d%d",&n,&k);
memset(a,0,sizeof(a));
dd init;
init.num=k;
init.sum=0;
a[k]=1;
q.push(init);
slove();
cout<<res<<endl;
return 0;
}
</span>
A广搜的更多相关文章
- HDU--杭电--1195--Open the Lock--深搜--都用双向广搜,弱爆了,看题了没?语文没过关吧?暴力深搜难道我会害羞?
这个题我看了,都是推荐的神马双向广搜,难道这个深搜你们都木有发现?还是特意留个机会给我装逼? Open the Lock Time Limit: 2000/1000 MS (Java/Others) ...
- HDU 5652(二分+广搜)
题目链接:http://acm.hust.edu.cn/vjudge/contest/128683#problem/E 题目大意:给定一只含有0和1的地图,0代表可以走的格子,1代表不能走的格 子.之 ...
- nyoj 613 免费馅饼 广搜
免费馅饼 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 都说天上不会掉馅饼,但有一天gameboy正走在回家的小径上,忽然天上掉下大把大把的馅饼.说来gameboy ...
- poj 3984:迷宫问题(广搜,入门题)
迷宫问题 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7635 Accepted: 4474 Description ...
- poj 3278:Catch That Cow(简单一维广搜)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 45648 Accepted: 14310 ...
- 双向广搜 POJ 3126 Prime Path
POJ 3126 Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16204 Accepted ...
- 广搜+打表 POJ 1426 Find The Multiple
POJ 1426 Find The Multiple Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 25734 Ac ...
- 双向广搜 codevs 3060 抓住那头奶牛
codevs 3060 抓住那头奶牛 USACO 时间限制: 1 s 空间限制: 16000 KB 题目等级 : 黄金 Gold 题目描述 Description 农夫约翰被告知一头逃跑奶牛 ...
- 双向广搜+hash+康托展开 codevs 1225 八数码难题
codevs 1225 八数码难题 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description Yours和zero在研究A*启 ...
- UVa12726 one Friend at a Time (位 广搜)
题目链接:UVa12726 是个PDF,不好复制进来. 大意:有个人要追个妹子,想加妹子QQ,但是不知道谁规定的,玩QQ的人要加好友必须先要有至少k个共同好友.共有N个人玩QQ,编号为1到N,1是男主 ...
随机推荐
- linux-ssh加密与https安全-9
非对称加密算法:RSA,DSA/DSS 对称加密算法:AES,RC4,3DES HASH算法:MD5,SHA1,SHA256 hash就是找到一种数据内容和数据存放地址之间的映射关系 (1) 文件校验 ...
- 如何修改 tomcat 端口号?
一.tomcat默认端口 tomcat默认的端口是8080,还会占用8005,8009和8443端口.如果已经启动了tomcat,再启动另一个tomcat就会发现 这些端口已经被占用了,这个时候就需要 ...
- 6.Python缩进规则(包含快捷键)
和其它程序设计语言(如 Java.C 语言)采用大括号“{}”分隔代码块不同,Python 采用代码缩进和冒号( : )来区分代码块之间的层次. 在 Python 中,对于类定义.函数定义.流程控制语 ...
- aws常用命令
EC2 挂载 EBS linux 查看块设备: lsblk 格式化磁盘: sudo mkfs -t ext4 /dev/xvdb 挂载卷: sudo mount /dev/xvdb /mnt/mydi ...
- Python基础面试题库
Python基础面试题库 Python是一门学习曲线较为容易的编程语言,随着人工智能时代的到来,Python迎来了新一轮的高潮.目前,国内知乎.网易(游戏).腾讯(某些网站).搜狐(邮箱).金山. ...
- VLC2.2.4命令参数
用法: vlc [选项] [流] ...您可以在命令行中指定多个流.它们将被加入播放列表队列.指定的首个项目将被首先播放. 选项样式: --选项 用于设置程序执行期间的全局选项. -选项 单字母版本的 ...
- python使用内置方法和修饰器方法获取类名、函数名
1. 外部获取 从外部的情况好获取,可以使用指向函数的对象,然后用__name__属性. def a(): pass a.__name__ 或者 getattr(a,'__name__') 2. 内部 ...
- RequestMapping 注解的解析、匹配、注册
RequestMapping 注解的解析.匹配.注册 1)创建 RequestMappingHandlerMapping 实例时会触发 afterPropertiesSet 调用. 2)读取容器中所有 ...
- 如何进行EDM邮件内容的撰写
近两个月没有来博客园更新一下博客文章了,实在惭愧,最近忙于工作,经常加班.下面来说说EDM邮件内容的撰写技巧吧,本文就跟大家交流一下自己一直以来的心得体会. EDM中很重要的一个步骤,就是邮件内容的撰 ...
- Java -- 泛型父类中获取子类的泛型T
原文:https://blog.csdn.net/u014723529/article/details/70574026 /** * 获取实体类型名称 * 子类可覆盖此方法,返回:泛型T的类名.cla ...