BFS/poj 3278 Catch That Cow

 1 #include<cstdio>
 2 #include<queue>
 3 #include<cstring>
 4 using namespace std;
 5 const int MAXN=200010;
 6 int n,k;
 7 bool v[MAXN];
 8 struct point
 9 {
10     int x;
11     int step;
12 };
13 int bfs()
14 {
15     memset(v,0,sizeof(v));
16     queue<point>que;
17     point now,next;
18     now.x=n;
19     now.step=0;
20     v[n]=1;
21     que.push(now);
22
23     while (!que.empty())
24     {
25         now=que.front();
26         que.pop();
27         if (now.x==k) break;
28
29         next.x=now.x+1;
30         if (next.x>=0 && next.x<=MAXN && !v[next.x])
31         {
32             v[next.x]=1;
33             next.step=now.step+1;
34             que.push(next);
35         }
36
37         next.x=now.x-1;
38         if (next.x>=0 && next.x<=MAXN && !v[next.x])
39         {
40             v[next.x]=1;
41             next.step=now.step+1;
42             que.push(next);
43         }
44
45         next.x=now.x*2;
46         if (next.x>=0 && next.x<=MAXN && !v[next.x])
47         {
48             v[next.x]=1;
49             next.step=now.step+1;
50             que.push(next);
51         }
52
53     }
54     return now.step;
55 }
56
57 int main()
58 {
59     while (scanf("%d%d",&n,&k)!=EOF)
60     {
61         printf("%d\n",bfs());
62     }
63     return 0;
64 }
时间: 2024-08-01 21:50:44

BFS/poj 3278 Catch That Cow的相关文章

POJ 3278 Catch That Cow --- 简单BFS

/* POJ 3278 Catch That Cow --- 简单BFS */ #include <cstdio> #include <queue> #include <cstring> using namespace std; const int maxn = 100005; bool visit[maxn]; int step[maxn]; int bfs(int n, int k){ if (n == k) return 0; memset(visit, 0, s

POJ 3278 Catch That Cow(BFS 剪枝)

题目链接:http://poj.org/problem?id=3278 这几次都是每天的第一道题都挺顺利,然后第二道题一卡一天. = =,今天的这道题7点40就出来了,不知道第二道题在下午7点能不能出来.0 0 先说说这道题目,大意是有个农夫要抓牛,已知牛的坐标,和农夫位置.而且农夫有三种移动方式,X + 1,X - 1,X * 2,问最少几步抓到牛. 开始认为很简单的,三方向的BFS就能顺利解决,然后在忘开标记的情况下直接广搜,果然TLE,在你计算出最少位置之前,牛早跑了. 然后反应过来开标记

poj 3278 Catch That Cow(广搜)

Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 45087   Accepted: 14116 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(简单一维广搜)

Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 45648   Accepted: 14310 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: 88732   Accepted: 27795 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: 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

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

poj 3278 Catch That Cow (bfs搜索)

Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 46715   Accepted: 14673 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