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

#include "bits/stdc++.h"
#include <random>
using namespace std;
typedef unsigned long long _ulong;
typedef long long int lint;
typedef pair<lint, lint> plint;
typedef pair<double long, double long> pld;
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((lint)(x).size())
#define FOR(i, begin, end)                                                     \\
  for (lint i = (begin), i##_end_ = (end); i < i##_end_; i++)
#define IFOR(i, begin, end)                                                    \\
  for (lint i = (end)-1, i##_begin_ = (begin); i >= i##_begin_; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)
#define endk '\\n'
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
const lint MOD = 1e9 + 7, INF = 1e18;
lint dx[8] = {0, -1, 1, 0, 1, -1, 1, -1}, dy[8] = {1, 0, 0, -1, -1, -1, 1, 1};

lint H, W, dp[1000][1000], sy, sx, gy, gx;
char mp[1000][1000];

typedef pair<lint, plint> tlint;
int main() {
  cin >> H >> W;
  cin >> sy >> sx;
  sy--;
  sx--;
  cin >> gy >> gx;
  gy--;
  gx--;
  REP(i, H) {
    REP(j, W) {
      cin >> mp[i][j];
      dp[i][j] = INF;
    }
  }

  priority_queue<tlint> que;
  dp[sy][sx] = 0;
  que.push({-dp[sy][sx], {sy, sx}});
  while (!que.empty()) {
    plint curr = que.top().second;
    que.pop();
    REP(k, 4) {
      lint ny = curr.first + dy[k], nx = curr.second + dx[k];
      if (ny < 0 || ny >= H || nx < 0 || nx >= W)
        continue;
      if (mp[ny][nx] == '#')
        continue;
      if (dp[ny][nx] > dp[curr.first][curr.second]) {
        dp[ny][nx] = dp[curr.first][curr.second];
        que.push({-dp[ny][nx], {ny, nx}});
      }
    }
    REP(i, 5) {
      REP(j, 5) {
        lint ny = curr.first - 2 + i, nx = curr.second - 2 + j;
        if (ny < 0 || ny >= H || nx < 0 || nx >= W)
          continue;
        if (mp[ny][nx] == '#')
          continue;
        if (dp[ny][nx] > dp[curr.first][curr.second] + 1) {
          dp[ny][nx] = dp[curr.first][curr.second] + 1;
          que.push({-dp[ny][nx], {ny, nx}});
        }
      }
    }
  }

  if (dp[gy][gx] == INF)
    cout << -1 << endk;
  else
    cout << dp[gy][gx] << endk;
}


Error: TLE

Fixed submission:

#include "bits/stdc++.h"
#include <random>
using namespace std;
typedef unsigned long long _ulong;
typedef long long int lint;
typedef pair<lint, lint> plint;
typedef pair<double long, double long> pld;
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((lint)(x).size())
#define FOR(i, begin, end)                                                     \\
  for (lint i = (begin), i##_end_ = (end); i < i##_end_; i++)
#define IFOR(i, begin, end)                                                    \\
  for (lint i = (end)-1, i##_begin_ = (begin); i >= i##_begin_; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)
#define endk '\\n'
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
const lint MOD = 1e9 + 7, INF = 1e18;
lint dx[8] = {0, -1, 1, 0, 1, -1, 1, -1}, dy[8] = {1, 0, 0, -1, -1, -1, 1, 1};

lint H, W, dp[1000][1000], sy, sx, gy, gx;
char mp[1000][1000];
int main() {
  cin >> H >> W;
  cin >> sy >> sx;
  sy--;
  sx--;
  cin >> gy >> gx;
  gy--;
  gx--;
  REP(i, H) {
    REP(j, W) {
      cin >> mp[i][j];
      dp[i][j] = INF;
    }
  }

  queue<plint> que;
  que.push({sy, sx});
  dp[sy][sx] = 0;
  while (!que.empty()) {
    plint curr = que.front();
    que.pop();
    REP(k, 4) {
      lint ny = curr.first + dy[k], nx = curr.second + dx[k];
      if (ny < 0 || ny >= H || nx < 0 || nx >= W)
        continue;
      if (mp[ny][nx] == '#')
        continue;
      if (dp[ny][nx] > dp[curr.first][curr.second]) {
        dp[ny][nx] = dp[curr.first][curr.second];
        que.push({ny, nx});
      }
    }
    REP(i, 5) {
      REP(j, 5) {
        lint ny = curr.first - 2 + i, nx = curr.second - 2 + j;
        if (ny < 0 || ny >= H || nx < 0 || nx >= W)
          continue;
        if (mp[ny][nx] == '#')
          continue;
        if (dp[ny][nx] > dp[curr.first][curr.second] + 1) {
          dp[ny][nx] = dp[curr.first][curr.second] + 1;
          que.push({ny, nx});
        }
      }
    }
  }

  if (dp[gy][gx] == INF)
    cout << -1 << endk;
  else
    cout << dp[gy][gx] << endk;
}