【HDU-2717】解题报告(BFS)
原始题目
Catch That Cow
- Time Limit: 5000/2000 MS (Java/Others)
- Memory Limit: 32768/32768 K (Java/Others)
- Total Submission(s): 17820
- Accepted Submission(s): 5260
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
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.
Source
USACO 2007 Open Silver
Recommend
teddy
题目大意
一个人想用最短的时间去抓住一头牛,牛不会动,人的移动规则是:下一次移动可以移动到X+1位置,X-1位置,或者2*X位置(X为当前位置坐标),现在给定人和牛的坐标,求人抓到牛所需要的最短时间(次数)
解题思路
很直接的广搜,搜索规则可以理解成三个direction,+1,-1,*2;最后可以加上一点处理(不知道算不算剪枝)因为坐标横大于0,那么若牛的初始坐标值小于人,那么无需广搜可以得到最短次数即坐标值之差(每次左移一个单位),另由于广搜深度增加,每次搜索的量会很大,可以加入判断搜索到结果时立即跳脱循环。
解题代码
1 |
|
收获与反思
使用模板里的结构时出现了两次MLE,不知道是什么个情况,有待检查。
本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!