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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, n) for (int i = n - 1; i >= 0; --i)
#define fi first
#define se second
using namespace std;
using lint = long long;
using uint = unsigned int;
using ulint = unsigned long long;
using ldouble = long double;
using pii = pair<int, int>;
using pli = pair<lint, lint>;
using pdd = pair<double, double>;
using pld = pair<ldouble, ldouble>;
using v1i = vector<int>;
using v1li = vector<lint>;
using v2i = vector<vector<int>>;
using v2li = vector<vector<lint>>;
using v3i = vector<vector<vector<int>>>;
using v3li = vector<vector<vector<lint>>>;
using v1b = vector<bool>;
using v2b = vector<vector<bool>>;
using v3b = vector<vector<vector<bool>>>;
using v1c = vector<char>;
using v2c = vector<vector<char>>;
using v3c = vector<vector<vector<char>>>;
constexpr lint mod1 = 1e9 + 7;
constexpr lint mod2 = 998244353;

int main() {
  int h, w, ch, cw, dh, dw, a;
  cin >> h >> w >> ch >> cw >> dh >> dw;
  vector<string> v(h);
  rep(i, h) cin >> v[i];
  ch--;
  cw--;
  dh--;
  dw--;
  v2i ans(h, v1i(w, 1e9));
  pii tmp = {ch * w + cw, 0};
  deque<pii> d, e;
  d.push_back(tmp);
  while (!d.empty() || !e.empty()) {
    if (!d.empty()) {
      tmp = d.front();
      d.pop_front();
    } else {
      tmp = e.front();
      e.pop_front();
    }
    int th = tmp.fi / w, tw = tmp.fi % w, tc = tmp.se;
    if (ans[th][tw] < tc)
      continue;
    rep(i, 5) {
      int thi = th - 2 + i;
      if (thi < 0 || thi >= h)
        continue;
      rep(j, 5) {
        int twj = tw - 2 + j;
        if (i == 2 & j == 2)
          continue;
        if (twj < 0 || twj >= w)
          continue;
        if (v[thi][twj] == '#')
          continue;
        if ((i == 1 && j == 2) || (i == 2 && j == 1) || (i == 2 && j == 3) ||
            (i == 3 && j == 2)) {
          if (tc < ans[thi][twj]) {
            d.push_back({thi * w + twj, tc});
            ans[thi][twj] = tc;
          }
        } else {
          if (tc + 1 < ans[thi][twj]) {
            e.push_back({thi * w + twj, tc + 1});
            ans[thi][twj] = tc + 1;
          }
        }
      }
    }
  }
  if (ans[dh][dw] == 1e9)
    a = -1;
  else
    a = ans[dh][dw];
  cout << a << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, n) for (int i = n - 1; i >= 0; --i)
#define fi first
#define se second
using namespace std;
using lint = long long;
using uint = unsigned int;
using ulint = unsigned long long;
using ldouble = long double;
using pii = pair<int, int>;
using pli = pair<lint, lint>;
using pdd = pair<double, double>;
using pld = pair<ldouble, ldouble>;
using v1i = vector<int>;
using v1li = vector<lint>;
using v2i = vector<vector<int>>;
using v2li = vector<vector<lint>>;
using v3i = vector<vector<vector<int>>>;
using v3li = vector<vector<vector<lint>>>;
using v1b = vector<bool>;
using v2b = vector<vector<bool>>;
using v3b = vector<vector<vector<bool>>>;
using v1c = vector<char>;
using v2c = vector<vector<char>>;
using v3c = vector<vector<vector<char>>>;
constexpr lint mod1 = 1e9 + 7;
constexpr lint mod2 = 998244353;

int main() {
  int h, w, ch, cw, dh, dw, a;
  cin >> h >> w >> ch >> cw >> dh >> dw;
  vector<string> v(h);
  rep(i, h) cin >> v[i];
  ch--;
  cw--;
  dh--;
  dw--;
  v2i ans(h, v1i(w, 1e9));
  pii tmp = {ch * w + cw, 0};
  deque<pii> d, e;
  d.push_back(tmp);
  while (!d.empty() || !e.empty()) {
    if (!d.empty()) {
      tmp = d.back();
      d.pop_back();
    } else {
      tmp = e.back();
      e.pop_back();
    }
    int th = tmp.fi / w, tw = tmp.fi % w, tc = tmp.se;
    if (ans[th][tw] < tc)
      continue;
    rep(i, 5) {
      int thi = th - 2 + i;
      if (thi < 0 || thi >= h)
        continue;
      rep(j, 5) {
        int twj = tw - 2 + j;
        if (i == 2 & j == 2)
          continue;
        if (twj < 0 || twj >= w)
          continue;
        if (v[thi][twj] == '#')
          continue;
        if ((i == 1 && j == 2) || (i == 2 && j == 1) || (i == 2 && j == 3) ||
            (i == 3 && j == 2)) {
          if (tc < ans[thi][twj]) {
            d.push_back({thi * w + twj, tc});
            ans[thi][twj] = tc;
          }
        } else {
          if (tc + 1 < ans[thi][twj]) {
            e.push_back({thi * w + twj, tc + 1});
            ans[thi][twj] = tc + 1;
          }
        }
      }
    }
  }
  if (ans[dh][dw] == 1e9)
    a = -1;
  else
    a = ans[dh][dw];
  cout << a << endl;
  return 0;
}