社区讨论
bfs 90分求大佬调
B3626跳跃机器人参与者 4已保存回复 3
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 3 条
- 当前快照
- 1 份
- 快照标识符
- @mm93fhdz
- 此快照首次捕获于
- 2026/03/02 19:26 上周
- 此快照最后确认于
- 2026/03/05 20:50 5 天前
CPP
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
#define x first
#define y second
const int N = 1e6 + 10;
int dist[2 * N];
int a;
int bfs (int x) {
queue<PII> q;
q.push({0, x});
dist[x] = 0;
while (q.size()) {
auto t = q.front();
q.pop();
int step = t.x, shu = t.y;
if (shu - 1 >= 0 && dist[shu - 1] == 0x3f3f3f3f) {
dist[shu - 1] = step + 1;
q.push({step + 1, shu - 1});
}
if (shu + 1 <= N && dist[shu + 1] == 0x3f3f3f3f) {
dist[shu + 1] = step + 1;
q.push({step + 1, shu + 1});
}
if (shu * 2 <= N && dist[shu * 2] == 0x3f3f3f3f) {
dist[shu * 2] = step + 1;
q.push({step + 1, shu * 2});
}
}
return dist[a];
}
int main () {
ios::sync_with_stdio(false);
cin.tie(nullptr);
memset(dist, 0x3f3f3f3f, sizeof dist);
cin >> a;
cout << bfs(1);
return 0;
}
回复
共 3 条回复,欢迎继续交流。
正在加载回复...