[ACM] hdu 2717 Catch That Cow (BFS)
来源:互联网 发布:齐鲁石化网络电视台 编辑:程序博客网 时间:2024/05/01 18:06
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?
* 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
4HintThe 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.
Source
USACO 2007 Open Silver
从位置n到位置k根据规则最少走几步可以到达,规则是从n可以走到n-1,可以走到n+1,也可以走到n*2.用bfs广搜来做,vis[]用来记录是否访问,hash[]用来记录走的步数。
遇到的问题有数组越界,一定要先判断n*2,n-1,n+1是否越界,还有问题就是步数的保存,因为很多节点都是同一个步数下的状态,因为每一步根据规则可以衍生出三种状态。所以用hash[ q.front() 下一个状态]=hash[q.front()]+1.用这种方法来记录步数,最后直接输出hash[k]就可以了。
代码:
0 0
- [ACM] hdu 2717 Catch That Cow (BFS)
- [ACM] hdu 2717 Catch That Cow (BFS)
- hdu 2717 Catch That Cow(bfs)
- hdu 2717 Catch That Cow(BFS)
- HDU 2717 Catch That Cow(BFS)
- HDU 2717 Catch That Cow (BFS)
- 【HDU 2717】Catch That Cow(BFS)
- 【HDU】-2717-Catch That Cow(BFS)
- HDU 2717 Catch That Cow(bfs)
- hdu 2717 Catch That Cow bfs
- HDU 2717 Catch That Cow (BFS)
- HDU 2717--Catch That Cow【BFS】
- HDU 2717:Catch That Cow 【bfs】
- hdu-2717 Catch That Cow (BFS)
- HDU 2717 Catch That Cow(BFS)
- HDU 2717 Catch That Cow 水题BFS
- HDU 2717 Catch That Cow [BFS]
- HDU 2717 Catch That Cow (BFS)
- 对象的序列化与反序列化---IO学习笔记(四)
- [ACM] hdu 1242 Rescue (BFS+优先队列)
- SpringMVC 集成JSR303数据校验
- 栈的顺序存储实现
- HDU 1874--畅通工程续【最短路 && floyd && 水题】
- [ACM] hdu 2717 Catch That Cow (BFS)
- Get Luffy Out (poj 2723 二分+2-SAT)
- LeetCode之Recover Binary Search Tree
- OC之集合家族
- machine learning in coding(python):拼接原始数据;生成高次特征
- [ACM] hdu 1035 Robot Motion (模拟或DFS)
- c++学习笔记
- POJ 2750 Potted Flower(线段树 + DP)
- No package 'theoraenc' found gstreamer