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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define DEBUG 1
using namespace std;
constexpr int kMod = 1000000007;
typedef long long LL;
constexpr LL kInf = 1e18;
typedef pair<int, int> P;

int H, W;
int Ch, Cw;
int Dh, Dw;
vector<string> S;
vector<vector<LL>> C;

int main() {
  cin >> H >> W;
  cin >> Ch >> Cw;
  --Ch, --Cw;
  cin >> Dh >> Dw;
  --Dh, --Dw;
  S.resize(H);
  C.resize(H, vector<LL>(W, kInf));
  for (int i = 0; i < H; ++i)
    cin >> S[i];

  deque<P> que;
  que.push_front({Ch, Cw});
  C[Ch][Cw] = 0;
  while (!que.empty()) {
    auto [r, c] = que.front();
    que.pop_front();
    LL dist = C[r][c];
    vector<pair<int, int>> ds = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
    for (auto [dr, dc] : ds) {
      int nr = r + dr, nc = c + dc;
      if (nr < 0 || H <= nr || nc < 0 || W <= nc)
        continue;
      if (S[nr][nc] != '#') {
        if (C[nr][nc] > dist) {
          C[nr][nc] = dist;
          que.push_front({nr, nc});
        }
      }
    }

    for (int y = r - 2; y <= r + 2; ++y) {
      for (int x = c - 2; x <= c + 2; ++x) {
        if (y < 0 || H <= y || x < 0 || W <= x)
          continue;
        if (S[y][x] != '#') {
          if (C[y][x] > dist + 1LL) {
            C[y][x] = dist + 1LL;
            que.push_back({y, x});
          }
        }
      }
    }
  }

  if (C[Dh][Dw] == kInf)
    cout << -1 << endl;
  else
    cout << C[Dh][Dw] << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define DEBUG 1
using namespace std;
constexpr int kMod = 1000000007;
typedef long long LL;
constexpr LL kInf = 1e18;
typedef tuple<LL, int, int> P;

int H, W;
int Ch, Cw;
int Dh, Dw;
vector<string> S;
vector<vector<LL>> C;

int main() {
  cin >> H >> W;
  cin >> Ch >> Cw;
  --Ch, --Cw;
  cin >> Dh >> Dw;
  --Dh, --Dw;
  S.resize(H);
  C.resize(H, vector<LL>(W, kInf));
  for (int i = 0; i < H; ++i)
    cin >> S[i];

  priority_queue<P> que;
  que.push({0, Ch, Cw});
  C[Ch][Cw] = 0;
  while (!que.empty()) {
    auto [dist, r, c] = que.top();
    que.pop();
    if (C[r][c] < dist)
      continue;
    vector<pair<int, int>> ds = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
    for (auto [dr, dc] : ds) {
      int nr = r + dr, nc = c + dc;
      if (nr < 0 || H <= nr || nc < 0 || W <= nc)
        continue;
      if (S[nr][nc] != '#') {
        if (C[nr][nc] > dist) {
          C[nr][nc] = dist;
          que.push({dist, nr, nc});
        }
      }
    }

    for (int y = r - 2; y <= r + 2; ++y) {
      for (int x = c - 2; x <= c + 2; ++x) {
        if (y < 0 || H <= y || x < 0 || W <= x)
          continue;
        if (S[y][x] != '#') {
          if (C[y][x] > dist + 1LL) {
            C[y][x] = dist + 1LL;
            que.push({dist + 1LL, y, x});
          }
        }
      }
    }
  }

  if (C[Dh][Dw] == kInf)
    cout << -1 << endl;
  else
    cout << C[Dh][Dw] << endl;
}