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

// 2020-08-22 23:03:05
#include <bits/stdc++.h>
#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;
}

const int inf = 1e9;
int h, w, S[2], G[2];
void answer() {
  cin >> h >> w >> S[0] >> S[1] >> G[0] >> G[1];
  S[0]--;
  S[1]--;
  G[0]--;
  G[1]--;
  vector<vector<int>> wall(h, vector<int>(w, 0));
  vector<vector<int>> dist(h, vector<int>(w, inf));
  rep(i, h) {
    string s;
    cin >> s;
    rep(j, w) wall[i][j] = (s[j] == '#');
  }
  using T = tuple<int, int, int>;
  deque<T> que;
  que.emplace_front(T(0, S[0], S[1]));
  dist[S[0]][S[1]] = 0;
  while (!que.empty()) {
    auto [cd, cy, cx] = que.front();
    que.pop_front();
    if (dist[cy][cx] < cd)
      continue;
    REPC(ny, cy - 2, cy + 2) REPC(nx, cx - 2, cx + 2) {
      if (ny < 0 || h <= ny || nx < 0 || w <= nx || wall[ny][nx])
        continue;
      if (cy == ny && cx == nx)
        continue;
      else if (abs(cy - ny) + abs(cx - nx) == 1) {
        int nd = cd;
        if (chmin(dist[ny][nx], nd)) {
          que.emplace_front(T(nd, ny, nx));
        }
      } else {
        int nd = cd + 1;
        if (chmin(dist[ny][nx], nd)) {
          que.emplace_back(T(nd, ny, nx));
        }
      }
    }
  }
  int ans = dist[G[0]][G[1]];
  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:

// 2020-08-22 21:07:09
#include <bits/stdc++.h>
#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;
}

const int MX = 1010;
const int inf = 1e9;
void answer() {
  int h, w;
  cin >> h >> w;
  int ch, cw;
  cin >> ch >> cw;
  ch--;
  cw--;
  int dh, dw;
  cin >> dh >> dw;
  dh--;
  dw--;
  vector<vector<int>> wall(h, vector<int>(w, 0));
  vector<vector<int>> D(h, vector<int>(w, inf)); // B, A
  rep(i, h) {
    string s;
    cin >> s;
    rep(j, w) { wall[i][j] = (s[j] == '#'); }
  }
  int dy[] = {1, 0, -1, 0};
  int dx[] = {0, 1, 0, -1};
  using T = tuple<int, int, int>;
  queue<T> que;
  que.emplace(T(0, ch, cw));
  while (!que.empty()) {
    auto [cd, y, x] = que.front();
    que.pop();
    if (D[y][x] < cd)
      continue;
    chmin(D[y][x], cd);
    rep(di, 4) {
      int ny = y + dy[di];
      int nx = x + dx[di];
      if (0 <= ny && ny < h && 0 <= nx && nx < w && !wall[ny][nx]) {
        auto nd = D[y][x];
        if (chmin(D[ny][nx], nd)) {
          que.emplace(T(nd, ny, nx));
        }
      }
    }
    REPC(ny, y - 2, y + 2) REPC(nx, x - 2, x + 2) {
      if (0 <= ny && ny < h && 0 <= nx && nx < w && !wall[ny][nx]) {
        auto nd = D[y][x] + 1;
        if (chmin(D[ny][nx], nd)) {
          que.emplace(T(nd, ny, nx));
        }
      }
    }
  }
  int ans = -1;
  if (D[dh][dw] < inf)
    ans = D[dh][dw];
  cout << ans << '\\n';
}
int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  answer();
  return 0;
}