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

#include <bits/stdc++.h>
using namespace std;
#define REP(i, b, e) for (int i = (b); i < (e); ++i)
#define RREP(i, b, e) for (int i = (b)-1; i >= e; --i)
#define rep(i, e) for (int i = 0; i < (e); ++i)

constexpr int MOD = 1000000007;
constexpr int INF = 1LL << 30;
constexpr long long LLINF = 1LL << 62;
constexpr double EPS = 1e-9;

struct Initializer {
  Initializer() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    cout << fixed << setprecision(15) << boolalpha;
  }
} initializer;

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (auto it = vec.begin(); it != vec.end(); ++it) {
    if (it != vec.begin())
      os << ", ";
    os << *it;
  }
  return os << "]";
}

template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &pa) {
  return os << "(" << pa.first << ", " << pa.second << ")";
}

template <typename Tuple, size_t N> struct TuplePrinter {
  static void print(const Tuple &t) {
    TuplePrinter<Tuple, N - 1>::print(t);
    cout << ", " << get<N - 1>(t);
  }
};

template <typename Tuple> struct TuplePrinter<Tuple, 1> {
  static void print(const Tuple &t) { cout << get<0>(t); }
};

template <typename... T>
ostream &operator<<(ostream &os, const tuple<T...> &tup) {
  os << "(";
  TuplePrinter<decltype(tup), sizeof...(T)>::print(tup);
  return os << ")";
}

template <typename T> ostream &operator<<(ostream &os, const set<T> &se) {
  os << "{";
  for (auto it = se.begin(); it != se.end(); ++it) {
    if (it != se.begin())
      os << ", ";
    os << *it;
  }
  return os << "}";
}

template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &ma) {
  os << "{";
  for (auto it = ma.begin(); it != ma.end(); ++it) {
    if (it != ma.begin())
      os << ", ";
    os << it->first << ": " << it->second;
  }
  return os << "}";
}

inline void print(void) { cout << '\\n'; }
template <class T> inline void print(const T &x) { cout << x << '\\n'; }
template <class T, class... U> inline void print(const T &x, const U &...y) {
  cout << x << " ";
  print(y...);
}

int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};

int main() {
  int h, w;
  cin >> h >> w;
  int ch, cw;
  cin >> ch >> cw;
  --ch;
  --cw;
  int dh, dw;
  cin >> dh >> dw;
  --dh;
  --dw;
  vector<string> s(h);
  rep(i, h) cin >> s[i];
  vector<vector<int>> dist(h, vector<int>(w, INF));
  dist[ch][cw] = 0;

  queue<pair<int, int>> q1, q2;
  q1.emplace(ch, cw);
  bool flag = 0;
  while (!q1.empty() or !q2.empty()) {
    int y, x;
    if (!q1.empty()) {
      tie(y, x) = q1.front();
      q1.pop();
    } else {
      tie(y, x) = q2.front();
      q2.pop();
    }
    rep(i, 4) {
      int nx = x + dx[i], ny = y + dy[i];
      if (!(0 <= ny and ny < h and 0 <= nx and nx < w and s[ny][nx] != '#'))
        continue;
      if (dist[ny][nx] > dist[y][x]) {
        dist[ny][nx] = dist[y][x];
        q1.emplace(ny, nx);
      }
    }
    for (int i = -2; i <= 2; i++)
      for (int j = -2; j <= 2; j++) {
        int nx = x + i, ny = y + j;
        if (!(0 <= ny and ny < h and 0 <= nx and nx < w and s[ny][nx] != '#'))
          continue;
        if (dist[ny][nx] > (dist[y][x] + 1)) {
          dist[ny][nx] = dist[y][x] + 1;
          q2.emplace(ny, nx);
        }
      }
  }
  if (dist[dh][dw] == INF) {
    print(-1);
  } else {
    print(dist[dh][dw]);
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define REP(i, b, e) for (int i = (b); i < (e); ++i)
#define RREP(i, b, e) for (int i = (b)-1; i >= e; --i)
#define rep(i, e) for (int i = 0; i < (e); ++i)

constexpr int MOD = 1000000007;
constexpr int INF = 1LL << 30;
constexpr long long LLINF = 1LL << 62;
constexpr double EPS = 1e-9;

struct Initializer {
  Initializer() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    cout << fixed << setprecision(15) << boolalpha;
  }
} initializer;

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (auto it = vec.begin(); it != vec.end(); ++it) {
    if (it != vec.begin())
      os << ", ";
    os << *it;
  }
  return os << "]";
}

template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &pa) {
  return os << "(" << pa.first << ", " << pa.second << ")";
}

template <typename Tuple, size_t N> struct TuplePrinter {
  static void print(const Tuple &t) {
    TuplePrinter<Tuple, N - 1>::print(t);
    cout << ", " << get<N - 1>(t);
  }
};

template <typename Tuple> struct TuplePrinter<Tuple, 1> {
  static void print(const Tuple &t) { cout << get<0>(t); }
};

template <typename... T>
ostream &operator<<(ostream &os, const tuple<T...> &tup) {
  os << "(";
  TuplePrinter<decltype(tup), sizeof...(T)>::print(tup);
  return os << ")";
}

template <typename T> ostream &operator<<(ostream &os, const set<T> &se) {
  os << "{";
  for (auto it = se.begin(); it != se.end(); ++it) {
    if (it != se.begin())
      os << ", ";
    os << *it;
  }
  return os << "}";
}

template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &ma) {
  os << "{";
  for (auto it = ma.begin(); it != ma.end(); ++it) {
    if (it != ma.begin())
      os << ", ";
    os << it->first << ": " << it->second;
  }
  return os << "}";
}

inline void print(void) { cout << '\\n'; }
template <class T> inline void print(const T &x) { cout << x << '\\n'; }
template <class T, class... U> inline void print(const T &x, const U &...y) {
  cout << x << " ";
  print(y...);
}

int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};

int main() {
  int h, w;
  cin >> h >> w;
  int ch, cw;
  cin >> ch >> cw;
  --ch;
  --cw;
  int dh, dw;
  cin >> dh >> dw;
  --dh;
  --dw;
  vector<string> s(h);
  rep(i, h) cin >> s[i];
  vector<vector<int>> dist(h, vector<int>(w, INF));
  dist[ch][cw] = 0;

  queue<pair<int, int>> q;
  q.emplace(ch, cw);
  while (!q.empty()) {
    int y, x;
    tie(y, x) = q.front();
    q.pop();
    rep(i, 4) {
      int nx = x + dx[i], ny = y + dy[i];
      if (!(0 <= ny and ny < h and 0 <= nx and nx < w))
        continue;
      if (dist[ny][nx] > dist[y][x] and s[ny][nx] != '#') {
        dist[ny][nx] = dist[y][x];
        q.emplace(ny, nx);
      }
    }
    for (int i = -2; i <= 2; i++)
      for (int j = -2; j <= 2; j++) {
        int nx = x + i, ny = y + j;
        if (!(0 <= ny and ny < h and 0 <= nx and nx < w))
          continue;
        if (dist[ny][nx] > (dist[y][x] + 1) and s[ny][nx] != '#') {
          dist[ny][nx] = dist[y][x] + 1;
          q.emplace(ny, nx);
        }
      }
  }
  if (dist[dh][dw] == INF) {
    print(-1);
  } else {
    print(dist[dh][dw]);
  }

  return 0;
}