HDU-2717-Catch That Cow(bfs)
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
题意描述:
把一个数变成另一个数,可以通过加1或减1或者变成现在数的2倍,求最少的步数
程序代码:
#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;
struct data{int x;int y;int step;
};
int book[200010];
int main()
{int a,b,k;struct data A,B,next;queue<data>que;while(scanf("%d%d",&a,&b)!=EOF){while (!que.empty()){que.pop();}A.x=a;A.step=0;memset(book,0,sizeof(book));book[A.x]=1;que.push(A);while(!que.empty()){B=que.front();if(B.x==b){printf("%d\n",B.step);break;}que.pop();for(k=0;k<3;k++){next=B;if(k==0)next.x=next.x+1;if(k==1)next.x=next.x-1;if(k==2)next.x=next.x*2;next.step++;if(next.x>=0&&next.x<=200000&&book[next.x]==0){book[next.x]=1;que.push(next); }}}}return 0;
}
HDU-2717-Catch That Cow(bfs)相关推荐
- HDU 2717 Catch That Cow(BFS)
题目链接 好裸,BFS.杭电多组..2A.. 1 #include <stdio.h> 2 #include <string.h> 3 int p[100001],o[1000 ...
- POJ 3278 Catch That Cow(BFS)
题目网址:http://poj.org/problem?id=3278 题目: Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Tot ...
- POJ 3278 / hdu 2717 Catch That Cow (广搜)
POJ 3278 HDU 2717 广搜题,用一个数组标记就可以过,不标记的话会超内存. 另外,poj的数据要比hdu强一些,比如0 100,这种数据.不特判的话会RE.不过如果不特判,在poj上用C ...
- C - Catch That Cow(BFS)
找奶牛 有三种移动方式,作为BFS搜索的方向 利用 当 n > k 时只能向后走,优化结果 #include<iostream> #include<cstring> #i ...
- poj 3278 Catch That Cow (bfs)
题目:http://poj.org/problem?id=3278 题意: 给定两个整数n和k 通过 n+1或n-1 或n*2 这3种操作,使得n==k 输出最少的操作次数 1 #include< ...
- HDU2717 Catch That Cow(bfs)
题意:农夫的牛跑了,给出农夫和牛在坐标轴上的位置n和k,农夫每次只能从点n移动到n-1.n+1或者n*2的位置.输出抓到牛所需要的最小移动次数. 思路:思路明显的bfs,每次搜索只按照这三种方式,标记 ...
- 【HDU 2612 Find a Way(BFS)】(兼BFS入门笔记)
[HDU 2612 Find a Way(BFS)](兼BFS入门笔记) 原题入口: http://acm.hdu.edu.cn/showproblem.php?pid=2612 第一篇在CSDN的博 ...
- POJ3278Catch That Cow(BFS)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 70652 Accepted: 22216 ...
- (BFS)Catch That Cow(poj3278)
题目: 农夫知道一头牛的位置,想要抓住它.农夫和牛都于数轴上 ,农夫起始位于点 N(0<=N<=100000) ,牛位于点 K(0<=K<=100000) .农夫有两种移动方式 ...
最新文章
- Http 状态码一览表
- mysql 备份_MySQL数据库备份实操
- python数据结构 树_Python数据结构——AVL树的实现
- 美团(Leaf)分布式ID算法
- 项目管理工程师:第二章信息系统服务管理
- SSM框架笔记13:Spring MVC基础
- 为informix数据库中的表创建同义词
- Linux 给普通用户分配root权限或给用户分配多个用户组
- css鼠标滑过变大,css 鼠标移上去会变大(示例代码)
- 三年程序员成功转型项目经理
- Python绘制心形图案
- Centos7快速搭建服务器加速
- pbe近似_量子化学中的主要近似.doc
- 计算机参数含义,电脑内存条参数各有什么含义?
- ffmpeg入门篇-ffmpeg是怎么转码的?
- Android App签名(证书)校验过程源码分析
- 华为OJ——参数解析
- windows10 英文路径下文件显示中文名称
- android蓝牙室内定位,基于蓝牙的室内定位系统设计与实现
- java2d游戏代码_JAVA游戏编程学习笔记(三)Java 2D游戏底层绘图框架
热门文章
- nginx.conf文件详解
- 20155322 《Java程序设计》课堂实践项目 数据库-3-4
- Thrift框架使用C++的一个demo
- .NET中方法的注意事项 明细
- 蓝点中文_linux2.0 实验二 简单shell命令
- Error configuring application listener of class org.springframework.web.context.ContextLoaderListene
- 性能测试之JMeter主页面布局
- Jmeter如何进行http接口测试
- 都是执行软件测试,差异点在那里
- 性能测试第一步——查看设备的硬件配置