poj3278Catch That Cow(bfs)

题目

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 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


思路:

poj3278Catch That Cow(bfs)_第1张图片


代码

 1 #include<iostream>
 2 #include<fstream>
 3 #include<string>
 4 #include<algorithm>
 5 #include<math.h>
 6 #include<stack>
 7 #include<queue>
 8 #include<set>
 9 #include<map>
10 #include<vector>
11 #include<stdio.h>
12 #include<stdlib.h>
13 #include<cstdio>
14 #include<cstdlib>
15 #include<cstring>
16 #include<cmath>
17 
18 using namespace std;
19 
20 typedef long long LL;
21 typedef long double real;
22 typedef vector<int> VI;
23 
24 
25 /**************************************************************
26     Problem: poj3278Catch That Cow
27     Ordering: bfs
28     Thought: 针对三个可行的步骤分三方向搜索,记录步数
29     Result: Accepted
30 ****************************************************************/
31 
32 
33 #define INF 0x3f3f3f3f
34 #define MINN -0x3f3f3f3f
35 #define MAXN 0x3f3f3f3f
36 #define MOD 10007
37 #define NUM 1000002
38 
39 int n,k;
40 int step[NUM];
41 bool vis[NUM];
42 int head,next;
43 queue<int> q;
44 
45 int bfs()
46 {
47     memset(step,0,sizeof(step));
48     memset(vis,false,sizeof(vis));
49     while(!q.empty()) q.pop();
50 
51     head=n;
52     q.push(head);
53     step[head]=0;
54     vis[head]=true;
55     while(!q.empty())
56     {
57         head=q.front();q.pop();
58         for(int i=0;i<3;i++)
59         {
60             if(i==0) next=head-1;
61             else if(i==1) next=head+1;
62             else next=head*2;
63             if(next<0 || next>=NUM) continue;
64             if(!vis[next])
65             {
66                 q.push(next);
67                 vis[next]=true;
68                 step[next]=step[head]+1;
69             }
70             if(next==k) return step[next];
71         }
72     }
73 }
74 
75 int main()
76 {
77     //freopen("poj.in","r",stdin);
78     //freopen("out.out","w",stdout);
79 
80     while(scanf("%d%d",&n,&k)!=EOF)
81     {
82         if(n>k) printf("%d\n",n-k);
83         else printf("%d\n",bfs());
84     }
85 
86     return 0;
87 }
View Code

 

版权声明:本文为博主原创文章,未经博主允许不得转载。

你可能感兴趣的:(catch)