Problem p02579 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define fi first
#define se second
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define rep2(i, a, b) for (int(i) = (a); (i) < (b); (i)++)
#define rev(i, n) for (int(i) = (n)-1; (i) >= 0; (i)--)
#define rev2(i, a, b) for (int(i) = (a)-1; (i) >= (b); (i)--)
#define rng(a) a.begin(), a.end()
#define rrng(a) a.rbegin(), a.rend()
#define pb push_back
#define eb emplace_back
#define yn                                                                     \
  { puts("Yes"); }                                                             \
  else {                                                                       \
    puts("No");                                                                \
  }
using namespace std;
using ll = long long;
using P = pair<int, int>;
using Pll = pair<long long, long long>;
template <class T> using V = vector<T>;
template <class T> using VV = vector<vector<T>>;
long long TEN(int i) { return !i ? 1 : TEN(i - 1) * 10LL; }
template <class T, class U> bool chmin(T &a, const U &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T, class U> bool chmax(T &a, const U &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}

struct IoSetup {
  IoSetup() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(15);
  }
} iosetup;

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//

int H, W, Ch, Cw, Dh, Dw;
V<string> S(1005);

const int INF = TEN(9);

#define int long long

signed main() {
  cin >> H >> W >> Ch >> Cw >> Dh >> Dw;
  rep(i, H) cin >> S[i];

  Ch--, Cw--, Dw--, Dh--;

  VV<int> val(1005, V<int>(1005, INF));
  val[Ch][Cw] = 0;

  deque<P> Q;
  VV<bool> visited(1005, V<bool>(1005, false));
  Q.emplace_front(-1, -1);
  Q.emplace_front(Ch, Cw);
  int d = 0;
  while (Q.size() > 1) {

    auto [y, x] = Q.front();
    Q.pop_front();
    if (y == -1) {
      d++;
      Q.emplace_back(-1, -1);
      tie(y, x) = Q.front();
    } else {
      visited[y][x] = true;
    }
    rep2(i, -2, 3) rep2(j, -2, 3) {
      int ny = y + i;
      int nx = x + j;
      if ((!i && !j) || ny < 0 || ny >= H || nx < 0 || nx >= W ||
          S[ny][nx] == '#' || visited[ny][nx]) {
        continue;
      }
      if (abs(i) + abs(j) <= 1) {
        chmin(val[ny][nx], d);
        Q.emplace_front(ny, nx);
      } else {
        chmin(val[ny][nx], d + 1);
        Q.emplace_back(ny, nx);
      }
    }
  }

  if (val[Dh][Dw] == INF)
    cout << -1 << endl;
  else
    cout << val[Dh][Dw] << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define fi first
#define se second
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define rep2(i, a, b) for (int(i) = (a); (i) < (b); (i)++)
#define rev(i, n) for (int(i) = (n)-1; (i) >= 0; (i)--)
#define rev2(i, a, b) for (int(i) = (a)-1; (i) >= (b); (i)--)
#define rng(a) a.begin(), a.end()
#define rrng(a) a.rbegin(), a.rend()
#define pb push_back
#define eb emplace_back
#define yn                                                                     \
  { puts("Yes"); }                                                             \
  else {                                                                       \
    puts("No");                                                                \
  }
using namespace std;
using ll = long long;
using P = pair<int, int>;
using Pll = pair<long long, long long>;
template <class T> using V = vector<T>;
template <class T> using VV = vector<vector<T>>;
long long TEN(int i) { return !i ? 1 : TEN(i - 1) * 10LL; }
template <class T, class U> bool chmin(T &a, const U &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T, class U> bool chmax(T &a, const U &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}

struct IoSetup {
  IoSetup() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(15);
  }
} iosetup;

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//

int H, W, Ch, Cw, Dh, Dw;
V<string> S(1005);

const int INF = TEN(9);

#define int long long

signed main() {
  cin >> H >> W >> Ch >> Cw >> Dh >> Dw;
  rep(i, H) cin >> S[i];

  Ch--, Cw--, Dw--, Dh--;

  VV<int> val(1005, V<int>(1005, INF));
  val[Ch][Cw] = 0;

  deque<P> Q;
  VV<bool> visited(1005, V<bool>(1005, false));
  Q.emplace_front(-1, -1);
  Q.emplace_front(Ch, Cw);
  int d = 0;
  while (Q.size() > 1) {

    auto [y, x] = Q.front();
    Q.pop_front();
    if (y == -1) {
      d++;
      Q.emplace_back(-1, -1);
      tie(y, x) = Q.front();
      Q.pop_front();
    } else {
      visited[y][x] = true;
    }
    rep2(i, -2, 3) rep2(j, -2, 3) {
      int ny = y + i;
      int nx = x + j;
      if ((!i && !j) || ny < 0 || ny >= H || nx < 0 || nx >= W ||
          S[ny][nx] == '#' || visited[ny][nx]) {
        continue;
      }
      if (abs(i) + abs(j) <= 1) {
        chmin(val[ny][nx], d);
        Q.emplace_front(ny, nx);
      } else {
        chmin(val[ny][nx], d + 1);
        Q.emplace_back(ny, nx);
      }
    }
  }

  if (val[Dh][Dw] == INF)
    cout << -1 << endl;
  else
    cout << val[Dh][Dw] << endl;

  return 0;
}