Catch That Cow POJ - 3278

14 篇文章 0 订阅
10 篇文章 0 订阅

超级简单的一维的BFS搜索,1Y,注意一点,就是不用考虑当前人的位置是在牛的前面还是后面,有的时候即便是在后面也可能需要继续向后挪动

//leehaoze
#include <iostream>
#include <deque>
#include <string>
#include <vector>
#include <queue>
#include <cstdio>
#include <stack>
#include <algorithm>
#include <cstring>
#include <cctype>
#include <cstdio>
#include <cmath>
#include <cstdlib>

using namespace std;
const int INF = 1 << 29;
#define INC_SAT(val) (val = ((val)+1 > (val)) ? (val)+1 : (val))
#define ARR_SIZE(a) ( sizeof( (a) ) / sizeof( (a[0]) ) )
#define ULL unsigned long long

#define MAXN 100000 + 5

struct Pos {
    Pos(int x, int step) : x_(x), step_(step) {}

    Pos() : step_(0) {}

    int x_, step_;
};

int N, K;
bool book[MAXN];
int Move[] = {1, -1};

void Input() {
    scanf("%d%d", &N, &K);
    memset(book, false, sizeof book);
}

bool Legal(int x) {
    return x >= 0 && x <= 100000 && !book[x];
}

void BFS() {
    queue<Pos> Q;
    Q.push(Pos(N, 0));
    while (!Q.empty()) {
        Pos now = Q.front();
        Q.pop();
        if (now.x_ == K) {
            cout << now.step_ << endl;
            return;
        }
        int dx = now.x_ * 2;
        if (Legal(dx)) {
            book[dx] = true;
            Q.push(Pos(dx, now.step_ + 1));
        }
        for (int i = 0; i < 2; ++i) {
            dx = now.x_ + Move[i];
            if (Legal(dx)) {
                book[dx] = true;
                Q.push(Pos(dx, now.step_ + 1));
            }
        }
    }
}

int main() {
#ifdef LOCAL
    freopen("IN.txt", "r", stdin);
#endif
    std::ios::sync_with_stdio(false);
    Input();
    BFS();

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值