博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ_3278_Catch That Cow
阅读量:6247 次
发布时间:2019-06-22

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

Catch That Cow
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 54911   Accepted: 17176

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 - 1 or + 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.
题解:
宽度优先搜索bfs
代码:
#include
#include
#include
using namespace std;#define N 100005struct Node{ int x; int step;};queue
q;bool visit[N];bool ok(int x){ if(x>=0 && x<=100000) return 1; return 0;}int cal(int i,int x){ if(i==0) return x+1; else if(i==1) return x-1; else return x*2;}void bfs(int a,int b){ memset(visit,0,sizeof(visit)); while(!q.empty()) { q.pop(); } Node aa; aa.step=1; aa.x=a; q.push(aa); visit[aa.x]=1; while(!q.empty()) { Node tmp=q.front(); q.pop(); for(int i=0; i<3; i++) { int y=cal(i,tmp.x); if(ok(y)&&!visit[y]) { if(y==b) cout<
<
>n>>k; if(k>n) bfs(n,k); else cout<<(n-k); return 0;}

 

转载于:https://www.cnblogs.com/jasonlixuetao/p/4531101.html

你可能感兴趣的文章
一个简单的ajax
查看>>
(筆記) initial的幾個特色 (SOC) (Verilog)
查看>>
CSS学习(四)CSS选择符详解
查看>>
IPMSG
查看>>
正则 截取固定开头结尾字符串中间的字符串
查看>>
电子书下载:Building Web Applications with SVG
查看>>
快速排序(QuickSort)用C# 实现的小例子
查看>>
.NET 3.5(7) - LINQ查询操作符之First、FirstOrDefault、Last、LastOrDefault
查看>>
坐标系统哪些事
查看>>
linux cp覆盖每次都有提示
查看>>
Msdn Enhanced Search
查看>>
Expression Tree Visualizer的使用
查看>>
我的美丽的家乡
查看>>
Java对象池技术的原理及其实现
查看>>
hdu 1035 Robot Motion(dfs)
查看>>
Android TextWatcher监控EditText中的输入内容并限制其输入字符个数
查看>>
socket Blocking connections
查看>>
使用 Buildot 实现持续集成(转载)
查看>>
Top 10 Universities for Artificial Intelligence
查看>>
LintCode,hihoCoder,LeetCode有什么区别?
查看>>