题目大意:给你两个坐标,你的位置是第一个坐标
现在有两种操作:操作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;
}