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

#include <bits/stdc++.h>
#include <boost/multiprecision/cpp_int.hpp>
using namespace std;
using bint = boost::multiprecision::cpp_int;
using ll = long long;
using pint = pair<int, int>;
using pll = pair<ll, ll>;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repSE(i, s, n) for (int i = (s); i < (n); i++)
#define rrepSE(i, s, e) for (int i = (s); i > (e); i--)
#define ssort(v) sort(v.begin(), v.end())
#define gsort(v) sort(v.rbegin(), v.rend())
template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
  for (auto &v : vec)
    is >> v;
  return is;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (auto v : vec)
    os << v << ",";
  os << "]";
  return os;
}
template <typename T> ostream &operator<<(ostream &os, const deque<T> &vec) {
  os << "deq[";
  for (auto v : vec)
    os << v << ",";
  os << "]";
  return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &vec) {
  os << "{";
  for (auto v : vec)
    os << v << ",";
  os << "}";
  return os;
}
template <typename T>
ostream &operator<<(ostream &os, const unordered_set<T> &vec) {
  os << "{";
  for (auto v : vec)
    os << v << ",";
  os << "}";
  return os;
}
template <typename T> ostream &operator<<(ostream &os, const multiset<T> &vec) {
  os << "{";
  for (auto v : vec)
    os << v << ",";
  os << "}";
  return os;
}
template <typename T>
ostream &operator<<(ostream &os, const unordered_multiset<T> &vec) {
  os << "{";
  for (auto v : vec)
    os << v << ",";
  os << "}";
  return os;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &pa) {
  os << "(" << pa.first << "," << pa.second << ")";
  return os;
}
template <typename TK, typename TV>
ostream &operator<<(ostream &os, const map<TK, TV> &mp) {
  os << "{";
  for (auto v : mp)
    os << v.first << "=>" << v.second << ",";
  os << "}";
  return os;
}
template <typename TK, typename TV>
ostream &operator<<(ostream &os, const unordered_map<TK, TV> &mp) {
  os << "{";
  for (auto v : mp)
    os << v.first << "=>" << v.second << ",";
  os << "}";
  return os;
}
template <typename T> bool chmax(T &m, const T q) {
  if (m < q) {
    m = q;
    return true;
  } else
    return false;
}
template <typename T> bool chmin(T &m, const T q) {
  if (q < m) {
    m = q;
    return true;
  } else
    return false;
}
#define DEBUG_PRINT(var) std::cout << #var << " = " << var << "\\n";

const int MX = int(1e9);

const int di[] = {-1, 0, 1, 0};
const int dj[] = {0, -1, 0, 1};

int main() {
  int h, w, ch, cw, dh, dw;
  cin >> h >> w;
  cin >> ch >> cw;
  ch--;
  cw--;
  cin >> dh >> dw;
  dh--;
  dw--;
  vector<string> s(h);
  rep(i, h) cin >> s[i];
  vector<vector<int>> dist(h, vector<int>(w, MX));
  deque<pint> que;
  dist[ch][cw] = 0;
  que.emplace_back(ch, cw);
  while (!que.empty()) {
    int ci = que.front().first;
    int cj = que.front().second;
    que.pop_front();
    int d = dist[ci][cj];
    rep(i, 4) {
      int ni = ci + di[i], nj = cj + dj[i];
      if (ni < 0 || ni >= h || nj < 0 || nj >= w)
        continue;
      if (s[ni][nj] == '#')
        continue;
      if (dist[ni][nj] <= d)
        continue;
      dist[ni][nj] = d;
      que.emplace_front(ni, nj);
    }
    repSE(i, -2, 3) repSE(j, -2, 3) {
      int ni = ci + i, nj = cj + j;
      if (ni < 0 || ni >= h || nj < 0 || nj >= w)
        continue;
      if (s[ni][nj] == '#')
        continue;
      if (dist[ni][nj] <= d + 1)
        continue;
      dist[ni][nj] = d + 1;
      que.emplace_back(ni, nj);
    }
  }
  cout << (dist[dh][dw] == MX ? -1 : dist[dh][dw]) << "\\n";
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <boost/multiprecision/cpp_int.hpp>
using namespace std;
using bint = boost::multiprecision::cpp_int;
using ll = long long;
using pint = pair<int, int>;
using pll = pair<ll, ll>;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repSE(i, s, n) for (int i = (s); i < (n); i++)
#define rrepSE(i, s, e) for (int i = (s); i > (e); i--)
#define ssort(v) sort(v.begin(), v.end())
#define gsort(v) sort(v.rbegin(), v.rend())
template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
  for (auto &v : vec)
    is >> v;
  return is;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (auto v : vec)
    os << v << ",";
  os << "]";
  return os;
}
template <typename T> ostream &operator<<(ostream &os, const deque<T> &vec) {
  os << "deq[";
  for (auto v : vec)
    os << v << ",";
  os << "]";
  return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &vec) {
  os << "{";
  for (auto v : vec)
    os << v << ",";
  os << "}";
  return os;
}
template <typename T>
ostream &operator<<(ostream &os, const unordered_set<T> &vec) {
  os << "{";
  for (auto v : vec)
    os << v << ",";
  os << "}";
  return os;
}
template <typename T> ostream &operator<<(ostream &os, const multiset<T> &vec) {
  os << "{";
  for (auto v : vec)
    os << v << ",";
  os << "}";
  return os;
}
template <typename T>
ostream &operator<<(ostream &os, const unordered_multiset<T> &vec) {
  os << "{";
  for (auto v : vec)
    os << v << ",";
  os << "}";
  return os;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &pa) {
  os << "(" << pa.first << "," << pa.second << ")";
  return os;
}
template <typename TK, typename TV>
ostream &operator<<(ostream &os, const map<TK, TV> &mp) {
  os << "{";
  for (auto v : mp)
    os << v.first << "=>" << v.second << ",";
  os << "}";
  return os;
}
template <typename TK, typename TV>
ostream &operator<<(ostream &os, const unordered_map<TK, TV> &mp) {
  os << "{";
  for (auto v : mp)
    os << v.first << "=>" << v.second << ",";
  os << "}";
  return os;
}
template <typename T> bool chmax(T &m, const T q) {
  if (m < q) {
    m = q;
    return true;
  } else
    return false;
}
template <typename T> bool chmin(T &m, const T q) {
  if (q < m) {
    m = q;
    return true;
  } else
    return false;
}
#define DEBUG_PRINT(var) std::cout << #var << " = " << var << "\\n";

const int MX = int(1e9);

const int di[] = {-1, 0, 1, 0};
const int dj[] = {0, -1, 0, 1};

int main() {
  int h, w, ch, cw, dh, dw;
  cin >> h >> w;
  cin >> ch >> cw;
  ch--;
  cw--;
  cin >> dh >> dw;
  dh--;
  dw--;
  vector<string> s(h);
  rep(i, h) cin >> s[i];
  vector<vector<int>> dist(h, vector<int>(w, MX));
  deque<pint> que;
  dist[ch][cw] = 0;
  que.emplace_back(ch, cw);
  while (!que.empty()) {
    int ci = que.front().first;
    int cj = que.front().second;
    que.pop_front();
    int d = dist[ci][cj];
    rep(i, 4) {
      int ni = ci + di[i], nj = cj + dj[i];
      if (ni < 0 || ni >= h || nj < 0 || nj >= w)
        continue;
      if (s[ni][nj] == '#')
        continue;
      if (dist[ni][nj] <= d)
        continue;
      dist[ni][nj] = d;
      que.emplace_back(ni, nj);
    }
    repSE(i, -2, 3) repSE(j, -2, 3) {
      int ni = ci + i, nj = cj + j;
      if (ni < 0 || ni >= h || nj < 0 || nj >= w)
        continue;
      if (s[ni][nj] == '#')
        continue;
      if (dist[ni][nj] <= d + 1)
        continue;
      dist[ni][nj] = d + 1;
      que.emplace_back(ni, nj);
    }
  }
  cout << (dist[dh][dw] == MX ? -1 : dist[dh][dw]) << "\\n";
  return 0;
}