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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const ll mod = 1e9 + 7;
#define pival 3.14159265358979323846264338327950L
#define pll pair<ll, ll>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define pqq priority_queue
#define all(a) a.begin(), a.end()
#define sz(a) (ll)(a.size())
#define endl '\\n'
#define DEBUG
#ifdef DEBUG
#define debug(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " = " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " = " << arg1 << " || ";
  __f(comma + 1, args...);
}
#else
#define debug(...)
#endif
template <class Ch, class Tr, class Container>
basic_ostream<Ch, Tr> &operator<<(basic_ostream<Ch, Tr> &os,
                                  Container const &x) {
  os << "{ ";
  for (auto &y : x)
    os << y << " ; ";
  return os << "}";
}
template <class X, class Y>
ostream &operator<<(ostream &os, pair<X, Y> const &p) {
  return os << "[ " << p.first << ", " << p.second << "]";
}
ll power(ll x, ll y, ll p) {
  ll res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
const ll L = 1e3 + 5;
ll comp[L][L];
ll vis[L][L];
ll arr[L][L];
set<pll> edges;
ll h, w;
ll st = 1;
void dfs(ll x, ll y) {
  if (x < 1 || x > h)
    return;
  if (y < 1 || y > w)
    return;
  if (vis[x][y] == 1)
    return;
  if (arr[x][y] == 1)
    return;
  vis[x][y] = 1;
  comp[x][y] = st;
  dfs(x + 1, y);
  dfs(x - 1, y);
  dfs(x, y + 1);
  dfs(x, y - 1);
}
void match(ll x, ll y, ll i, ll j) {
  if (x < 1 || x > h)
    return;
  if (y < 1 || y > w)
    return;
  if (arr[x][y] == 1)
    return;
  ll a = comp[x][y];
  ll b = comp[i][j];
  if (a == b)
    return;
  edges.insert({min(a, b), max(a, b)});
}
const ll L2 = 1e6 + 5;
vector<ll> ad[L2];
ll vis2[L2];
ll sht[L2];
void bfs(ll ind) {
  deque<ll> q;
  vis2[ind] = 1;
  q.push_front(ind);
  while (!q.empty()) {
    ll p = q.front();
    q.pop_front();
    for (ll v : ad[p]) {
      if (!vis2[v]) {
        vis2[v] = 1;
        sht[v] = sht[p] + 1;
        q.push_back(v);
      }
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> h >> w;
  ll ch, cw, dh, dw;
  cin >> ch >> cw >> dh >> dw;
  for (ll i = 1; i <= h; i++) {
    string s;
    cin >> s;
    for (ll j = 1; j <= w; j++) {
      if (s[j - 1] == '.')
        arr[i][j] = 0;
      else
        arr[i][j] = 1;
    }
  }
  ll temp = 1;
  for (ll i = 1; i <= h; i++) {
    for (ll j = 1; j <= w; j++) {
      if (!vis[i][j] && arr[i][j] == 0) {
        dfs(i, j);
        st++;
      }
    }
  }
  if (comp[ch][cw] == comp[dh][dw]) {
    cout << 0 << endl;
    return 0;
  }
  for (ll i = 1; i <= h; i++) {
    for (ll j = 1; j <= w; j++) {
      if (arr[i][j] == 0) {
        for (ll x = -2; x <= 2; x++) {
          for (ll y = -2; y <= 2; y++) {
            match(i + x, j + y, i, j);
          }
        }
      }
    }
  }
  for (ll i = 1; i <= st; i++)
    vis2[i] = 0;
  for (auto it : edges) {
    ll u = it.fi;
    ll v = it.se;
    ad[u].pb(v);
    ad[v].pb(u);
  }
  ll start = comp[ch][cw];
  bfs(start);
  ll end = comp[dh][dw];
  if (sht[end] == 0) {
    cout << -1 << endl;
  } else {
    cout << sht[end] << endl;
  }
  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const ll mod = 1e9 + 7;
#define pival 3.14159265358979323846264338327950L
#define pll pair<ll, ll>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define pqq priority_queue
#define all(a) a.begin(), a.end()
#define sz(a) (ll)(a.size())
#define endl '\\n'
#define DEBUG
#ifdef DEBUG
#define debug(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " = " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " = " << arg1 << " || ";
  __f(comma + 1, args...);
}
#else
#define debug(...)
#endif
template <class Ch, class Tr, class Container>
basic_ostream<Ch, Tr> &operator<<(basic_ostream<Ch, Tr> &os,
                                  Container const &x) {
  os << "{ ";
  for (auto &y : x)
    os << y << " ; ";
  return os << "}";
}
template <class X, class Y>
ostream &operator<<(ostream &os, pair<X, Y> const &p) {
  return os << "[ " << p.first << ", " << p.second << "]";
}
ll power(ll x, ll y, ll p) {
  ll res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
const ll L = 5e3 + 5;
ll comp[L][L];
ll vis[L][L];
ll arr[L][L];
set<pll> edges;
ll h, w;
ll st = 1;
void dfs(ll x, ll y) {
  if (x < 1 || x > h)
    return;
  if (y < 1 || y > w)
    return;
  if (vis[x][y] == 1)
    return;
  if (arr[x][y] == 1)
    return;
  vis[x][y] = 1;
  comp[x][y] = st;
  dfs(x + 1, y);
  dfs(x - 1, y);
  dfs(x, y + 1);
  dfs(x, y - 1);
}
void match(ll x, ll y, ll i, ll j) {
  if (x < 1 || x > h)
    return;
  if (y < 1 || y > w)
    return;
  if (arr[x][y] == 1)
    return;
  ll a = comp[x][y];
  ll b = comp[i][j];
  if (a == b)
    return;
  edges.insert({min(a, b), max(a, b)});
}
vector<ll> ad[L];
ll vis2[L];
ll sht[L];
void bfs(ll ind) {
  deque<ll> q;
  vis2[ind] = 1;
  q.push_front(ind);
  while (!q.empty()) {
    ll p = q.front();
    q.pop_front();
    for (ll v : ad[p]) {
      if (!vis2[v]) {
        vis2[v] = 1;
        sht[v] = sht[p] + 1;
        q.push_back(v);
      }
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> h >> w;
  ll ch, cw, dh, dw;
  cin >> ch >> cw >> dh >> dw;
  for (ll i = 1; i <= h; i++) {
    string s;
    cin >> s;
    for (ll j = 1; j <= w; j++) {
      if (s[j - 1] == '.')
        arr[i][j] = 0;
      else
        arr[i][j] = 1;
    }
  }
  ll temp = 1;
  for (ll i = 1; i <= h; i++) {
    for (ll j = 1; j <= w; j++) {
      if (!vis[i][j] && arr[i][j] == 0) {
        dfs(i, j);
        st++;
      }
    }
  }
  if (comp[ch][cw] == comp[dh][dw]) {
    cout << 0 << endl;
    return 0;
  }
  for (ll i = 1; i <= h; i++) {
    for (ll j = 1; j <= w; j++) {
      if (arr[i][j] == 0) {
        for (ll x = -2; x <= 2; x++) {
          for (ll y = -2; y <= 2; y++) {
            match(i + x, j + y, i, j);
          }
        }
      }
    }
  }
  for (ll i = 1; i < st; i++)
    vis2[i] = 0;
  for (auto it : edges) {
    ll u = it.fi;
    ll v = it.se;
    ad[u].pb(v);
    ad[v].pb(u);
  }
  ll start = comp[ch][cw];
  sht[start] = 0;
  bfs(start);
  ll end = comp[dh][dw];
  if (sht[end] == 0) {
    cout << -1 << endl;
  } else {
    cout << sht[end] << endl;
  }
  return 0;
}