博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[kuangbin带你飞]专题一 简单搜索 C
阅读量:4557 次
发布时间:2019-06-08

本文共 1982 字,大约阅读时间需要 6 分钟。

C - Catch That Cow

题目链接:

题目:

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 题意: 农夫约翰已被告知逃亡牛的位置,并希望立即抓住她。 他从数字线上的N点(0≤N≤100,000)开始,并且母牛在同一数字线上的点K(0≤K≤100,000)。 农夫约翰有两种交通方式:步行和传送。 *步行:FJ可以在一分钟内从任何一点X移动到X  -  1或X + 1点 *传送:FJ可以在一分钟内从任意点X移动到2×X点。 如果母牛不知道它的追求,根本不动,那么农夫约翰需要多长时间才能找回它? 输入 第1行:两个以空格分隔的整数:N和K. 产量 第1行:Farmer John捕捉逃亡牛所需的最短时间(以分钟为单位)。 思路:广搜
#include
#include
#include
#include
using namespace std;bool book[maxn];int fan[maxn];queue
qu;int bfs(int N,int K){ int tou,xia;//tou是现在位置,xia是下一步的位置 qu.push(N);//把现在位置压入队列中, fan[N]=0;//一开始步数为0 book[N]=true;//该位置已经被走过了 while(!qu.empty()) { tou=qu.front();//将现在的位置存入队列中 qu.pop();//删去队首 //cout<
<
100000)//防止越界 continue; if(!book[xia]) { qu.push(xia);//把下一步的位置存入队列中 fan[xia]=fan[tou]+1; book[xia]=true;//已经走过 } } } return fan[xia];}int main(){ int N,K; while(cin>>N>>K){ memset(fan,0,sizeof(fan)); memset(book,false,sizeof(book)); while(!qu.empty()) { qu.pop(); } cout<
<

 

转载于:https://www.cnblogs.com/Vampire6/p/11143182.html

你可能感兴趣的文章
Java并发编程:并发容器之ConcurrentHashMap
查看>>
Java范例集锦(二)
查看>>
C语言变量和常量
查看>>
LInuxDay8——shell脚本编程基础
查看>>
topcoder 673
查看>>
Java中一些常用的类,包,接口
查看>>
下载特定区域内街景照片数据 | Download Street View Photos within Selected Region
查看>>
StarUML 破解方法
查看>>
C语言结构体
查看>>
[转]Tribon船体生产设计应用
查看>>
easy ui datagrid 让某行复选框不能选中
查看>>
第六周作业
查看>>
关于adb端口被占用的解决办法
查看>>
php 部分内置函数的使用
查看>>
字符串处理技巧
查看>>
归档及压缩命令
查看>>
Mybatis步骤
查看>>
WPF自定义控件之扩展原生控件
查看>>
《区块链100问》笔记整理——42~49问
查看>>
使用Jquery+EasyUI 进行框架项目开发案例讲解之二---用户管理源码分享
查看>>