POJ 3278 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 on a number line and the cow is at a point on the same number line. Farmer John has two modes of transportation: walking and teleporting.
- Walking: FJ can move from any point to the points or in a single minute
- Teleporting: FJ can move from any point to the point 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: and
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.
Translation
农夫丢了奶牛,要去找奶牛。农夫和奶牛在一条数轴上,奶牛不动,农夫位于点,有三种走法(根据题意),每走一步花费一分钟,问至少花费几分钟找到奶牛。
Idea
由于每一种走法花费的时间都是相同的,可以采用的方法。
坑点是需要特判,如果,就要输出。
Code
1 |
|