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, sizeof visit);
    queue<int> q; //对中存储已访问但下一个点待判断的结点
    q.push(n);
    step[n] = 0;
    visit[n] = 1;
    int x,next;

    while (!q.empty()){
        x = q.front(); q.pop();

        for (int i = 0; i < 3; ++i){
            //i = 0,1,2表示分别按不同的方向广搜
            if (i == 0)
                next = x - 1;
            else if (i == 1)
                next = x + 1;
            else
                next = x * 2;

            //是否越界和是否已访问的判断
            if (visit[next] || next < 0 ||next >= maxn)
                continue;
            visit[next] = 1;
            step[next] = step[x] + 1; //步数+1
            if (next == k)
                return step[next];
            q.push(next);
        }
        //q.pop();
    }
    return -1;
}

int main()
{
    int n, k;
    while (scanf("%d%d", &n, &k) == 2){
        printf("%d\n", bfs(n, k));
    }

    return 0;
}

时间: 2024-10-24 05:30:14

POJ 3278 Catch That Cow --- 简单BFS的相关文章

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 【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: 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 简单搜索

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 m

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(广搜)

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