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

#include <bits/stdc++.h>

using ll = long long;
using ull = unsigned long long;
using namespace std;
constexpr int inf = 1 << 30;
constexpr int mo = 1e9 + 7;
constexpr ll infl = 1ll << 60;

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

  int h, w;
  cin >> h >> w;
  int ch, cw;
  cin >> ch >> cw;
  ch--;
  cw--;
  int dh, dw;
  cin >> dh >> dw;
  dh--;
  dw--;
  vector<string> f(h);
  for (int i = 0; i < h; ++i)
    cin >> f[i];
  deque<pair<int, int>> q;
  vector<vector<int>> dp(h, vector<int>(w, inf));
  vector<pair<int, int>> d1 = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}};
  q.push_back({ch, cw});
  dp[ch][cw] = 0;
  while (!q.empty()) {
    auto cur = q.front();
    q.pop_front();
    int y = cur.first;
    int x = cur.second;
    if (y == dh and x == dw)
      break;
    for (auto &d : d1) {
      int dy = y + d.first;
      int dx = x + d.second;
      if (dy < 0 or dx < 0 or dy > h - 1 or dx > w - 1 or f[dy][dx] == '#')
        continue;
      if (dp[dy][dx] > dp[y][x]) {
        q.push_front({dy, dx});
        dp[dy][dx] = dp[y][x];
      }
    }
    for (int i = -2; i <= 2; ++i)
      for (int j = -2; j <= 2; ++j) {
        if (i == 0 and j == 0)
          continue;
        if ((i == 1 and j == 0) or (i == 0 and j == 1) or
            (i == -1 and j == 0) or (i == 0 and j == -1))
          continue;
        int dy = y + i;
        int dx = x + j;
        if (dy < 0 or dx < 0 or dy > h - 1 or dx > w - 1 or f[dy][dx] == '#')
          continue;
        if (dp[dy][dx] > dp[y][x] + 1) {
          q.push_back({dy, dx});
          dp[dy][dx] = dp[y][x] + 1;
        }
      }
  }

  if (dp[dh][dw] == inf)
    cout << -1 << endl;
  else
    cout << dp[dh][dw] << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using ll = long long;
using ull = unsigned long long;
using namespace std;
constexpr int inf = 1 << 30;
constexpr int mo = 1e9 + 7;
constexpr ll infl = 1ll << 60;

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

  int h, w;
  cin >> h >> w;
  int ch, cw;
  cin >> ch >> cw;
  ch--;
  cw--;
  int dh, dw;
  cin >> dh >> dw;
  dh--;
  dw--;
  vector<string> f(h);
  for (int i = 0; i < h; ++i)
    cin >> f[i];
  vector<vector<int>> dp(h, vector<int>(w, inf));
  vector<vector<bool>> visi(h, vector<bool>(w));
  using node = pair<int, int>;
  priority_queue<pair<int, node>> q;
  q.push({0, {ch, cw}});
  vector<pair<int, int>> d1 = {{-1, 0}, {0, -1}, {0, 1}, {1, 0}};
  while (!q.empty()) {
    auto cur = q.top();
    q.pop();
    int cost = -cur.first;
    int y = cur.second.first;
    int x = cur.second.second;
    if (dp[y][x] < cost)
      continue;
    dp[y][x] = cost;
    visi[y][x] = true;
    for (auto d : d1) {
      int dy = y + d.first;
      int dx = x + d.second;
      if (dy < 0 or dx < 0 or dy > h - 1 or dx > w - 1 or f[dy][dx] == '#' or
          visi[dy][dx])
        continue;
      q.push({-cost, {dy, dx}});
    }
    for (int i = -2; i <= 2; ++i) {
      for (int j = -2; j <= 2; ++j) {
        if (i == 0 and j == 0)
          continue;
        if ((i == -1 and j == 0) or (i == 0 and j == -1) or
            (i == 1 and j == 0) or (i == 0 and j == -1))
          continue;
        int dy = y + i;
        int dx = x + j;
        if (dy < 0 or dx < 0 or dy > h - 1 or dx > w - 1 or f[dy][dx] == '#' or
            visi[dy][dx])
          continue;
        if (dp[dy][dx] > cost + 1) {
          q.push({-(cost + 1), {dy, dx}});
        }
      }
    }
  }

  if (dp[dh][dw] == inf)
    cout << -1 << endl;
  else
    cout << dp[dh][dw] << endl;
  return 0;
}