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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long int ll;
typedef long double ld;
typedef pair<int, int> P;
typedef pair<int, P> PP;
const ll MOD = 1000000007;
const ll MAX_N = 500010;
const ll INF = 9999999999;
ll dp[1001][1001];
int dx[] = {-1, 0, 0, 1};
int dy[] = {0, 1, -1, 0};

int main() {
  int h, w;
  cin >> h >> w;
  int sh, sw, gh, gw;
  cin >> sh >> sw >> gh >> gw;
  sh--;
  sw--;
  gh--;
  gw--;
  vector<string> G(h);
  for (int i = 0; i < h; i++)
    cin >> G[i];
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      dp[i][j] = INF;
    }
  }
  dp[sh][sw] = 0;
  priority_queue<PP, vector<PP>, greater<PP>> q;
  q.push(PP(0, P(sh, sw)));
  while (!q.empty()) {
    PP pp = q.top();
    q.pop();
    int cost = pp.first;
    int i = pp.second.first;
    int j = pp.second.second;
    if (cost > dp[i][j])
      continue;
    for (int d = 0; d < 4; d++) {
      int ni = i + dy[d];
      int nj = j + dx[d];
      if (ni >= 0 && ni < h && nj >= 0 && nj < w && G[ni][nj] == '.' &&
          dp[ni][nj] > dp[i][j]) {
        q.push(PP(cost, P(ni, nj)));
        dp[ni][nj] = dp[i][j];
      }
    }
    for (int y = -2; y < 3; y++) {
      for (int x = -2; x < 3; x++) {
        int ni = i + y;
        int nj = j + x;
        if (ni >= 0 && ni < h && nj >= 0 && nj < w && G[ni][nj] == '.' &&
            dp[ni][nj] > dp[i][j] + 1) {
          q.push(PP(cost + 1, P(ni, nj)));
          dp[ni][nj] = dp[i][j] + 1;
        }
      }
    }
  }
  if (dp[gh][gw] == INF)
    dp[gh][gw] = -1;
  cout << dp[gh][gw] << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long int ll;
typedef long double ld;
typedef pair<int, int> P;
typedef pair<int, P> PP;
const ll MOD = 1000000007;
const ll MAX_N = 500010;
const ll INF = 9999999999;
ll dp[1001][1001];
int dx[] = {-1, 0, 0, 1};
int dy[] = {0, 1, -1, 0};

int main() {
  int h, w;
  cin >> h >> w;
  int sh, sw, gh, gw;
  cin >> sh >> sw >> gh >> gw;
  sh--;
  sw--;
  gh--;
  gw--;
  vector<string> G(h);
  for (int i = 0; i < h; i++)
    cin >> G[i];
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      dp[i][j] = INF;
    }
  }
  dp[sh][sw] = 0;
  priority_queue<PP> q;
  q.push(PP(0, P(sh, sw)));
  while (!q.empty()) {
    PP pp = q.top();
    q.pop();
    int cost = pp.first;
    int i = pp.second.first;
    int j = pp.second.second;
    if (cost > dp[i][j])
      continue;
    for (int d = 0; d < 4; d++) {
      int ni = i + dy[d];
      int nj = j + dx[d];
      if (ni >= 0 && ni < h && nj >= 0 && nj < w && G[ni][nj] == '.' &&
          dp[ni][nj] > dp[i][j]) {
        q.push(PP(cost, P(ni, nj)));
        dp[ni][nj] = dp[i][j];
      }
    }
    for (int y = -2; y < 3; y++) {
      for (int x = -2; x < 3; x++) {
        int ni = i + y;
        int nj = j + x;
        if (ni >= 0 && ni < h && nj >= 0 && nj < w && G[ni][nj] == '.' &&
            dp[ni][nj] > dp[i][j] + 1) {
          q.push(PP(cost + 1, P(ni, nj)));
          dp[ni][nj] = dp[i][j] + 1;
        }
      }
    }
  }
  if (dp[gh][gw] == INF)
    dp[gh][gw] = -1;
  cout << dp[gh][gw] << endl;
}