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

#include <bits/stdc++.h>
#include <iostream>
using namespace std;
#define ll long long
#define ld long double
#define mp make_pair
#define pb push_back
#define fo(i, n) for (ll i = 0; i < n; i++)
#define fo1(i, n) for (ll i = 1; i <= n; i++)
#define loop(i, a, b) for (ll i = a; i <= b; i++)
#define loopr(i, a, b) for (ll i = b; i >= a; i--)
#define all(x) x.begin(), x.end()
#define sz(x) (ll)(x).size()
#define vll vector<ll>
#define vvl vector<vll>
#define vpll vector<pll>
#define pll pair<ll, ll>
#define F first
#define S second
#define MOD 1000000007
ll max(ll a, ll b) {
  if (a > b)
    return a;
  else
    return b;
}
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
ll fexp(ll a, ll b) {
  ll ans = 1;
  while (b) {
    if (b & 1)
      ans = ans * a % MOD;
    b /= 2;
    a = a * a % MOD;
  }
  return ans;
}
ll inverse(ll a, ll p) { return fexp(a, p - 2); }
ll cc;
void dfs(ll i, ll j, ll n, ll m, vvl &vi, vector<string> &mat) {
  if (i < 1 || i > n || j < 1 || j > m || vi[i][j] || mat[i][j] == '#')
    return;
  vi[i][j] = cc;
  dfs(i + 1, j, n, m, vi, mat);
  dfs(i - 1, j, n, m, vi, mat);
  dfs(i, j - 1, n, m, vi, mat);
  dfs(i, j + 1, n, m, vi, mat);
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ll T = 1;
  // cin >> T;
  ll t = 0;
  while (t++ < T) {
    ll n, m;
    cin >> n >> m;
    ll x1, y1, x2, y2;
    cin >> x1 >> y1 >> x2 >> y2;
    vector<string> mat(n + 1);
    for (ll i = 1; i <= n; i++) {
      cin >> mat[i];
      mat[i] = ' ' + mat[i];
    }
    cc = 0;
    vvl vi(n + 1, vll(m + 1, 0));
    for (ll i = 1; i <= n; i++) {
      for (ll j = 1; j <= m; j++) {
        if (mat[i][j] == '.' && !vi[i][j]) {
          cc++;
          dfs(i, j, n, m, vi, mat);
        }
      }
    }
    map<ll, map<ll, ll>> pr;
    vll adj[cc + 5];
    for (ll i = 1; i <= n; i++) {
      for (ll j = 1; j <= m; j++) {
        if (mat[i][j] == '#' || vi[i][j] == 0)
          continue;
        for (ll l = -2; l <= 2; l++) {
          for (ll r = -2; r <= 2; r++) {
            ll x = i + l, y = j + r;
            if (x < 1 || x > n || y < 1 || y > m)
              continue;
            ll cc1 = vi[i][j];
            ll cc2 = vi[x][y];
            if (cc1 == cc2 || pr[cc1][cc2] || cc2 == 0 || mat[x][y] == '#')
              continue;
            pr[cc1][cc2]++;
            pr[cc2][cc1]++;
            adj[cc1].pb(cc2);
            adj[cc2].pb(cc1);
          }
        }
      }
    }
    queue<ll> q;
    q.push(vi[x1][y1]);
    vll d(cc + 5, INT_MAX);
    d[vi[x1][y1]] = 0;
    while (!q.empty()) {
      ll u = q.front();
      q.pop();
      for (auto x : adj[u]) {
        if (d[x] == INT_MAX) {
          d[x] = d[u] + 1;
          q.push(x);
        }
      }
    }
    if (d[vi[x2][y2]] == INT_MAX)
      cout << -1 << endl;
    else
      cout << d[vi[x2][y2]] << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <iostream>
using namespace std;
#define ll long long
#define ld long double
#define mp make_pair
#define pb push_back
#define fo(i, n) for (ll i = 0; i < n; i++)
#define fo1(i, n) for (ll i = 1; i <= n; i++)
#define loop(i, a, b) for (ll i = a; i <= b; i++)
#define loopr(i, a, b) for (ll i = b; i >= a; i--)
#define all(x) x.begin(), x.end()
#define sz(x) (ll)(x).size()
#define vll vector<ll>
#define vvl vector<vll>
#define vpll vector<pll>
#define pll pair<ll, ll>
#define F first
#define S second
#define MOD 1000000007
ll max(ll a, ll b) {
  if (a > b)
    return a;
  else
    return b;
}
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
ll fexp(ll a, ll b) {
  ll ans = 1;
  while (b) {
    if (b & 1)
      ans = ans * a % MOD;
    b /= 2;
    a = a * a % MOD;
  }
  return ans;
}
ll inverse(ll a, ll p) { return fexp(a, p - 2); }
ll cc;
void dfs(ll i, ll j, ll n, ll m, vvl &vi, vector<string> &mat) {
  if (i < 1 || i > n || j < 1 || j > m || vi[i][j] || mat[i][j] == '#')
    return;
  vi[i][j] = cc;
  dfs(i + 1, j, n, m, vi, mat);
  dfs(i - 1, j, n, m, vi, mat);
  dfs(i, j - 1, n, m, vi, mat);
  dfs(i, j + 1, n, m, vi, mat);
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ll T = 1;
  // cin >> T;
  ll t = 0;
  while (t++ < T) {
    ll n, m;
    cin >> n >> m;
    ll x1, y1, x2, y2;
    cin >> x1 >> y1 >> x2 >> y2;
    vector<string> mat(n + 1);
    for (ll i = 1; i <= n; i++) {
      cin >> mat[i];
      mat[i] = ' ' + mat[i];
    }
    cc = 0;
    vvl vi(n + 1, vll(m + 1, 0));
    for (ll i = 1; i <= n; i++) {
      for (ll j = 1; j <= m; j++) {
        if (mat[i][j] == '.' && !vi[i][j]) {
          cc++;
          dfs(i, j, n, m, vi, mat);
        }
      }
    }
    vvl pr(cc + 5, vll(cc + 5, 0));
    vll adj[cc + 5];
    for (ll i = 1; i <= n; i++) {
      for (ll j = 1; j <= m; j++) {
        if (mat[i][j] == '#' || vi[i][j] == 0)
          continue;
        for (ll l = -2; l <= 2; l++) {
          for (ll r = -2; r <= 2; r++) {
            ll x = i + l, y = j + r;
            if (x < 1 || x > n || y < 1 || y > m)
              continue;
            ll cc1 = vi[i][j];
            ll cc2 = vi[x][y];
            if (cc1 == cc2 || pr[cc1][cc2] || cc2 == 0 || mat[x][y] == '#')
              continue;
            pr[cc1][cc2]++;
            adj[cc1].pb(cc2);
            adj[cc2].pb(cc1);
          }
        }
      }
    }
    queue<ll> q;
    q.push(vi[x1][y1]);
    vll d(cc + 5, INT_MAX);
    d[vi[x1][y1]] = 0;
    while (!q.empty()) {
      ll u = q.front();
      q.pop();
      for (auto x : adj[u]) {
        if (d[x] == INT_MAX) {
          d[x] = d[u] + 1;
          q.push(x);
        }
      }
    }
    if (d[vi[x2][y2]] == INT_MAX)
      cout << -1 << endl;
    else
      cout << d[vi[x2][y2]] << endl;
  }
  return 0;
}