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

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

ll solve() {
  ll H, W, Ch, Cw, Dh, Dw;
  cin >> H >> W >> Ch >> Cw >> Dh >> Dw;
  Ch--, Cw--, Dh--, Dw--;
  vector<string> S(H);
  for (int i = 0; i < H; i++)
    cin >> S[i];
  vector<int> dh = {0, 1, 0, -1}, dw = {1, 0, -1, 0};
  ll INF = 1LL << 60;
  vector<vector<ll>> d(H, vector<ll>(W, INF));

  queue<pii> q;
  q.push(pii(Ch, Cw));
  d[Ch][Cw] = 0;
  queue<pii> q2, qt;
  qt.push(pii(Ch, Cw));
  while (!qt.empty()) {
    while (!qt.empty()) {
      q.push(qt.front());
      q2.push(qt.front());
      qt.pop();
    }
    while (!q.empty()) {
      pii v = q.front();
      q.pop();
      int h0 = v.first, w0 = v.second;
      for (int ii = 0; ii < 4; ii++) {
        int h = h0 + dh[ii];
        int w = w0 + dw[ii];
        if (h < 0 || h >= H || w < 0 || w >= W)
          continue;
        if (S[h][w] == '#')
          continue;
        if (d[h][w] > d[h0][w0]) {
          d[h][w] = d[h0][w0];
          q.push(pii(h, w));
          q2.push(pii(h, w));
        }
      }
    }
    while (!q2.empty()) {
      pii v = q2.front();
      q2.pop();
      int h0 = v.first, w0 = v.second;
      for (int i1 = -2; i1 <= 2; i1++) {
        for (int j1 = -2; j1 <= 2; j1++) {
          int h = h0 + i1;
          int w = w0 + j1;
          if (h < 0 || h >= H || w < 0 || w >= W)
            continue;
          if (S[h][w] == '#')
            continue;
          if (d[h][w] > d[h0][w0] + 1) {
            d[h][w] = d[h0][w0] + 1;
            qt.push(pii(h, w));
          }
        }
      }
    }
  }
  ll ans = d[Dh][Dw];
  if (ans >= INF)
    ans = -1;
  return ans;
}

int main() {
  auto ans = solve();
  cout << ans << "\\n";
  return 0;
}

Error: TLE

Fixed submission:

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

ll solve() {
  ll H, W, Ch, Cw, Dh, Dw;
  cin >> H >> W >> Ch >> Cw >> Dh >> Dw;
  Ch--, Cw--, Dh--, Dw--;
  vector<string> S(H);
  for (int i = 0; i < H; i++)
    cin >> S[i];
  vector<int> dh = {0, 1, 0, -1}, dw = {1, 0, -1, 0};
  ll INF = 1LL << 60;
  vector<vector<ll>> d(H, vector<ll>(W, INF));

  auto cmp = [&](pii &v1, pii &v2) {
    return d[v1.first][v1.first] > d[v2.first][v2.first];
  };
  priority_queue<pii, vector<pii>, decltype(cmp)> q(cmp);
  q.push(pii(Ch, Cw));
  d[Ch][Cw] = 0;
  while (!q.empty()) {
    pii v = q.top();
    q.pop();
    int h0 = v.first, w0 = v.second;
    for (int ii = 0; ii < 4; ii++) {
      int h = h0 + dh[ii];
      int w = w0 + dw[ii];
      if (h < 0 || h >= H || w < 0 || w >= W)
        continue;
      if (S[h][w] == '#')
        continue;
      if (d[h][w] > d[h0][w0]) {
        d[h][w] = d[h0][w0];
        q.push(pii(h, w));
      }
    }
    for (int i1 = -2; i1 <= 2; i1++) {
      for (int j1 = -2; j1 <= 2; j1++) {
        int h = h0 + i1;
        int w = w0 + j1;
        if (h < 0 || h >= H || w < 0 || w >= W)
          continue;
        if (S[h][w] == '#')
          continue;
        if (d[h][w] > d[h0][w0] + 1) {
          d[h][w] = d[h0][w0] + 1;
          q.push(pii(h, w));
        }
      }
    }
  }
  ll ans = d[Dh][Dw];
  if (ans == INF)
    ans = -1;
  return ans;
}

int main() {
  auto ans = solve();
  cout << ans << "\\n";
  return 0;
}