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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <vector>

#define SPBR(w, n) std::cout << (w + 1 == n ? '\\n' : ' ');
#define YES cout << "YES" << endl
#define Yes cout << "Yes" << endl
#define NO cout << "NO" << endl
#define No cout << "No" << endl
#define ALL(i) (i).begin(), (i).end()
#define FOR(i, a, n) for (int i = (a); i < (n); ++i)
#define RFOR(i, a, n) for (int i = (n)-1; i >= (a); --i)
#define REP(i, n) for (int i = 0; i < int(n); ++i)
#define RREP(i, n) for (int i = int(n) - 1; i >= 0; --i)
#define IN(a, x, b) (a <= x && x < b)
#define OUT(a, x, b) (x < a || b <= x)
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

#define int ll
using ll = long long;
using ull = unsigned long long;
using ld = long double;
const int MOD = 1000000007;
/* const int MOD = 998244353; */
const int INF = 1e18;
const double PI = acos(-1);

using namespace std;
struct INIT {
  INIT() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
  }
} INIT;

signed main() {
  int H, W;
  cin >> H >> W;

  int sx, sy, gx, gy;
  cin >> sx >> sy >> gx >> gy;

  vector<string> s(H);
  REP(i, H) cin >> s[i];

  sx--;
  sy--;
  gx--;
  gy--;

  using P = pair<int, pair<int, int>>;
  priority_queue<P, vector<P>, greater<P>> q;
  q.push({0, {sx, sy}});

  vector<vector<int>> dist(H, vector<int>(W, INF));
  dist[sx][sy] = 0;

  while (!q.empty()) {
    int x = q.top().second.first;
    int y = q.top().second.second;
    q.pop();
    REP(i, 5) REP(j, 5) {
      if (i == 2 && j == 2)
        continue;
      int nx = x + i - 2;
      int ny = y + j - 2;
      if (OUT(0, nx, H) || OUT(0, ny, W))
        continue;
      if (s[nx][ny] == '#')
        continue;

      int d = 1;
      if (abs(i - 2) + abs(j - 2) <= 1)
        d = 0;
      if (chmin(dist[nx][ny], dist[x][y] + d)) {
        q.push({dist[x][y] + d, {nx, ny}});
      }
    }
  }

  if (dist[gx][gy] == INF)
    dist[gx][gy] = -1;
  cout << dist[gx][gy] << "\\n";

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <vector>

#define SPBR(w, n) std::cout << (w + 1 == n ? '\\n' : ' ');
#define YES cout << "YES" << endl
#define Yes cout << "Yes" << endl
#define NO cout << "NO" << endl
#define No cout << "No" << endl
#define ALL(i) (i).begin(), (i).end()
#define FOR(i, a, n) for (int i = (a); i < (n); ++i)
#define RFOR(i, a, n) for (int i = (n)-1; i >= (a); --i)
#define REP(i, n) for (int i = 0; i < int(n); ++i)
#define RREP(i, n) for (int i = int(n) - 1; i >= 0; --i)
#define IN(a, x, b) (a <= x && x < b)
#define OUT(a, x, b) (x < a || b <= x)
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

#define int ll
using ll = long long;
using ull = unsigned long long;
using ld = long double;
const int MOD = 1000000007;
/* const int MOD = 998244353; */
const int INF = 1e18;
const double PI = acos(-1);

using namespace std;
struct INIT {
  INIT() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
  }
} INIT;

signed main() {
  int H, W;
  cin >> H >> W;

  int sx, sy, gx, gy;
  cin >> sx >> sy >> gx >> gy;

  vector<string> s(H);
  REP(i, H) cin >> s[i];

  sx--;
  sy--;
  gx--;
  gy--;

  queue<pair<int, int>> q;
  q.emplace(sx, sy);

  vector<vector<int>> dist(H, vector<int>(W, INF));
  dist[sx][sy] = 0;

  int dx[] = {0, 1, 0, -1, 1, 1, -1, -1};
  int dy[] = {1, 0, -1, 0, 1, -1, 1, -1};

  while (!q.empty()) {
    auto [x, y] = q.front();
    q.pop();
    REP(i, 5) REP(j, 5) {
      if (i == 2 && j == 2)
        continue;
      int nx = x + i - 2;
      int ny = y + j - 2;
      if (OUT(0, nx, H) || OUT(0, ny, W))
        continue;
      if (s[nx][ny] == '#')
        continue;

      int d = 1;
      if (abs(i - 2) + abs(j - 2) <= 1)
        d = 0;
      if (chmin(dist[nx][ny], dist[x][y] + d)) {
        q.emplace(nx, ny);
      }
    }
  }

  if (dist[gx][gy] == INF)
    dist[gx][gy] = -1;
  cout << dist[gx][gy] << "\\n";

  return 0;
}