poj 3278 Catch That Cow(bfs)

Catch That Cow
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 76520   Accepted: 24178

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 - 1 or + 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.
题目地址:http://poj.org/problem?id=3278
                                                           
 
【题意】人从n点出发找在k处不动的牛,在横轴上人只能从n到n-1,n+1,n*2三种步子,每步用时一分钟,求找到牛的最短用时。
 
【分析】这一道题是一维的,相对来说简单的很。我们在此处用bfs,但是要分开n>k和n<=k这两种情况。
 
代码一:
 1 #include<cstdio>
 2 #include<queue>
 3 #include<cstring>
 4 using namespace std;
 5 int n, k;
 6 struct Node{
 7     int num, step;
 8 }a, b;
 9 int vis[100005];
10 int bfs()
11 {
12     memset(vis, 0, sizeof(vis));
13     queue<Node> q;
14     a.num = n;
15     a.step = 0;
16     vis[n] = 1;
17     q.push(a);    
18     while(1)
19     {
20         a = q.front();
21         q.pop();
22         for(int i = 0; i < 3; i++)
23         {
24             if(i == 0)    b.num = a.num + 1;
25             if(i == 1)    b.num = a.num - 1;
26             if(i == 2)    b.num = a.num * 2;
27             if(b.num == k)
28                 return a.step + 1;
29             if(b.num < 100005 && b.num >= 0 && !vis[b.num])
30             {
31                 b.step = a.step + 1;
32                 vis[b.num] = 1;
33                 q.push(b);
34             }
35         }
36     } 
37 }
38 int main()
39 {
40     while(scanf("%d %d", &n, &k) != EOF)
41     {
42         if(n >= k)
43         {  printf("%d
", n-k);  continue;  }
44         printf("%d
", bfs());    
45     }
46     return 0;
47 } 

代码二:

 1 #include<cstdio>
 2 #include<string.h>
 3 #include<iostream>
 4 using namespace std;
 5 int N, K;
 6 int a[100005], vis[100005], step[100005];
 7 int dir[3][2]={{1,1},{1,-1},{2,0}};
 8              // 后     前    二倍 
 9 void BFS(int i)
10 {   
11     int front, rear, x, k;
12     front = 0; rear = 0;
13     a[rear++] = i;
14     vis[i] = 1;
15     step[i] = 0; 
16     while(front < rear)
17     {
18         i = a[front++];
19         for(k=0; k<3; k++)
20         {
21             x = i * dir[k][0] + dir[k][1];
22             if(!vis[x] && x >=0 && x <= 100000)
23             {
24                 if(x == K)
25                 {
26                     step[x] = step[i] + 1;
27                     return;
28                 }        
29                 else
30                 {
31                     vis[x] = 1;
32                     a[rear++] = x;
33                     step[x] = step[i] + 1;
34                 }
35             }
36         }
37     }
38 }
39 int main()
40 {
41     cin >> N >> K;
42     memset(vis, 0, sizeof(vis));
43     memset(step, 0, sizeof(step));
44     memset(a, 0, sizeof(a));
45     BFS(N);
46     cout << step[K] << endl;
47     return 0;
48 }
原文地址:https://www.cnblogs.com/123tang/p/5784601.html