Problem p02579 (C++) — buggy submission, verdict: Runtime Error

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

#define REP(i, m, n) for (int i = (m); i < (int)(n); i++)
#define RREP(i, m, n) for (int i = (int)((n)-1); i >= m; i--)
#define rep(i, n) REP(i, 0, n)
#define rrep(i, n) RREP(i, 0, n)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define fi first
#define se second
#define debug(...)                                                             \\
  {                                                                            \\
    cerr << "[L" << __LINE__ << "] ";                                          \\
    _debug(__VA_ARGS__);                                                       \\
  }

template <typename T> string join(const vector<T> &v, string del = ", ") {
  stringstream s;
  for (auto x : v)
    s << del << x;
  return s.str().substr(del.size());
}
template <typename T> ostream &operator<<(ostream &o, const vector<T> &v) {
  if (v.size())
    o << "[" << join(v) << "]";
  return o;
}
template <typename T>
ostream &operator<<(ostream &o, const vector<vector<T>> &vv) {
  int l = vv.size();
  if (l) {
    o << endl;
    rep(i, l) o << (i == 0 ? "[ " : ",\\n  ") << vv[i]
                << (i == l - 1 ? " ]" : "");
  }
  return o;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &o, const pair<T1, T2> &p) {
  return o << "(" << p.first << ", " << p.second << ")";
}
inline void _debug() { cerr << endl; }
template <class First, class... Rest>
void _debug(const First &first, const Rest &...rest) {
  cerr << first << " ";
  _debug(rest...);
}

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;

const double PI = (1 * acos(0.0));
const double EPS = 1e-9;
const int INF = 0x3f3f3f3f;
const ll INFL = 0x3f3f3f3f3f3f3f3fLL;
const ll mod = 1e9 + 7;

inline void finput(string filename) { freopen(filename.c_str(), "r", stdin); }

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

int h, w;
vector<string> g;
vvi bfs(int si, int sj) {
  vvi d = vvi(h, vi(w, INF));
  d[si][sj] = 0;
  deque<pii> dq({pii(si, sj)});
  while (!dq.empty()) {
    int i = dq.front().fi;
    int j = dq.front().se;
    dq.pop_front();
    rep(k, 4) {
      int ni = i + di[k];
      int nj = j + dj[k];
      if (ni < 0 || h <= ni)
        continue;
      if (nj < 0 || w <= nj)
        continue;
      if (g[ni][nj] == '#')
        continue;
      if (d[ni][nj] > d[i][j]) {
        d[ni][nj] = d[i][j];
        dq.emplace_front(ni, nj);
      }
    }
    rep(ii, 5) rep(jj, 5) {
      int ni = i + ii - 2;
      int nj = j + jj - 2;
      if (ni < 0 || h <= ni)
        continue;
      if (nj < 0 || w <= nj)
        continue;
      if (g[ni][nj] == '#')
        continue;
      if (d[ni][nj] > d[i][j] + 1) {
        d[ni][nj] = d[i][j] + 1;
        dq.emplace_back(ni, nj);
      }
    }
  }
  return d;
}

int main() {
  ios_base::sync_with_stdio(0);
  // finput("./input");
  cin >> h >> w;
  int ch, cw;
  cin >> ch >> cw;
  int dh, dw;
  cin >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  g = vector<string>(h);
  rep(i, h) cin >> g[i];

  auto d = bfs(ch, cw);
  if (d[dh][dw] == INF) {
    cout << -1 << endl;
  } else {
    cout << d[dh][dw] << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

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

#define REP(i, m, n) for (int i = (m); i < (int)(n); i++)
#define RREP(i, m, n) for (int i = (int)((n)-1); i >= m; i--)
#define rep(i, n) REP(i, 0, n)
#define rrep(i, n) RREP(i, 0, n)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define fi first
#define se second
#define debug(...)                                                             \\
  {                                                                            \\
    cerr << "[L" << __LINE__ << "] ";                                          \\
    _debug(__VA_ARGS__);                                                       \\
  }

template <typename T> string join(const vector<T> &v, string del = ", ") {
  stringstream s;
  for (auto x : v)
    s << del << x;
  return s.str().substr(del.size());
}
template <typename T> ostream &operator<<(ostream &o, const vector<T> &v) {
  if (v.size())
    o << "[" << join(v) << "]";
  return o;
}
template <typename T>
ostream &operator<<(ostream &o, const vector<vector<T>> &vv) {
  int l = vv.size();
  if (l) {
    o << endl;
    rep(i, l) o << (i == 0 ? "[ " : ",\\n  ") << vv[i]
                << (i == l - 1 ? " ]" : "");
  }
  return o;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &o, const pair<T1, T2> &p) {
  return o << "(" << p.first << ", " << p.second << ")";
}
inline void _debug() { cerr << endl; }
template <class First, class... Rest>
void _debug(const First &first, const Rest &...rest) {
  cerr << first << " ";
  _debug(rest...);
}

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;

const double PI = (1 * acos(0.0));
const double EPS = 1e-9;
const int INF = 0x3f3f3f3f;
const ll INFL = 0x3f3f3f3f3f3f3f3fLL;
const ll mod = 1e9 + 7;

inline void finput(string filename) { freopen(filename.c_str(), "r", stdin); }

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

int h, w;
vector<string> g;
vvi d;

int m = 0;
vvi g2;
vi d2;
void dfs(int i, int j) {
  rep(k, 4) {
    int ni = i + di[k];
    int nj = j + dj[k];
    if (ni < 0 || h <= ni)
      continue;
    if (nj < 0 || w <= nj)
      continue;
    if (g[ni][nj] == '#')
      continue;
    if (d[ni][nj] > d[i][j]) {
      d[ni][nj] = d[i][j];
      dfs(ni, nj);
    }
  }
}

void dfs2(int v) {
  rep(u, m) {
    if (g2[v][u] && d2[u] > d2[v] + 1) {
      d2[u] = d2[v] + 1;
      dfs2(u);
    }
  }
}

int main() {
  ios_base::sync_with_stdio(0);
  // finput("./input");
  cin >> h >> w;
  int ch, cw;
  cin >> ch >> cw;
  int dh, dw;
  cin >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  d = vvi(h, vi(w, INF));
  g = vector<string>(h);
  rep(i, h) cin >> g[i];

  m = 0;
  rep(i, h) rep(j, w) {
    if (g[i][j] == '.' && d[i][j] == INF) {
      d[i][j] = m++;
      dfs(i, j);
    }
  }

  g2 = vvi(m, vi(m, 0));
  d2 = vi(m, INF);
  rep(i, h) rep(j, w) {
    if (g[i][j] == '#')
      continue;
    rep(ii, 5) rep(jj, 5) {
      int ni = i + ii - 2;
      int nj = j + jj - 2;
      if (ni < 0 || h <= ni)
        continue;
      if (nj < 0 || w <= nj)
        continue;
      if (g[ni][nj] == '#')
        continue;
      if (d[i][j] != d[ni][nj]) {
        g2[d[i][j]][d[ni][nj]] = 1;
        g2[d[ni][nj]][d[i][j]] = 1;
      }
    }
  }
  d2[d[ch][cw]] = 0;
  dfs2(d[ch][cw]);

  if (d2[d[dh][dw]] == INF) {
    cout << -1 << endl;
  } else {
    cout << d2[d[dh][dw]] << endl;
  }
  return 0;
}