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

#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using P = pair<ll, ll>;

const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
const ll INF = 1ll << 60;

#define FOR(i, a, b) for (ll i = (a); i < (ll)(b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()
#define DEBUG(x) ;

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

ll H, W;
ll sh, sw;
ll gh, gw;

char field[1010][1010];
ll cnt[1010][1010];
struct grid {
  ll to;
  ll cost;
};

int main(int argc, char **argv) {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> H >> W >> sh >> sw >> gh >> gw;
  --sh, --sw, --gh, --gw;
  REP(h, H) REP(w, W) cin >> field[h][w];
  REP(h, H) REP(w, W) cnt[h][w] = INF;

  cnt[sh][sw] = 0;
  priority_queue<pair<ll, P>, vector<pair<ll, P>>, greater<pair<ll, P>>> que;
  que.push(pair<ll, P>{0, P{sh, sw}});

  while (que.size()) {
    DEBUG(que.size());
    auto now = que.top();
    que.pop();
    ll cn = now.first;
    ll h = now.second.first;
    ll w = now.second.second;

    if (cnt[h][w] < cn)
      continue;

    REP(i, 4) {
      ll hh = h + dy[i];
      ll ww = w + dx[i];
      if (hh < 0 || hh >= H || ww < 0 || ww >= W)
        continue;
      if (field[hh][ww] == '#')
        continue;

      if (cnt[hh][ww] > cn) {
        cnt[hh][ww] = cn;
        que.push(pair<ll, P>{cn, P{hh, ww}});
      }
    }

    for (ll dy = -2; dy <= 2; ++dy) {
      for (ll dx = -2; dx <= 2; ++dx) {
        ll hh = h + dy;
        ll ww = w + dx;

        if (hh < 0 || hh >= H || ww < 0 || ww >= W)
          continue;
        if (field[hh][ww] == '#')
          continue;

        if (cnt[hh][ww] > cn + 1) {
          cnt[hh][ww] = cn + 1;
          que.push(pair<ll, P>{cn + 1, P{hh, ww}});
        }
      }
    }
  }

  if (cnt[gh][gw] == INF)
    std::cout << -1 << std::endl;
  else
    std::cout << cnt[gh][gw] << std::endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using P = pair<ll, ll>;

const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
const ll INF = 1ll << 60;

#define FOR(i, a, b) for (ll i = (a); i < (ll)(b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()
#define DEBUG(x) ;

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

ll H, W;
ll sh, sw;
ll gh, gw;

char field[1010][1010];
ll cnt[1010][1010];

int main(int argc, char **argv) {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> H >> W >> sh >> sw >> gh >> gw;
  --sh, --sw, --gh, --gw;
  REP(h, H) REP(w, W) cin >> field[h][w];
  REP(h, H) REP(w, W) cnt[h][w] = INF;

  cnt[sh][sw] = 0;
  queue<pair<P, ll>> que;
  que.push(pair<P, ll>{P{sh, sw}, 0});

  while (que.size()) {
    DEBUG(que.size());
    auto now = que.front();
    que.pop();
    ll h = now.first.first;
    ll w = now.first.second;
    ll cn = now.second;

    if (cnt[h][w] < cn)
      continue;

    REP(i, 4) {
      ll hh = h + dy[i];
      ll ww = w + dx[i];
      if (hh < 0 || hh >= H || ww < 0 || ww >= W)
        continue;
      if (field[hh][ww] == '#')
        continue;

      if (cnt[hh][ww] > cn) {
        cnt[hh][ww] = cn;
        que.push(pair<P, ll>{P{hh, ww}, cn});
      }
    }

    for (ll dy = -2; dy <= 2; ++dy) {
      for (ll dx = -2; dx <= 2; ++dx) {
        ll hh = h + dy;
        ll ww = w + dx;

        if (hh < 0 || hh >= H || ww < 0 || ww >= W)
          continue;
        if (field[hh][ww] == '#')
          continue;

        if (cnt[hh][ww] > cn + 1) {
          cnt[hh][ww] = cn + 1;
          que.push(pair<P, ll>{P{hh, ww}, cn + 1});
        }
      }
    }
  }

  if (cnt[gh][gw] == INF)
    std::cout << -1 << std::endl;
  else
    std::cout << cnt[gh][gw] << std::endl;

  return 0;
}