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

#include <bits/stdc++.h>
#define FOR(i, a, b) for (register int i = (a); i < (b); ++i)
#define ROF(i, a, b) for (register int i = (a); i >= (b); --i)
#define pi pair<int, int>
#define mk(a, b) make_pair(a, b)
#define mygc(c) (c) = getchar()
#define mypc(c) putchar(c)
#define fi first
#define se second
#define ls(x) t[x].lson
#define rs(x) t[x].rson
using namespace std;
typedef long long ll;
typedef double db;
const int maxn = 1005;
const int maxm = 1000005;
const int inf = 2147483647;
typedef long long ll;
const double eps = 1e-9;
const long long INF = 9223372036854775807ll;
ll qpow(ll a, ll b, ll c) {
  ll ans = 1;
  while (b) {
    if (b & 1)
      ans = ans * a % c;
    a = a * a % c;
    b >>= 1;
  }
  return ans;
}
inline void rd(int *x) {
  int k, m = 0;
  *x = 0;
  for (;;) {
    mygc(k);
    if (k == '-') {
      m = 1;
      break;
    }
    if ('0' <= k && k <= '9') {
      *x = k - '0';
      break;
    }
  }
  for (;;) {
    mygc(k);
    if (k < '0' || k > '9')
      break;
    *x = (*x) * 10 + k - '0';
  }
  if (m)
    (*x) = -(*x);
}
inline void rd(ll *x) {
  int k, m = 0;
  *x = 0;
  for (;;) {
    mygc(k);
    if (k == '-') {
      m = 1;
      break;
    }
    if ('0' <= k && k <= '9') {
      *x = k - '0';
      break;
    }
  }
  for (;;) {
    mygc(k);
    if (k < '0' || k > '9')
      break;
    *x = (*x) * 10 + k - '0';
  }
  if (m)
    (*x) = -(*x);
}
inline void rd(db *x) { scanf("%lf", x); }
inline int rd(char c[]) {
  int i, s = 0;
  for (;;) {
    mygc(i);
    if (i != ' ' && i != '\\n' && i != '\\r' && i != '\\t' && i != EOF)
      break;
  }
  c[s++] = i;
  for (;;) {
    mygc(i);
    if (i == ' ' || i == '\\n' || i == '\\r' || i == '\\t' || i == EOF)
      break;
    c[s++] = i;
  }
  c[s] = '\\0';
  return s;
}
inline void rd(int a[], int n) { FOR(i, 0, n) rd(&a[i]); }
inline void rd(ll a[], int n) { FOR(i, 0, n) rd(&a[i]); }
template <class T, class S> inline void rd(T *x, S *y) {
  rd(x);
  rd(y);
}
template <class T, class S, class U> inline void rd(T *x, S *y, U *z) {
  rd(x);
  rd(y);
  rd(z);
}
template <class T, class S, class U, class V>
inline void rd(T *x, S *y, U *z, V *w) {
  rd(x);
  rd(y);
  rd(z);
  rd(w);
}
inline void wr(int x) {
  if (x < 10)
    putchar('0' + x);
  else
    wr(x / 10), wr(x % 10);
}
inline void wr(int x, char c) {
  int s = 0, m = 0;
  char f[10];
  if (x < 0)
    m = 1, x = -x;
  while (x)
    f[s++] = x % 10, x /= 10;
  if (!s)
    f[s++] = 0;
  if (m)
    mypc('-');
  while (s--)
    mypc(f[s] + '0');
  mypc(c);
}
inline void wr(ll x, char c) {
  int s = 0, m = 0;
  char f[20];
  if (x < 0)
    m = 1, x = -x;
  while (x)
    f[s++] = x % 10, x /= 10;
  if (!s)
    f[s++] = 0;
  if (m)
    mypc('-');
  while (s--)
    mypc(f[s] + '0');
  mypc(c);
}
inline void wr(db x, char c) {
  printf("%.15f", x);
  mypc(c);
}
inline void wr(const char c[]) {
  int i;
  for (i = 0; c[i] != '\\0'; i++)
    mypc(c[i]);
}
inline void wr(const char x[], char c) {
  int i;
  for (i = 0; x[i] != '\\0'; i++)
    mypc(x[i]);
  mypc(c);
}
template <class T> inline void wrn(T x) { wr(x, '\\n'); }
template <class T, class S> inline void wrn(T x, S y) {
  wr(x, ' ');
  wr(y, '\\n');
}
template <class T, class S, class U> inline void wrn(T x, S y, U z) {
  wr(x, ' ');
  wr(y, ' ');
  wr(z, '\\n');
}
template <class T, class S, class U, class H>
inline void wrn(T x, S y, U z, H h) {
  wr(x, ' ');
  wr(y, ' ');
  wr(z, ' ');
  wr(h, '\\n');
}
template <class T> inline void wra(T x[], int n) {
  int i;
  if (!n) {
    mypc('\\n');
    return;
  }
  FOR(i, 0, n - 1) wr(x[i], ' ');
  wr(x[n - 1], '\\n');
}
int n, m, sr, sc, tr, tc, id[maxn][maxn], tot,
    dr[4] = {-1, 1, 0, 0}, dc[4] = {0, 0, 1, -1}, vis[maxm], d[maxm], inq[maxm];
vector<pi> g[maxm];
char s[maxn][maxn];
void dfs(int r, int c, int col) {
  id[r][c] = col;
  g[col].push_back(mk(r, c));
  FOR(i, 0, 4) {
    int rr = r + dr[i], cc = c + dc[i];
    if (!rr || !cc || rr > n || cc > m || id[rr][cc] || s[rr][cc] == '#')
      continue;
    dfs(rr, cc, col);
  }
}
queue<int> q;
void fd(int u) {
  FOR(i, 0, g[u].size()) {
    int r = g[u][i].fi, c = g[u][i].se;
    FOR(drr, -2, 3)
    FOR(dcc, -2, 3) {
      if (!drr && !dcc)
        continue;
      int rr = r + drr, cc = c + dcc;
      if (rr <= 0 || cc <= 0 || rr > n || cc > m || vis[id[rr][cc]] ||
          s[rr][cc] == '#' || d[id[rr][cc]] <= d[u] + 1)
        continue;
      d[id[rr][cc]] = min(d[id[rr][cc]], d[u] + 1);
      if (!inq[id[rr][cc]])
        q.push(id[rr][cc]);
      inq[id[rr][cc]] = 1;
    }
  }
}
int main() {
  rd(&n, &m);
  rd(&sr, &sc, &tr, &tc);
  FOR(i, 1, n + 1) rd(s[i] + 1);
  FOR(i, 1, n + 1)
  FOR(j, 1, m + 1) {
    if (!id[i][j] && s[i][j] != '#')
      dfs(i, j, ++tot);
  }
  FOR(i, 1, tot + 1) d[i] = inf / 2;
  d[id[sr][sc]] = 0;
  q.push(id[sr][sc]);
  while (!q.empty()) {
    vis[q.front()] = 1;
    fd(q.front());
    q.pop();
  }
  if (d[id[tr][tc]] == inf / 2)
    wrn(-1);
  else
    wrn(d[id[tr][tc]]);
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define FOR(i, a, b) for (register int i = (a); i < (b); ++i)
#define ROF(i, a, b) for (register int i = (a); i >= (b); --i)
#define pi pair<int, int>
#define mk(a, b) make_pair(a, b)
#define mygc(c) (c) = getchar()
#define mypc(c) putchar(c)
#define fi first
#define se second
#define ls(x) t[x].lson
#define rs(x) t[x].rson
using namespace std;
typedef long long ll;
typedef double db;
const int maxn = 1005;
const int maxm = 1000005;
const int inf = 2147483647;
typedef long long ll;
const double eps = 1e-9;
const long long INF = 9223372036854775807ll;
ll qpow(ll a, ll b, ll c) {
  ll ans = 1;
  while (b) {
    if (b & 1)
      ans = ans * a % c;
    a = a * a % c;
    b >>= 1;
  }
  return ans;
}
inline void rd(int *x) {
  int k, m = 0;
  *x = 0;
  for (;;) {
    mygc(k);
    if (k == '-') {
      m = 1;
      break;
    }
    if ('0' <= k && k <= '9') {
      *x = k - '0';
      break;
    }
  }
  for (;;) {
    mygc(k);
    if (k < '0' || k > '9')
      break;
    *x = (*x) * 10 + k - '0';
  }
  if (m)
    (*x) = -(*x);
}
inline void rd(ll *x) {
  int k, m = 0;
  *x = 0;
  for (;;) {
    mygc(k);
    if (k == '-') {
      m = 1;
      break;
    }
    if ('0' <= k && k <= '9') {
      *x = k - '0';
      break;
    }
  }
  for (;;) {
    mygc(k);
    if (k < '0' || k > '9')
      break;
    *x = (*x) * 10 + k - '0';
  }
  if (m)
    (*x) = -(*x);
}
inline void rd(db *x) { scanf("%lf", x); }
inline int rd(char c[]) {
  int i, s = 0;
  for (;;) {
    mygc(i);
    if (i != ' ' && i != '\\n' && i != '\\r' && i != '\\t' && i != EOF)
      break;
  }
  c[s++] = i;
  for (;;) {
    mygc(i);
    if (i == ' ' || i == '\\n' || i == '\\r' || i == '\\t' || i == EOF)
      break;
    c[s++] = i;
  }
  c[s] = '\\0';
  return s;
}
inline void rd(int a[], int n) { FOR(i, 0, n) rd(&a[i]); }
inline void rd(ll a[], int n) { FOR(i, 0, n) rd(&a[i]); }
template <class T, class S> inline void rd(T *x, S *y) {
  rd(x);
  rd(y);
}
template <class T, class S, class U> inline void rd(T *x, S *y, U *z) {
  rd(x);
  rd(y);
  rd(z);
}
template <class T, class S, class U, class V>
inline void rd(T *x, S *y, U *z, V *w) {
  rd(x);
  rd(y);
  rd(z);
  rd(w);
}
inline void wr(int x) {
  if (x < 10)
    putchar('0' + x);
  else
    wr(x / 10), wr(x % 10);
}
inline void wr(int x, char c) {
  int s = 0, m = 0;
  char f[10];
  if (x < 0)
    m = 1, x = -x;
  while (x)
    f[s++] = x % 10, x /= 10;
  if (!s)
    f[s++] = 0;
  if (m)
    mypc('-');
  while (s--)
    mypc(f[s] + '0');
  mypc(c);
}
inline void wr(ll x, char c) {
  int s = 0, m = 0;
  char f[20];
  if (x < 0)
    m = 1, x = -x;
  while (x)
    f[s++] = x % 10, x /= 10;
  if (!s)
    f[s++] = 0;
  if (m)
    mypc('-');
  while (s--)
    mypc(f[s] + '0');
  mypc(c);
}
inline void wr(db x, char c) {
  printf("%.15f", x);
  mypc(c);
}
inline void wr(const char c[]) {
  int i;
  for (i = 0; c[i] != '\\0'; i++)
    mypc(c[i]);
}
inline void wr(const char x[], char c) {
  int i;
  for (i = 0; x[i] != '\\0'; i++)
    mypc(x[i]);
  mypc(c);
}
template <class T> inline void wrn(T x) { wr(x, '\\n'); }
template <class T, class S> inline void wrn(T x, S y) {
  wr(x, ' ');
  wr(y, '\\n');
}
template <class T, class S, class U> inline void wrn(T x, S y, U z) {
  wr(x, ' ');
  wr(y, ' ');
  wr(z, '\\n');
}
template <class T, class S, class U, class H>
inline void wrn(T x, S y, U z, H h) {
  wr(x, ' ');
  wr(y, ' ');
  wr(z, ' ');
  wr(h, '\\n');
}
template <class T> inline void wra(T x[], int n) {
  int i;
  if (!n) {
    mypc('\\n');
    return;
  }
  FOR(i, 0, n - 1) wr(x[i], ' ');
  wr(x[n - 1], '\\n');
}
int n, m, sr, sc, tr, tc, id[maxn][maxn], tot,
    dr[4] = {-1, 1, 0, 0}, dc[4] = {0, 0, 1, -1}, vis[maxn], d[maxn], inq[maxn];
vector<pi> g[maxn];
char s[maxn][maxn];
void dfs(int r, int c, int col) {
  id[r][c] = col;
  g[col].push_back(mk(r, c));
  FOR(i, 0, 4) {
    int rr = r + dr[i], cc = c + dc[i];
    if (!rr || !cc || rr > n || cc > m || id[rr][cc] || s[rr][cc] == '#')
      continue;
    dfs(rr, cc, col);
  }
}
queue<int> q;
void fd(int u) {
  FOR(i, 0, g[u].size()) {
    int r = g[u][i].fi, c = g[u][i].se;
    FOR(drr, -2, 3)
    FOR(dcc, -2, 3) {
      if (!drr && !dcc)
        continue;
      int rr = r + drr, cc = c + dcc;
      if (rr <= 0 || cc <= 0 || rr > n || cc > m || vis[id[rr][cc]] ||
          s[rr][cc] == '#' || d[id[rr][cc]] <= d[u] + 1)
        continue;
      d[id[rr][cc]] = min(d[id[rr][cc]], d[u] + 1);
      if (!inq[id[rr][cc]])
        q.push(id[rr][cc]);
      inq[id[rr][cc]] = 1;
    }
  }
}
int main() {
  rd(&n, &m);
  rd(&sr, &sc, &tr, &tc);
  FOR(i, 1, n + 1) rd(s[i] + 1);
  FOR(i, 1, n + 1)
  FOR(j, 1, m + 1) {
    if (!id[i][j] && s[i][j] != '#')
      dfs(i, j, ++tot);
  }
  FOR(i, 1, tot + 1) d[i] = inf / 2;
  d[id[sr][sc]] = 0;
  q.push(id[sr][sc]);
  while (!q.empty()) {
    vis[q.front()] = 1;
    fd(q.front());
    q.pop();
  }
  if (d[id[tr][tc]] == inf / 2)
    wrn(-1);
  else
    wrn(d[id[tr][tc]]);
}