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

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

// typedef
//------------------------------------------
using LL = int64_t;
using VL = vector<LL>;
using VVL = vector<VL>;
using VS = vector<string>;
using PLL = pair<LL, LL>;

// container util
//------------------------------------------
#define whole(f, x, ...)                                                       \\
  ([&](decltype((x)) whole) {                                                  \\
    return (f)(begin(whole), end(whole), ##__VA_ARGS__);                       \\
  })(x)
#define rwhole(f, x, ...)                                                      \\
  ([&](decltype((x)) whole) {                                                  \\
    return (f)(rbegin(whole), rend(whole), ##__VA_ARGS__);                     \\
  })(x)
#define EACH(i, c)                                                             \\
  for (decltype((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define ALL(x) ::std::begin(x), ::std::end(x)
#define RALL(x) ::std::rbegin(x), ::std::rend(x)
#define SORT(c) whole(sort, c)
#define RSORT(c) rwhole(sort, c)

// constant
//--------------------------------------------
constexpr double EPS = 1e-10;
constexpr double PI = 3.14159265358979323846;
constexpr int MOD = 1000000007;

// grid
//--------------------------------------------
VL dx = {0, 1, 0, -1};
VL dy = {1, 0, -1, 0};
VL dx2 = {-1, 0, 1, -1, 1, -1, 0, 1};
VL dy2 = {-1, -1, -1, 0, 0, 1, 1, 1};

// debug
//--------------------------------------------
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;

// IO accelerate
//--------------------------------------------
struct InitIO {
  InitIO() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(30);
  }
} init_io;

// template
//--------------------------------------------
//  declaretion
template <typename T> istream &operator>>(istream &is, vector<T> &vec);
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p);
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec);
template <typename T>
ostream &operator<<(ostream &os, const vector<vector<T>> &vv);
template <typename T> vector<T> make_v(size_t a);
template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts);
template <typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type fill_v(T &t, const V &v);
template <typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type fill_v(T &t, const V &v);

// implementation
template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
  for (T &x : vec)
    is >> x;
  return is;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  os << p.first << "," << p.second;
  return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  for (int i = 0; i < vec.size(); i++) {
    os << vec[i] << (i + 1 == vec.size() ? "" : "\\t");
  }
  return os;
}
template <typename T>
ostream &operator<<(ostream &s, const vector<vector<T>> &vv) {
  for (int i = 0; i < vv.size(); ++i) {
    s << vv[i] << endl;
  }
  return s;
}

// 多重vector
// auto dp=make_v<int>(4,h,w) みたいに使える
template <typename T> vector<T> make_v(size_t a) { return vector<T>(a); }

template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
  return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}

// 多重vectorのためのfill
// fill_v(dp,0) みたいに使える
template <typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type fill_v(T &t, const V &v) {
  t = v;
}

template <typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type fill_v(T &t, const V &v) {
  for (auto &e : t)
    fill_v(e, v);
}

template <class T, class U> void chmax(T &a, U b) {
  if (a < b)
    a = b;
}
template <class T, class U> void chmin(T &a, U b) {
  if (b < a)
    a = b;
}
template <typename T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template <typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }

// main code

int main(int argc, char *argv[]) {
  LL h, w;
  cin >> h >> w;
  int ch, cw, dh, dw;
  cin >> ch >> cw >> dh >> dw;
  VS s(h);
  cin >> s;
  ch--;
  cw--;
  dh--;
  dw--;
  deque<PLL> dq;
  VVL score(h, VL(w, INT32_MAX));
  dq.push_front(make_pair(ch, cw));
  score[ch][cw] = 0;
  while (!dq.empty()) {
    auto [vh, vw] = dq.front();
    dq.pop_front();
    for (int i = -2; i < 3; i++) {
      for (int j = -2; j < 3; j++) {
        if (i == 0 and j == 0) {
          continue;
        }
        int nh = vh + i;
        int nw = vw + j;
        if (nh < 0 or nw < 0 or nh >= h or nw >= w or s[nh][nw] == '#') {
          continue;
        }
        if (abs(i) + abs(j) == 1) {
          if (score[nh][nw] > score[vh][vw]) {
            score[nh][nw] = score[vh][vw];
            dq.push_front(make_pair(nh, nw));
          }
        } else {
          if (score[nh][nw] > score[vh][vw] + 1) {
            score[nh][nw] = score[vh][vw] + 1;
            dq.push_back(make_pair(nh, nw));
          }
        }
      }
    }
  }
  if (score[dh][dw] == INT32_MAX) {
    cout << -1 << endl;
  } else {
    cout << score[dh][dw] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

// typedef
//------------------------------------------
using LL = int64_t;
using VL = vector<LL>;
using VVL = vector<VL>;
using VS = vector<string>;
using PLL = pair<LL, LL>;

// container util
//------------------------------------------
#define whole(f, x, ...)                                                       \\
  ([&](decltype((x)) whole) {                                                  \\
    return (f)(begin(whole), end(whole), ##__VA_ARGS__);                       \\
  })(x)
#define rwhole(f, x, ...)                                                      \\
  ([&](decltype((x)) whole) {                                                  \\
    return (f)(rbegin(whole), rend(whole), ##__VA_ARGS__);                     \\
  })(x)
#define EACH(i, c)                                                             \\
  for (decltype((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define ALL(x) ::std::begin(x), ::std::end(x)
#define RALL(x) ::std::rbegin(x), ::std::rend(x)
#define SORT(c) whole(sort, c)
#define RSORT(c) rwhole(sort, c)

// constant
//--------------------------------------------
constexpr double EPS = 1e-10;
constexpr double PI = 3.14159265358979323846;
constexpr int MOD = 1000000007;

// grid
//--------------------------------------------
VL dx = {0, 1, 0, -1};
VL dy = {1, 0, -1, 0};
VL dx2 = {-1, 0, 1, -1, 1, -1, 0, 1};
VL dy2 = {-1, -1, -1, 0, 0, 1, 1, 1};

// debug
//--------------------------------------------
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;

// IO accelerate
//--------------------------------------------
struct InitIO {
  InitIO() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(30);
  }
} init_io;

// template
//--------------------------------------------
//  declaretion
template <typename T> istream &operator>>(istream &is, vector<T> &vec);
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p);
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec);
template <typename T>
ostream &operator<<(ostream &os, const vector<vector<T>> &vv);
template <typename T> vector<T> make_v(size_t a);
template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts);
template <typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type fill_v(T &t, const V &v);
template <typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type fill_v(T &t, const V &v);

// implementation
template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
  for (T &x : vec)
    is >> x;
  return is;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  os << p.first << "," << p.second;
  return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  for (int i = 0; i < vec.size(); i++) {
    os << vec[i] << (i + 1 == vec.size() ? "" : "\\t");
  }
  return os;
}
template <typename T>
ostream &operator<<(ostream &s, const vector<vector<T>> &vv) {
  for (int i = 0; i < vv.size(); ++i) {
    s << vv[i] << endl;
  }
  return s;
}

// 多重vector
// auto dp=make_v<int>(4,h,w) みたいに使える
template <typename T> vector<T> make_v(size_t a) { return vector<T>(a); }

template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
  return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}

// 多重vectorのためのfill
// fill_v(dp,0) みたいに使える
template <typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type fill_v(T &t, const V &v) {
  t = v;
}

template <typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type fill_v(T &t, const V &v) {
  for (auto &e : t)
    fill_v(e, v);
}

template <class T, class U> void chmax(T &a, U b) {
  if (a < b)
    a = b;
}
template <class T, class U> void chmin(T &a, U b) {
  if (b < a)
    a = b;
}
template <typename T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template <typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }

// main code

int main(int argc, char *argv[]) {
  LL h, w;
  cin >> h >> w;
  LL ch, cw, dh, dw;
  cin >> ch >> cw >> dh >> dw;
  VS s(h);
  cin >> s;
  VVL score(h, VL(w, INT32_MAX));
  ch--;
  cw--;
  dh--;
  dw--;
  score[ch][cw] = 0;
  auto dfs = [&](auto &&dfs, int ph, int pw, int vh, int vw, int wp) {
    if (vh == dh and vw == dw) {
      return;
    }
    for (int i = 0; i < 4; i++) {
      int nh = vh + dy[i];
      int nw = vw + dx[i];
      if (nh >= 0 and nw >= 0 and nh < h and nw < w and s[nh][nw] == '.' and
          score[nh][nw] < wp) {
        wp = score[nh][nw];
        score[vh][vw] = wp;
      }
    }
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        int nh = vh + i;
        int nw = vw + j;
        if (nh >= 0 and nw >= 0 and nh < h and nw < w and s[nh][nw] == '.' and
            score[nh][nw] + 1 < wp) {
          wp = score[nh][nw] + 1;
          score[vh][vw] = wp;
        }
      }
    }
    for (int i = 0; i < 4; i++) {
      int nh = vh + dy[i];
      int nw = vw + dx[i];
      if (nh >= 0 and nw >= 0 and nh < h and nw < w and s[nh][nw] == '.' and
          score[nh][nw] > wp) {
        score[nh][nw] = wp;
        dfs(dfs, vh, vw, nh, nw, wp);
      }
    }
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        int nh = vh + i;
        int nw = vw + j;
        if (nh >= 0 and nw >= 0 and nh < h and nw < w and s[nh][nw] == '.' and
            score[nh][nw] > wp + 1) {
          score[nh][nw] = wp + 1;
          dfs(dfs, vh, vw, nh, nw, wp + 1);
        }
      }
    }
  };
  dfs(dfs, -1, -1, ch, cw, 0);
  if (score[dh][dw] == INT32_MAX) {
    cout << -1 << endl;
  } else {
    cout << score[dh][dw] << endl;
  }
  return 0;
}