本文主要是介绍poj3278--Catch That Cow,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 43680 | Accepted: 13615 |
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
Output
Sample Input
5 17
Sample Output
4
Hint
Source
#include <stdio.h>
#include <string.h>
int p[1000000] ;
int a[1000000] , top , low ;
int main()
{int n , k ;memset(p,-1,sizeof(p));scanf("%d %d", &n, &k);top = 0 ; low = 0 ;a[top++] = n ;p[n] = 0 ;while(low < top){int s = a[low++] ;if(s == k)break;if( s - 1 >= 0 && p[s-1] == -1){p[s-1] = p[s]+1 ;a[top++] = s - 1 ;}if(s + 1 < 1000000 && p[s+1] == -1){p[s+1] = p[s]+1 ;a[top++] = s+1 ;}if(s * 2 < 1000000 && p[s*2] == -1){p[s * 2] = p[s] +1 ;a[top++] = s * 2;}}printf("%d\n", p[k]);return 0;
}
这篇关于poj3278--Catch That Cow的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!