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

#include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
using P = pair<int, pair<int, int>>;

const int H_MAX = 1005;
const int W_MAX = 1005;
const int INF = 1000005;

char S[H_MAX][W_MAX];
int root[H_MAX][W_MAX];

int main() {
  int h, w;
  int ch, dh, cw, dw;
  cin >> h >> w;
  cin >> ch >> cw >> dh >> dw;
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++)
      cin >> S[i][j];
  }

  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++)
      root[i][j] = INF;
  }
  root[ch][cw] = 0;
  priority_queue<P> Q;
  Q.push(make_pair(0, make_pair(ch, cw)));
  vector<int> zh{1, -1, 0, 0};
  vector<int> zw{0, 0, 1, -1};
  while (!Q.empty()) {
    int nowh = Q.top().second.first;
    int noww = Q.top().second.second;
    int num = -1 * Q.top().first;
    Q.pop();
    if (root[nowh][noww] < num)
      continue;
    for (int i = 0; i < 4; i++) {
      int newh = nowh + zh[i];
      int neww = noww + zw[i];
      if (newh < 1 || newh > h || neww < 1 || neww > w)
        continue;
      if (S[newh][neww] == '.' && root[newh][neww] > num) {
        root[newh][neww] = num;
        Q.push(make_pair(-num, make_pair(newh, neww)));
      }
    }
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        int newh = nowh + i;
        int neww = noww + j;
        if (newh < 1 || newh > h || neww < 1 || neww > w)
          continue;
        if (S[newh][neww] == '.' && root[newh][neww] > num + 1) {
          root[newh][neww] = num + 1;
          Q.push(make_pair(-(num + 1), make_pair(newh, neww)));
        }
      }
    }
  }
  if (root[dh][dw] == INF)
    cout << -1 << endl;
  else
    cout << root[dh][dw] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
using P = pair<int, pair<int, int>>;

const int H_MAX = 1005;
const int W_MAX = 1005;
const int INF = 1000005;

char S[H_MAX][W_MAX];
int root[H_MAX][W_MAX];

int main() {
  int h, w;
  int ch, dh, cw, dw;
  cin >> h >> w;
  cin >> ch >> cw >> dh >> dw;
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++)
      cin >> S[i][j];
  }

  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++)
      root[i][j] = INF;
  }
  root[ch][cw] = 0;
  priority_queue<P> Q;
  Q.push(make_pair(0, make_pair(ch, cw)));
  vector<int> zh{1, -1, 0, 0};
  vector<int> zw{0, 0, 1, -1};
  while (!Q.empty()) {
    int nowh = Q.top().second.first;
    int noww = Q.top().second.second;
    int num = Q.top().first;
    Q.pop();
    if (root[nowh][noww] < num)
      continue;
    for (int i = 0; i < 4; i++) {
      int newh = nowh + zh[i];
      int neww = noww + zw[i];
      if (newh < 1 || newh > h || neww < 1 || neww > w)
        continue;
      if (S[newh][neww] == '.' && root[newh][neww] > num) {
        root[newh][neww] = num;
        Q.push(make_pair(num, make_pair(newh, neww)));
      }
    }
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        int newh = nowh + i;
        int neww = noww + j;
        if (newh < 1 || newh > h || neww < 1 || neww > w)
          continue;
        if (S[newh][neww] == '.' && root[newh][neww] > num + 1) {
          root[newh][neww] = num + 1;
          Q.push(make_pair(num + 1, make_pair(newh, neww)));
        }
      }
    }
  }
  if (root[dh][dw] == INF)
    cout << -1 << endl;
  else
    cout << root[dh][dw] << endl;
  return 0;
}