POJ - 3278 Catch That Cow(BFS)
发布日期:2021-10-03 15:44:50 浏览次数:2 分类:技术文章

本文共 1060 字,大约阅读时间需要 3 分钟。

题目大意:给你两个坐标,你的位置是第一个坐标

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

解题思路:纯BFS裸题

#include 
#include
#include
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
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;}

转载地址:https://blog.csdn.net/L123012013048/article/details/49019859 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:POJ - 3279 Fliptile(DFS)
下一篇:POJ - 2251 Dungeon Master(BFS)

发表评论

最新留言

能坚持,总会有不一样的收获!
[***.219.124.196]2024年04月14日 18时36分26秒