POJ 3278 Catch That Cow
C-Catch That Cow
Crawling in process...Crawling failedTime Limit:2000 MSMemory Limit:65536KB64bit IO Format:% I64d & % I64u
SubmitStatus
Description
Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a pointN(0 ≤N≤ 100,000) on a number line and the cow is at a pointK(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 pointXTo the pointsX-1 orX+ 1 in a single minute
* Teleporting: FJ can move from any pointXTo the point 2 ×XIn 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:
NAnd
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.
Question: How to change n to m with the minimum number of operations. N can only be incremented by one or multiplied by two.
Range: 0 to 100000
Eliminate the number of bfs, out-of-bounds, or searched data.
# Include
# Define inf 0x6ffff # include
# Include
# Include using namespace std; struct node {int x; int step ;}; int n, m; int flag [100005]; int bfs (int x) {int I; queue
Q; node st, ed; flag [x] = 1; st. x = x; st. step = 0; q. push (st); while (! Q. empty () {st = q. front (); q. pop (); if (st. x = m) return st. step; for (I = 1; I <= 3; I ++) {if (I = 1) ed. x = st. x + 1; if (I = 2) ed. x = st. x-1; if (I = 3) ed. x = st. x * 2; if (ed. x> 100000 | ed. x <0 | flag [ed. x]) // The number of searched records is marked as one. Cross-border exclusion is performed. Continue; ed. step = st. step + 1; flag [ed. x] = 1; q. push (ed) ;}} int main () {while (scanf ("% d", & n, & m )! = EOF) {memset (flag, 0, sizeof (flag); if (n = m) printf ("0 \ n "); else {int ans = bfs (n); printf ("% d \ n", ans) ;}} return 0 ;}