catch that cow java_POJ3278——Catch That Cow
发布日期:2021-06-24 13:17:03 浏览次数:4 分类:技术文章

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

Catch That

Cow

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.

题目大意:

有个农夫在N点,有个牛在K点。农夫有三种移动方式:1)向前一步 2)向后一步 3)从当所在位置X瞬间移动到2*X点

输出最少进行多少步,农夫能找到牛。(大坑是农夫和牛的活动范围在[0,100000])

结题思路:

bfs即可。注意农夫的可活动范围

Code:

48304ba5e6f9fe08f3fa1abda7d326ab.png1 #include

2 #include

3 #include

4 #include

5 #include

6 using namespacestd;7 int dis[100005],vis[100005],N,K;8 queueq;9 int bfs(int N,intK)10 {11 q.push(N);12 dis[N]=0,vis[N]=1;13 while (!q.empty())14 {15 int x[5],i;16 int front=q.front();17 q.pop();18 x[1]=front-1,x[2]=front+1,x[3]=front*2;19 for (i=1; i<=3; i++)20 {21 if (x[i]>=0&&x[i]<=100000&&!vis[x[i]])22 {23 q.push(x[i]),vis[x[i]]=1,dis[x[i]]=dis[front]+1;24 if (x[i]==K) returndis[x[i]];25 }26 }27 }28 }29 intmain()30 {31 cin>>N>>K;32 if (N==K) cout<<0;33 else

34 cout<

48304ba5e6f9fe08f3fa1abda7d326ab.png

原文:http://www.cnblogs.com/Enumz/p/3768282.html

转载地址:https://blog.csdn.net/weixin_33073525/article/details/114874909 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:在java中下面对于构造函数描述正确的是_在Java中,下面对于构造函数的描述正确的是()。(选择一项)...
下一篇:Java怎么使用spring定时器_浅析spring定时器的使用

发表评论

最新留言

初次前来,多多关照!
[***.217.46.12]2024年04月10日 15时42分20秒