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

#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
using namespace std;
// #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
// using namespace __gnu_pbds;
#define fastio()                                                               \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define pb push_back
#define show(x) cout << (#x) << " : " << x << endl;
#define ll long long
#define ld long double
#define fill(a, val) memset(a, val, sizeof(a))
#define mp make_pair
#define ff first
#define ss second
#define pii pair<ll, ll>
#define sq(x) ((x) * (x))
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define endl "\\n"
#define int long long
#define printclock                                                             \\
  cerr << "Time : " << 1000 * (ld)clock() / (ld)CLOCKS_PER_SEC << "ms\\n";
const ll MOD = 1000 * 1000 * 1000 + 7;
const ll INF = 1ll * 1000 * 1000 * 1000 * 1000 * 1000 * 1000 + 7;
const ll MOD2 = 998244353;
const ll N = 1000 + 5;
const ll N2 = 1000 * 1000 + 5;
const ld PI = 3.141592653589793;
// template<class T> using oset=tree<T, null_type, less<T>, rb_tree_tag,
// tree_order_statistics_node_update>;
ll gcd(ll a, ll b) {
  if (!b)
    return a;
  return gcd(b, a % b);
}
ll power(ll x, ll y, ll p = LLONG_MAX) {
  ll res = 1;
  x %= p;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); }
vector<string> s;
int h, w;
bool vis[N][N];
int a[N][N];
int cur = -1;
set<int> g[N2];

void solve(int source, int dest) {

  vector<int> dist(N2, INF); // graph name = g
  priority_queue<pii, vector<pii>, greater<pii>> pq;
  pq.push(mp(0, source)); // dist,vertex
  dist[source] = 0;
  while (!pq.empty()) {
    int u = pq.top().ss;
    pq.pop();
    for (auto x : g[u]) {
      if (dist[x] > dist[u] + 1) {
        dist[x] = dist[u] + 1; // 1 can be replaced with weight
        pq.push(mp(dist[x], x));
      }
    }
  }
  cout << ((dist[dest] == INF) ? -1 : dist[dest])
       << endl; // this is for 0 - n , dist[i] contains for all from 0 to n-1
}

void dfs(int curx, int cury, int my) {
  if (curx < 0 || cury < 0 || curx >= h || cury >= w || s[curx][cury] == '#' ||
      vis[curx][cury])
    return;
  vis[curx][cury] = true;
  a[curx][cury] = my;

  dfs(curx - 1, cury, my);

  dfs(curx + 1, cury, my);

  dfs(curx, cury - 1, my);

  dfs(curx, cury + 1, my);
}
signed main() {
  fastio();
// cout<<fixed<<setprecision(20);
// CHECK for LONG LONG and LONG DOUBLE
//*comment for all except cc/cf
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif //*/
  cin >> h >> w;
  int ch, cw;
  int dh, dw;
  cin >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  s.resize(h);
  for (int i = 0; i < h; ++i)
    cin >> s[i];

  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      vis[i][j] = false;
      a[i][j] = -1;
    }
  }

  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      if (s[i][j] == '.' && !vis[i][j]) {
        dfs(i, j, ++cur);
      }
    }
  }

  int source = a[ch][cw];
  int dest = a[dh][dw];

  if (source == dest)
    cout << 0;
  else {
    for (int i = 0; i < h; ++i) {
      for (int j = 0; j < w; ++j) {
        for (int ii = -2; ii <= 2; ii++) {
          for (int jj = -2; jj <= 2; jj++) {
            int xx = i + ii;
            int yy = j + jj;
            if (xx < 0 || yy < 0 || xx >= h || yy >= w)
              continue;
            if (s[i][j] != '.' || s[xx][yy] != '.')
              continue;
            if (a[i][j] != a[xx][yy]) {
              g[a[i][j]].insert(a[xx][yy]);
              g[a[xx][yy]].insert(a[i][j]);
            }
          }
        }
      }
    }
    solve(source, dest);
  }

  printclock;
  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
using namespace std;
// #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
// using namespace __gnu_pbds;
#define fastio()                                                               \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define pb push_back
#define show(x) cout << (#x) << " : " << x << endl;
#define ll long long
#define ld long double
#define fill(a, val) memset(a, val, sizeof(a))
#define mp make_pair
#define ff first
#define ss second
#define pii pair<ll, ll>
#define sq(x) ((x) * (x))
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define endl "\\n"
#define int long long
#define printclock                                                             \\
  cerr << "Time : " << 1000 * (ld)clock() / (ld)CLOCKS_PER_SEC << "ms\\n";
const ll MOD = 1000 * 1000 * 1000 + 7;
const ll INF = 1ll * 1000 * 1000 * 1000 * 1000 * 1000 * 1000 + 7;
const ll MOD2 = 998244353;
const ll N = 1000 + 5;
const ll N2 = 1000 * 1000 + 5;
const ld PI = 3.141592653589793;
// template<class T> using oset=tree<T, null_type, less<T>, rb_tree_tag,
// tree_order_statistics_node_update>;
ll gcd(ll a, ll b) {
  if (!b)
    return a;
  return gcd(b, a % b);
}
ll power(ll x, ll y, ll p = LLONG_MAX) {
  ll res = 1;
  x %= p;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); }
vector<string> s;
int h, w;
bool vis[N][N];
int a[N][N];
int cur = -1;
set<int> g[N];

void solve(int source, int dest) {

  vector<int> dist(N2, INF); // graph name = g
  priority_queue<pii, vector<pii>, greater<pii>> pq;
  pq.push(mp(0, source)); // dist,vertex
  dist[0] = 0;
  while (!pq.empty()) {
    int u = pq.top().ss;
    pq.pop();
    for (auto x : g[u]) {
      if (dist[x] > dist[u] + 1) {
        dist[x] = dist[u] + 1; // 1 can be replaced with weight
        pq.push(mp(dist[x], x));
      }
    }
  }
  cout << ((dist[dest] == INF) ? -1 : dist[dest])
       << endl; // this is for 0 - n , dist[i] contains for all from 0 to n-1
}

void dfs(int curx, int cury, int my) {
  if (curx < 0 || cury < 0 || curx >= h || cury >= w || s[curx][cury] == '#' ||
      vis[curx][cury])
    return;
  vis[curx][cury] = true;
  a[curx][cury] = my;

  dfs(curx - 1, cury, my);

  dfs(curx + 1, cury, my);

  dfs(curx, cury - 1, my);

  dfs(curx, cury + 1, my);
}
signed main() {
  fastio();
// cout<<fixed<<setprecision(20);
// CHECK for LONG LONG and LONG DOUBLE
//*comment for all except cc/cf
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif //*/
  cin >> h >> w;
  int ch, cw;
  int dh, dw;
  cin >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  s.resize(h);
  for (int i = 0; i < h; ++i)
    cin >> s[i];

  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      vis[i][j] = false;
      a[i][j] = -1;
    }
  }

  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      if (s[i][j] == '.' && !vis[i][j]) {
        dfs(i, j, ++cur);
      }
    }
  }

  int source = a[ch][cw];
  int dest = a[dh][dw];

  // for (int i = 0; i < h; ++i)
  // {
  //     for (int j = 0; j < w; ++j)
  //     {
  //         cout<<a[i][j]<<" ";
  //     }
  //     cout<<endl;
  // }

  if (source == dest)
    cout << 0;
  else {
    for (int i = 0; i < h; ++i) {
      for (int j = 0; j < w; ++j) {
        for (int ii = -2; ii <= 2; ii++) {
          for (int jj = -2; jj <= 2; jj++) {
            int xx = i + ii;
            int yy = j + jj;
            if (xx < 0 || yy < 0 || xx >= h || yy >= w)
              continue;
            if (s[i][j] != '.' || s[xx][yy] != '.')
              continue;
            if (a[i][j] != a[xx][yy]) {
              g[a[i][j]].insert(a[xx][yy]);
              g[a[xx][yy]].insert(a[i][j]);
            }
          }
        }
      }
    }

    solve(source, dest);
  }

  printclock;
  return 0;
}