POJ 3248 Catch That Cow

http://poj.org/problem?id=3278

二维BFS

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <string.h>
 4 #include <queue>
 5 #define READ() freopen("in.txt", "r", stdin);
 6
 7 using namespace std;
 8
 9 struct Loc
10 {
11     int x, step;
12     Loc () {}
13     Loc(int x, int step) : x(x), step(step) {}
14 };
15
16 int N, K;
17 bool use[100007];
18 bool OK(int x)
19 {
20     if (x < 0 || x > 100000 ) return false;
21     else return true;
22 }
23 int bfs()
24 {
25     queue<Loc> que;
26     que.push(Loc(N, 0));
27     while (!que.empty())
28     {
29         Loc crt = que.front();
30         que.pop();
31         if(use[crt.x]) continue;
32         use[crt.x] = true;
33         int nx = crt.x - 1;
34         if (OK(nx) && !use[nx])
35         {
36             if (nx == K) return crt.step+1;
37             else que.push(Loc(nx, crt.step+1));
38         }
39         nx = crt.x + 1;
40         if (OK(nx))
41         {
42             if (nx == K && !use[nx]) return crt.step+1;
43             else que.push(Loc(nx, crt.step+1));
44         }
45         nx = 2*crt.x;
46         if (OK(nx) && !use[nx])
47         {
48             if (nx == K) return crt.step+1;
49             else que.push(Loc(nx, crt.step+1));
50         }
51     }
52 }
53 int main()
54 {
55     //READ()
56     scanf("%d%d", &N, &K);
57     if (N == K) //有N == K的坑点 严谨一点
58     {
59         cout << 0 << endl;
60         return 0;
61     }
62     memset(use, 0, sizeof(use));
63     int ans = bfs();
64     cout << ans << endl;
65     return 0;
66 }
时间: 2025-01-04 06:28:27

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

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

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