[USACO07OPEN]Catch That Cow

题目:洛谷P2685、HDU2717

题目大意:有一个人在点$n$,一头牛在点$k$,人每秒能从$x$移动到点$x+1$、$x-1$、$2x$,牛不会动,求最少多少秒后人能移动到牛所在的$k$。

思路:BFS。按照题意进行广搜。

注意:题目数据较大,如中途计算中的点$x$大于100000或小于1,则不放入队列中。

细节见代码。

C++ Code:

 1 #include<cstdio>
 2 #include<queue>
 3 #include<cstring>
 4 using namespace std;
 5 bool b[100051];
 6 int main(){
 7     int n,k;
 8     while(scanf("%d%d",&n,&k)!=EOF){
 9         if(n>=k){//特判n≥k的情况
10             printf("%d\n",n-k);continue;
11         }
12         queue<int>q1,q2;
13         q1.push(n);
14         q2.push(0);
15         memset(b,1,sizeof(b));
16         b[n]=0;
17         while(!q1.empty()){
18             int p=q1.front();q1.pop();
19             int P=q2.front();q2.pop();
20             int l=p-1;
21             if(l&&b[l]){
22                 if(l==k){
23                     printf("%d\n",P+1);break;
24                 }
25                 b[l]=0;
26                 q1.push(l);
27                 q2.push(P+1);
28             }
29             l=p+1;
30             if(l<=100000&&b[l]){
31                 if(l==k){
32                     printf("%d\n",P+1);break;
33                 }
34                 b[l]=0;
35                 q1.push(l);
36                 q2.push(P+1);
37             }
38             l=p*2;
39             if(l<=100000&&b[l]){
40                 if(l==k){
41                     printf("%d\n",P+1);break;
42                 }
43                 b[l]=0;
44                 q1.push(l);
45                 q2.push(P+1);
46             }
47         }
48     }
49     return 0;
50 }
时间: 2024-11-08 18:23:23

[USACO07OPEN]Catch That Cow的相关文章

bfs hdu 2717 Catch That Cow

Catch That Cow Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 14553    Accepted Submission(s): 4422 Problem Description Farmer John has been informed of the location of a fugitive cow and wants

hdu 2717 Catch That Cow

---恢复内容开始--- Catch That Cow Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 14433    Accepted Submission(s): 4396 Problem Description Farmer John has been informed of the location of a fugitive

POJ 3278 Catch That Cow(bfs)

传送门 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 80273   Accepted: 25290 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 ≤ 10

Catch That Cow

Catch That Cow Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Total Submission(s) : 113   Accepted Submission(s) : 46 Problem Description Farmer John has been informed of the location of a fugitive cow and wants to

POJ - 3278 - Catch That Cow (BFS)

题目传送:Catch That Cow 思路:BFS找最小步数,用一个结构体存下当前结点的数值以及当前步数 AC代码: #include <cstdio> #include <cstring> #include <string> #include <cstdlib> #include <iostream> #include <algorithm> #include <cmath> #include <queue>

POJ 题目Catch That Cow(BFS)

Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 52537   Accepted: 16471 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,00

poj 3278 Catch That Cow 【bfs】

Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 52335   Accepted: 16416 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,00

hdu 2717 Catch That Cow(广搜bfs)

题目链接:http://i.cnblogs.com/EditPosts.aspx?opt=1 Catch That Cow Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 7909    Accepted Submission(s): 2498 Problem Description Farmer John has been inform

POJ 3278 Catch That Cow

POJ: https://i.cnblogs.com/EditPosts.aspx?opt=1 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 80291   Accepted: 25297 Description Farmer John has been informed of the location of a fugitive cow and wants to catch her imm