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

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

vector<int> Vx = {1, 0, -1, 0}, Vy = {0, 1, 0, -1};
int inf = 1001001001;

int main() {
  int H, W, Ch, Cw, Dh, Dw;
  cin >> H >> W >> Ch >> Cw >> Dh >> Dw;
  Ch--;
  Cw--;
  Dh--;
  Dw--;
  vector<vector<int>> S(H, vector<int>(W, inf));
  for (int i = 0; i < H; i++) {
    string s;
    cin >> s;
    for (int j = 0; j < W; j++) {
      if (s[j] == '#')
        S[i][j] = -1;
    }
  }

  priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>,
                 greater<pair<int, pair<int, int>>>>
      Q;
  Q.push(make_pair(0, make_pair(Ch, Cw)));
  S[Ch][Cw] = 0;
  while (!Q.empty()) {
    pair<int, pair<int, int>> P = Q.top();
    Q.pop();
    int x = P.second.first, y = P.second.second;
    if (S[x][y] != P.first)
      continue;

    for (int k = 0; k < 4; k++) {
      int x2 = x + Vx[k], y2 = y + Vy[k];
      if (x2 < 0 || x2 >= H)
        continue;
      if (y2 < 0 || y2 >= W)
        continue;
      if (S[x2][y2] == -1)
        continue;
      if (S[x2][y2] > S[x][y]) {
        S[x2][y2] = S[x][y];
        Q.push(make_pair(S[x2][y2], make_pair(x2, y2)));
      }
    }
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        int x2 = x + i, y2 = y + j;
        if (x2 < 0 || x2 >= H)
          continue;
        if (y2 < 0 || y2 >= W)
          continue;
        if (S[x2][y2] == -1)
          continue;
        if (S[x2][y2] > S[x][y] + 1) {
          S[x2][y2] = S[x][y] + 1;
          Q.push(make_pair(S[x2][y2], make_pair(x2, y2)));
        }
      }
    }
  }

  if (S[Dh][Dw] == inf)
    cout << -1 << endl;
  else
    cout << S[Dh][Dw] << endl;
  return 0;
}


Error: TLE

Fixed submission:

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

vector<int> Vx = {1, 0, -1, 0}, Vy = {0, 1, 0, -1};
int inf = 1001001001;

int main() {
  int H, W, Ch, Cw, Dh, Dw;
  cin >> H >> W >> Ch >> Cw >> Dh >> Dw;
  Ch--;
  Cw--;
  Dh--;
  Dw--;
  vector<vector<int>> S(H, vector<int>(W, inf));
  for (int i = 0; i < H; i++) {
    string s;
    cin >> s;
    for (int j = 0; j < W; j++) {
      if (s[j] == '#')
        S[i][j] = -1;
    }
  }

  queue<pair<pair<int, int>, int>> Q;
  Q.push(make_pair(make_pair(Ch, Cw), 0));
  S[Ch][Cw] = 0;
  while (!Q.empty()) {
    pair<pair<int, int>, int> P = Q.front();
    Q.pop();
    int x = P.first.first, y = P.first.second;
    if (S[x][y] != P.second)
      continue;

    for (int k = 0; k < 4; k++) {
      int x2 = x + Vx[k], y2 = y + Vy[k];
      if (x2 < 0 || x2 >= H)
        continue;
      if (y2 < 0 || y2 >= W)
        continue;
      if (S[x2][y2] == -1)
        continue;
      if (S[x2][y2] > S[x][y]) {
        S[x2][y2] = S[x][y];
        Q.push(make_pair(make_pair(x2, y2), S[x2][y2]));
      }
    }
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        int x2 = x + i, y2 = y + j;
        if (x2 < 0 || x2 >= H)
          continue;
        if (y2 < 0 || y2 >= W)
          continue;
        if (S[x2][y2] == -1)
          continue;
        if (S[x2][y2] > S[x][y] + 1) {
          S[x2][y2] = S[x][y] + 1;
          Q.push(make_pair(make_pair(x2, y2), S[x2][y2]));
        }
      }
    }
  }

  if (S[Dh][Dw] == inf)
    cout << -1 << endl;
  else
    cout << S[Dh][Dw] << endl;
  return 0;
}