注意vis数组范围

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cmath>

using namespace std;

const int maxn = 2 * 1e5 + 10;
int s, e, vis[maxn];

struct State {
int x, step;
}state, temp;

int bfs() {
memset(vis, 0, sizeof(vis));
int ans = 0;
queue<State> q;
state.x = s, state.step = 0;
vis[s] = 1;
q.push(state);
while(!q.empty()) {
temp = q.front(); q.pop();
if (temp.x == e) return temp.step;
state.step = temp.step + 1;
if (temp.x > 0 && !vis[temp.x - 1]) {
vis[temp.x - 1] = 1;
state.x = temp.x - 1;
q.push(state);
}
if (temp.x < maxn && !vis[temp.x + 1]) {
vis[temp.x + 1] = 1;
state.x = temp.x + 1;
q.push(state);
}
if (temp.x * 2 < maxn && !vis[temp.x * 2]) {
vis[temp.x * 2] = 1;
state.x = temp.x * 2;
q.push(state);
}
}
}

int main()
{
while (scanf("%d %d", &s, &e) == 2) {
printf("%d\n", bfs());
}
return 0;
}