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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define rep1(i, n) FOR(i, 1, n + 1)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; --i)
#define whole(x) (x).begin(), (x).end()
#define rwhole(x) (x).rbegin(), (x).rend()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())
#define P pair<int, int>
#define debug(var) cerr << "[" << #var << "] " << var << endl
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define vi vector<int>
#define vl vector<ll>
#define pb push_back
#define eb emplace_back
#define pr(s) cout << (s) << '\\n'
const ll mod = 1000000007;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};
const int INF = 1001001001;

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

  int h, w;
  cin >> h >> w;
  P c, d;
  cin >> c.first >> c.second >> d.first >> d.second;
  c.first--;
  c.second--;
  d.first--;
  d.second--;
  vector<string> s(h);
  rep(i, h) cin >> s[i];
  vector<vector<int>> dist(h, vector<int>(w, INF));
  auto comp = [](pair<P, int> a, pair<P, int> b) {
    return a.second > b.second;
  };
  priority_queue<pair<P, int>, vector<pair<P, int>>, decltype(comp)> q{comp};
  q.emplace(c, 0);
  dist[c.first][c.second] = 0;
  while (!q.empty()) {
    auto now = q.top();
    int x = now.first.first;
    int y = now.first.second;
    int di = now.second;
    q.pop();
    if (di > dist[x][y])
      continue;
    rep(i, 4) {
      int ix = x + dx[i];
      int iy = y + dy[i];
      if (ix < 0 || ix >= h || iy < 0 || iy >= w)
        continue;
      if (s[ix][iy] == '#')
        continue;
      if (di < dist[ix][iy]) {
        dist[ix][iy] = di;
        q.emplace(P(ix, iy), di);
      }
    }
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        if (i == 0 && j == 0)
          continue;
        int ix = x + i;
        int iy = y + j;
        if (ix < 0 || ix >= h || iy < 0 || iy >= w)
          continue;
        if (s[ix][iy] == '#')
          continue;
        if (di + 1 < dist[ix][iy]) {
          dist[ix][iy] = di + 1;
          q.emplace(P(ix, iy), di + 1);
        }
      }
    }
  }
  int ans = dist[d.first][d.second];
  if (ans == INF)
    ans = -1;

  pr(ans);
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define rep1(i, n) FOR(i, 1, n + 1)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; --i)
#define whole(x) (x).begin(), (x).end()
#define rwhole(x) (x).rbegin(), (x).rend()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())
#define P pair<int, int>
#define debug(var) cerr << "[" << #var << "] " << var << endl
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define vi vector<int>
#define vl vector<ll>
#define pb push_back
#define eb emplace_back
#define pr(s) cout << (s) << '\\n'
const ll mod = 1000000007;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};
const int INF = 1001001001;

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

  int h, w;
  cin >> h >> w;
  P c, d;
  cin >> c.first >> c.second >> d.first >> d.second;
  c.first--;
  c.second--;
  d.first--;
  d.second--;
  vector<string> s(h);
  rep(i, h) cin >> s[i];
  vector<vector<int>> dist(h, vector<int>(w, INF));
  queue<pair<P, int>> q;
  q.emplace(c, 0);
  dist[c.first][c.second] = 0;
  while (!q.empty()) {
    auto now = q.front();
    int x = now.first.first;
    int y = now.first.second;
    int di = now.second;
    q.pop();
    if (di > dist[x][y])
      continue;
    rep(i, 4) {
      int ix = x + dx[i];
      int iy = y + dy[i];
      if (ix < 0 || ix >= h || iy < 0 || iy >= w)
        continue;
      if (s[ix][iy] == '#')
        continue;
      if (di < dist[ix][iy]) {
        dist[ix][iy] = di;
        q.emplace(P(ix, iy), di);
      }
    }
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        if (i == 0 && j == 0)
          continue;
        int ix = x + i;
        int iy = y + j;
        if (ix < 0 || ix >= h || iy < 0 || iy >= w)
          continue;
        if (s[ix][iy] == '#')
          continue;
        if (di + 1 < dist[ix][iy]) {
          dist[ix][iy] = di + 1;
          q.emplace(P(ix, iy), di + 1);
        }
      }
    }
  }
  int ans = dist[d.first][d.second];
  if (ans == INF)
    ans = -1;

  pr(ans);
  return 0;
}