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

// 2020-08-23 22:46:59
#include <cstring>
#include <deque>
#include <iostream>
#include <queue>
#include <tuple>
#include <vector>

#ifdef LOCAL
#include "lib/debug.hpp"
#else
#define debug(...) 1
#endif
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define rep(i, n) REP(i, 0, (n))
#define repc(i, n) REPC(i, 0, (n))
#define REP(i, n, m) for (int i = (int)(n); i < (int)(m); i++)
#define REPC(i, n, m) for (int i = (int)(n); i <= (int)(m); i++)
#define REPCM(i, n, m) for (int i = (int)(n); i >= (int)(m); i--)
using namespace std;
using ll = long long;
using ld = long double;
using pr = pair<ll, ll>;
using vll = vector<ll>;
using vpr = vector<pr>;
using P = pair<int, int>;
template <class T> inline bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  } else
    return false;
}
template <class T> inline bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  } else
    return false;
}

using T = tuple<int, int, int>;
const int inf = 1e9;
void answer() {
  int h, w;
  cin >> h >> w;
  int Ch, Cw, Dh, Dw;
  cin >> Ch >> Cw >> Dh >> Dw;
  Ch--;
  Cw--;
  Dh--;
  Dw--;
  vector<string> maze(h);
  rep(i, h) {
    string s;
    cin >> s;
    maze[i] = s;
  }
  deque<T> que;
  vector<vector<int>> D(h, vector<int>(w, inf));
  que.emplace_front(0, Ch, Cw);
  while (!que.empty()) {
    auto [cd, cy, cx] = que.front();
    que.pop_front();
    if (D[cy][cx] < cd)
      continue;
    chmin(D[cy][cx], cd);
    REPC(dy, -2, 2) REPC(dx, -2, 2) {
      if (dy == 0 && dx == 0)
        continue;
      int ny = cy + dy, nx = cx + dx;
      if (ny < 0 || h <= ny || nx < 0 || w <= nx || maze[ny][nx] == '#')
        continue;
      if (abs(ny - cy) + abs(nx - cx) == 1) {
        int nd = cd;
        if (chmin(D[ny][nx], nd)) {
          que.emplace_front(nd, ny, nx);
        }
      } else {
        int nd = cd + 1;
        if (chmin(D[ny][nx], nd)) {
          que.emplace_back(nd, ny, nx);
        }
      }
    }
  }
  int ans = D[Dh][Dw];
  if (ans == inf)
    ans = -1;
  cout << ans << '\\n';
}
int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  answer();
  return 0;
}

Error: TLE

Fixed submission:

#include <cstring>
#include <iostream>
#include <queue>
#include <tuple>
#include <vector>

using namespace std;
using Vec3i = tuple<int, int, int>;

const char Wall = '#', Road = '.';
char Maze[1001][1001];
unsigned H, W, Ch, Cw, Dh, Dw;
int Magic[1001][1001];

inline bool in_range(int x, int y) {
  return x >= 1 && x <= W && y >= 1 && y <= H;
}

int resolve(int x, int y) {
  queue<Vec3i> path{};
  path.emplace(x, y, 0);

  while (!path.empty()) {
    const auto &[fx, fy, fz] = path.front();
    path.pop();
    if (Magic[fy][fx] < fz)
      continue;
    if (Magic[fy][fx] > fz)
      Magic[fy][fx] = fz;
    for (int dy = -2; dy <= 2; ++dy) {
      for (int dx = -2; dx <= 2; ++dx) {
        if (dy == 0 && dx == 0)
          continue;
        int cx = fx + dx, cy = fy + dy;
        if (!in_range(cx, cy) || Maze[cy][cx] == Wall)
          continue;
        if (abs(dx) + abs(dy) == 1) {
          if (Magic[cy][cx] > fz) {
            Magic[cy][cx] = fz;
            path.emplace(cx, cy, fz);
          }
        } else {
          int z = fz + 1;
          if (Magic[cy][cx] > z) {
            Magic[cy][cx] = z;
            path.emplace(cx, cy, z);
          }
        }
      }
    }
  }

  return Magic[Dh][Dw];
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  cin >> H >> W >> Ch >> Cw >> Dh >> Dw;
  for (size_t i = 1; i <= H; i++) {
    for (size_t j = 1; j <= W; j++) {
      cin >> Maze[i][j];
    }
  }

  for (auto &i : Magic) {
    fill_n(begin(i), 1001, INT32_MAX);
  }
  int v = resolve(Cw, Ch);
  cout << (v == INT32_MAX ? -1 : v) << endl;

  return 0;
}