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

//  Mayank Agarwal
//  IIIT Hyderabad

#include <bits/stdc++.h>
using namespace std;
const long long int M = 1000000007;
#define ll long long int
#define ld double
#define pb push_back
#define mp make_pair
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 % p;
}
ll modinver(ll a, ll b) { return (power(a, 1, M) * power(b, M - 2, M)) % M; }
void SieveOfEratosthenes(int n) {
  bool prime[10005];
  memset(prime, true, sizeof(prime));

  for (int p = 2; p * p <= n; p++) {
    if (prime[p] == true) {
      for (int i = p * p; i <= n; i += p)
        prime[i] = false;
    }
  }
}
ll s1[1005][1005];
string s[1005];
ll h, w;
std::vector<ll> v[1000004];
ll dep[1000002];
ll vis[1000002];
void dfs(ll l, ll r, ll zz) {
  s1[l][r] = zz;
  s[l][r] = '1';
  if (l - 1 >= 0 && s[l - 1][r] == '.') {
    dfs(l - 1, r, zz);
  }
  if (l + 1 < h && s[l + 1][r] == '.') {
    dfs(l + 1, r, zz);
  }
  if (r - 1 >= 0 && s[l][r - 1] == '.') {
    dfs(l, r - 1, zz);
  }
  if (r + 1 < w && s[l][r + 1] == '.') {
    dfs(l, r + 1, zz);
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> h >> w;
  ll ch, dh, cw, dw;
  cin >> ch >> cw >> dh >> dw;
  for (int i = 0; i < h; ++i) {
    cin >> s[i];
  }
  for (int i = 0; i < 1002; ++i) {
    for (int j = 0; j < 1002; ++j) {
      s1[i][j] = -1;
    }
  }
  ll co = 0;
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      if (s[i][j] == '.') {
        dfs(i, j, co);
        co++;
      }
    }
  }
  set<pair<ll, ll>> ss;
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      if (s[i][j] == '#')
        continue;
      for (int k = max(i - 2, 0); k < min((ll)(i + 3), h); k++) {
        for (int l = max(j - 2, 0); l < min((ll)(j + 3), w); l++) {
          if (s1[k][l] != -1 && s1[i][j] != s1[k][l]) {
            ll x1 = s1[i][j];
            ll x2 = s1[k][l];
            ss.insert(mp(min(x1, x2), max(x1, x2)));
          }
        }
      }
    }
  }
  ll so = s1[ch - 1][cw - 1];
  ll de = s1[dh - 1][dw - 1];

  for (auto i : ss) {
    // cout<<i.first<<" "<<i.second<<'\\n';
    v[i.first].pb(i.second);
    v[i.second].pb(i.first);
  }
  queue<ll> qq;
  qq.push(so);

  for (int i = 0; i <= 1000001; ++i) {
    dep[i] = 1e9;
    vis[i] = 0;
  }
  dep[so] = 0;
  vis[so] = 1;
  while (!qq.empty()) {
    ll x = qq.front();
    qq.pop();
    for (auto i : v[x]) {
      if (vis[i] == 0) {
        dep[i] = dep[x] + 1;
        qq.push(i);
        vis[i] = 1;
      }
    }
  }
  if (dep[de] == 1e9) {
    cout << "-1" << '\\n';
  } else {
    cout << dep[de] << '\\n';
  }
}


Error: -11

Fixed submission:

//  Mayank Agarwal
//  IIIT Hyderabad

#include <bits/stdc++.h>
using namespace std;
const long long int M = 1000000007;
#define ll long long int
#define ld double
#define pb push_back
#define mp make_pair
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 % p;
}
ll modinver(ll a, ll b) { return (power(a, 1, M) * power(b, M - 2, M)) % M; }
void SieveOfEratosthenes(int n) {
  bool prime[10005];
  memset(prime, true, sizeof(prime));

  for (int p = 2; p * p <= n; p++) {
    if (prime[p] == true) {
      for (int i = p * p; i <= n; i += p)
        prime[i] = false;
    }
  }
}
string s1[1002];
ll h, w;

void dfs(ll l, ll r, ll zz) {
  s1[l][r] = zz + '0';
  if (l - 1 >= 0 && s1[l - 1][r] == '.') {
    dfs(l - 1, r, zz);
  }
  if (l + 1 < h && s1[l + 1][r] == '.') {
    dfs(l + 1, r, zz);
  }
  if (r - 1 >= 0 && s1[l][r - 1] == '.') {
    dfs(l, r - 1, zz);
  }
  if (r + 1 < w && s1[l][r + 1] == '.') {
    dfs(l, r + 1, zz);
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> h >> w;
  ll ch, dh, cw, dw;
  cin >> ch >> cw >> dh >> dw;
  string s[1002];
  for (int i = 0; i < h; ++i) {
    cin >> s[i];
    s1[i] = s[i];
  }
  ll co = 0;
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      if (s1[i][j] == '.') {
        dfs(i, j, co);
        co++;
      }
    }
  }
  set<pair<ll, ll>> ss;
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      if (s1[i][j] == '#')
        continue;
      for (int k = max(i - 2, 0); k < min((ll)(i + 3), h); k++) {
        for (int l = max(j - 2, 0); l < min((ll)(j + 3), w); l++) {
          if (s1[k][l] != '#' && s1[i][j] != s1[k][l]) {
            ll x1 = s1[i][j] - '0';
            ll x2 = s1[k][l] - '0';
            ss.insert(mp(min(x1, x2), max(x1, x2)));
          }
        }
      }
    }
  }
  ll so = s1[ch - 1][cw - 1] - '0';
  ll de = s1[dh - 1][dw - 1] - '0';
  std::vector<ll> v[1000004];
  for (auto i : ss) {
    // cout<<i.first<<" "<<i.second<<'\\n';
    v[i.first].pb(i.second);
    v[i.second].pb(i.first);
  }
  queue<ll> qq;
  qq.push(so);
  ll dep[1000002];
  ll vis[1000002];
  for (int i = 0; i <= 1000001; ++i) {
    dep[i] = 1e9;
    vis[i] = 0;
  }
  dep[so] = 0;
  vis[so] = 1;
  while (!qq.empty()) {
    ll x = qq.front();
    qq.pop();
    for (auto i : v[x]) {
      if (vis[i] == 0) {
        dep[i] = dep[x] + 1;
        qq.push(i);
        vis[i] = 1;
      }
    }
  }
  if (dep[de] == 1e9) {
    cout << "-1" << '\\n';
  } else {
    cout << dep[de] << '\\n';
  }
}