完全不知道哪错了
查看原帖
完全不知道哪错了
814343
bc2_cryeggy楼主2023/7/19 09:48

对着题解改了好久,还是不对,WA 了 44 次。

代码:

#include <bits/stdc++.h>

using namespace std;

#define int long long

#define qwq ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)

const int N = 1e5 + 10;

int dis[N];

queue<int> q;

int bfs(int xx, int y) {
  while (q.size())
    q.pop();
  memset(dis, 0x3f, sizeof dis);
  dis[xx] = 0;
  q.push(xx);
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    if (x == y)
      return dis[y];
    if (x + 1 <= 1e5 && dis[x + 1] == dis[0]) {
      dis[x + 1] = dis[x] + 1;
      q.push(x + 1);
    }
    else if (x - 1 > 0 && dis[x - 1] == dis[0]) {
      dis[x - 1] = dis[x] + 1;
      q.push(x - 1);
    }
    else if (x * 2 <= 1e5 && dis[2 * x] == dis[0]) {
      dis[2 * x] = dis[x] + 1;
      q.push(2 * x);
    }
  }
}

signed main() {
  qwq;
  int T;
  cin >> T;
  while (T --) {
    int x, y;
    cin >> x >> y;
    cout << bfs(x, y) << endl;
  }
  return 0;
}
2023/7/19 09:48
加载中...