POJ - 3278 Catch That Cow(BFS)

来源:互联网 发布:visio软件安装包 编辑:程序博客网 时间:2024/05/22 04:32

题目大意:给你两个坐标,你的位置是第一个坐标
现在有两种操作:操作1:前进一个单位
操作二:前进(当前位置 - 0)个单位
问到达第二个坐标的最小操作数

解题思路:纯BFS裸题

#include <cstdio>#include <cstring>#include <queue>using namespace std;const int N = 200010;struct Node {    int pos, time;    Node() {}    Node(int pos, int time): pos(pos), time(time) {}};bool vis[N];int start, End;void solve() {    queue<Node> Q;    Q.push(Node(start, 0));    memset(vis, 0, sizeof(vis));    vis[start] = true;    while (!Q.empty()) {        int x = Q.front().pos;         int t = Q.front().time;        Q.pop();            if (x == End) {            printf("%d\n", t);            return ;        }        if (x + 1 <= End && !vis[x + 1]) {            vis[x + 1] = true;            Q.push(Node(x + 1, t + 1));        }        if (x - 1 >= 0 && !vis[x - 1]) {            vis[x - 1] = true;            Q.push(Node(x - 1, t + 1));        }        if (x * 2 < N && !vis[x * 2]) {            vis[x * 2] = true;            Q.push(Node(x * 2, t + 1));        }    }}int main() {    while (scanf("%d%d", &start, &End) != EOF) solve();    return 0;}
0 0
原创粉丝点击