poj3278 Catch That Cow

这么简单的暴搜问题也能wa爆心态,我果然还是一如既往的菜。

/**
* poj3278 Catch That Cow
* bfs
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;

const int N = 2e5+5;
bool book[N];

struct node{int x, step;};
int bfs(int n, int k)
{
    memset(book, 0, sizeof book);
    queue<node> Q;
    Q.push(node{n, 0});
    book[n] = 1;  // 之前写成book[0] = 1, wa到怀疑人生
    while (!Q.empty()) {
        node t = Q.front(); Q.pop();
        if (t.x == k) return t.step;
        if (t.x-1 >= 0 && !book[t.x-1]) 
            book[t.x-1] = 1, Q.push(node{t.x-1, t.step+1});
        if (t.x+1 < N && !book[t.x+1]) 
            book[t.x+1] = 1, Q.push(node{t.x+1, t.step+1});
        if (t.x*2 < N && !book[t.x*2]) 
            book[t.x*2] = 1, Q.push(node{t.x*2, t.step+1});
    }
    return -1;
}

int main()
{
    int n, k;
    scanf("%d%d", &n, &k);
    printf("%d\n", bfs(n, k));
    return 0;
}


posted @ 2021-01-22 12:47  Zewbie  阅读(57)  评论(0)    收藏  举报