Problem p02579 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int H, W;
  cin >> H >> W;
  int sx, sy;
  cin >> sx >> sy;
  sx--;
  sy--;
  int gx, gy;
  cin >> gx >> gy;
  gx--;
  gy--;
  vector<string> mp(H);
  for (int i = 0; i < H; i++)
    cin >> mp[i];

  auto dp = vector<vector<int>>(H, vector<int>(W, (1 << 30)));
  dp[sx][sy] = 0;

  queue<P> q;
  set<P> mq;
  q.emplace(sx, sy);
  while (!q.empty() || !mq.empty()) {
    if (q.empty()) {
      for (auto p : mq)
        q.push(p);
      mq.clear();
    }
    P cp = q.front();
    // cout << cp.first << ", " << cp.second << '\\n';
    q.pop();
    for (int i = cp.first - 2; i < cp.first + 3; i++) {
      for (int j = cp.second - 2; j < cp.second + 3; j++) {
        if (i < 0 || j < 0 || i >= H || j >= W || mp[i][j] == '#')
          continue;
        if (i == cp.first && j == cp.second)
          continue;
        bool is_magic = (llabs(i - cp.first) + llabs(j - cp.second) == 1);
        // cout << i << "/" << j << '/' << boolalpha << is_magic << " dp[i][j]:"
        // << dp[i][j] << ", " << dp[cp.first][cp.second] << '\\n';
        if (chmin(dp[i][j], dp[cp.first][cp.second] + (is_magic ? 0 : 1))) {
          if (is_magic)
            q.emplace(i, j);
          else
            mq.emplace(i, j);
        }
      }
    }
  }
  // for(auto v : dp)
  // {
  //     for(auto num : v)
  //     {
  //         cout << num << ' ';
  //     }
  //     cout << '\\n';
  // }

  if (dp[gx][gy] == (1 << 30))
    cout << -1 << endl;
  else
    cout << dp[gx][gy] << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int H, W;
  cin >> H >> W;
  int sx, sy;
  cin >> sx >> sy;
  sx--;
  sy--;
  int gx, gy;
  cin >> gx >> gy;
  gx--;
  gy--;
  vector<string> mp(H);
  for (int i = 0; i < H; i++)
    cin >> mp[i];

  auto dp = vector<vector<int>>(H, vector<int>(W, (1 << 30)));
  dp[sx][sy] = 0;

  queue<P> q;
  q.emplace(sx, sy);
  while (!q.empty()) {
    P cp = q.front();
    // cout << cp.first << ", " << cp.second << '\\n';
    q.pop();
    for (int i = cp.first - 2; i < cp.first + 3; i++) {
      for (int j = cp.second - 2; j < cp.second + 3; j++) {
        if (i < 0 || j < 0 || i >= H || j >= W || mp[i][j] == '#')
          continue;
        if (i == cp.first && j == cp.second)
          continue;
        bool is_magic = (abs(i - cp.first) + abs(j - cp.second) == 1);
        // cout << i << "/" << j << '/' << boolalpha << is_magic << " dp[i][j]:"
        // << dp[i][j] << ", " << dp[cp.first][cp.second] << '\\n';
        if (chmin(dp[i][j], dp[cp.first][cp.second] + (is_magic ? 0 : 1))) {
          q.emplace(i, j);
        }
      }
    }
  }
  // for(auto v : dp)
  // {
  //     for(auto num : v)
  //     {
  //         cout << num << ' ';
  //     }
  //     cout << '\\n';
  // }

  if (dp[gx][gy] == (1 << 30))
    cout << -1 << endl;
  else
    cout << dp[gx][gy] << endl;

  return 0;
}