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

#include <bits/stdc++.h>
#include <iostream>
using namespace std;
typedef long long int ll;
typedef long double db;
#define pb push_back
#define mp make_pair
#define ub(v, val) upper_bound(v.begin(), v.end(), val)
#define np(str) next_permutation(str.begin(), str.end())
#define lb(v, val) lower_bound(v.begin(), v.end(), val)
#define sortv(vec) sort(vec.begin(), vec.end())
#define rev(p) reverse(p.begin(), p.end());
#define v vector
#define PI 3.1415926535
#define len length()
#define repc(i, s, e) for (ll i = s; i < e; i++)
#define fi first
#define se second
#define mset(a, val) memset(a, val, sizeof(a));
#define mt make_tuple
#define repr(i, n) for (ll i = n - 1; i >= 0; i--)
#define rep(i, n) for (ll i = 0; i < n; i++)
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
const ll M = 1e9 + 7;
ll INF = 1e18;

ll n, m;
v<v<char>> s;
v<v<ll>> used;
ll cnt = 0;
void dfs(ll i, ll j) {
  used[i][j] = cnt;
  v<pair<ll, ll>> move = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
  for (auto mv : move) {
    ll x = i + mv.fi;
    ll y = j + mv.se;
    if (x >= 0 && x < n && y >= 0 && y < m) {
      if (s[x][y] == '.' && used[x][y] == 0) {
        dfs(x, y);
      }
    }
  }
}
v<v<ll>> adj;
v<bool> vis;
int main() {
  // your code goes here
  IOS;

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ll i, j, t, k, x, y, z, N;
  cin >> n >> m;
  ll x1, x2, y1, y2;
  cin >> x1 >> y1;
  cin >> x2 >> y2;
  s.resize(n, v<char>(m));
  used.assign(n, v<ll>(m, 0));
  rep(i, n) {
    rep(j, m) { cin >> s[i][j]; }
  }
  rep(i, n) {
    rep(j, m) {
      if (used[i][j] == 0 && s[i][j] == '.') {
        cnt++;
        dfs(i, j);
      }
    }
  }
  adj.resize(cnt);
  vis.assign(cnt, false);
  map<pair<ll, ll>, ll> edge;
  rep(i, n) {
    rep(j, m) {
      if (s[i][j] == '.') {
        for (x = i - 2; x <= i + 2; x++) {
          for (y = j - 2; y <= j + 2; y++) {
            if (x >= 0 && x < n && y >= 0 && y < m) {
              if (s[x][y] == '.') {
                if (edge[{min(used[x][y], used[i][j]),
                          max(used[x][y], used[i][j])}] == 0 &&
                    used[x][y] != used[i][j]) {
                  edge[{min(used[x][y], used[i][j]),
                        max(used[x][y], used[i][j])}] = 1;
                  adj[used[x][y] - 1].pb(used[i][j] - 1);
                  adj[used[i][j] - 1].pb(used[x][y] - 1);
                }
              }
            }
          }
        }
      }
    }
  }
  ll s = used[x1 - 1][y1 - 1] - 1;
  queue<ll> q;
  v<ll> d(cnt, INF);
  d[s] = 0;
  q.push(s);
  vis[s] = true;
  while (!q.empty()) {
    ll v = q.front();
    q.pop();
    for (ll u : adj[v]) {
      if (!vis[u]) {
        vis[u] = true;
        q.push(u);
        d[u] = d[v] + 1;
      }
    }
  }
  y = used[x2 - 1][y2 - 1] - 1;
  if (d[y] == INF)
    cout << -1;
  else
    cout << d[y];

  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
#include <iostream>
using namespace std;
typedef long long int ll;
typedef long double db;
#define pb push_back
#define mp make_pair
#define ub(v, val) upper_bound(v.begin(), v.end(), val)
#define np(str) next_permutation(str.begin(), str.end())
#define lb(v, val) lower_bound(v.begin(), v.end(), val)
#define sortv(vec) sort(vec.begin(), vec.end())
#define rev(p) reverse(p.begin(), p.end());
#define v vector
#define PI 3.1415926535
#define len length()
#define repc(i, s, e) for (ll i = s; i < e; i++)
#define fi first
#define se second
#define mset(a, val) memset(a, val, sizeof(a));
#define mt make_tuple
#define repr(i, n) for (ll i = n - 1; i >= 0; i--)
#define rep(i, n) for (ll i = 0; i < n; i++)
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
const ll M = 1e9 + 7;
ll INF = 1e18;

ll n, m;
v<v<char>> s;
v<v<ll>> used;
ll cnt = 0;
void dfs(ll i, ll j) {
  used[i][j] = cnt;
  v<pair<ll, ll>> move = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
  for (auto mv : move) {
    ll x = i + mv.fi;
    ll y = j + mv.se;
    if (x >= 0 && x < n && y >= 0 && y < m) {
      if (s[x][y] == '.' && used[x][y] == 0) {
        dfs(x, y);
      }
    }
  }
}
v<v<ll>> adj;
v<bool> vis;
int main() {
  // your code goes here
  IOS;

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ll i, j, t, k, x, y, z, N;
  cin >> n >> m;
  ll x1, x2, y1, y2;
  cin >> x1 >> y1;
  cin >> x2 >> y2;
  s.resize(n, v<char>(m));
  used.assign(n, v<ll>(m, 0));
  rep(i, n) {
    rep(j, m) { cin >> s[i][j]; }
  }
  rep(i, n) {
    rep(j, m) {
      if (used[i][j] == 0 && s[i][j] == '.') {
        cnt++;
        dfs(i, j);
      }
    }
  }
  adj.resize(cnt);
  vis.assign(cnt, false);
  map<pair<ll, ll>, ll> edge;
  rep(i, n) {
    rep(j, m) {
      if (s[i][j] == '.') {
        for (x = i - 2; x <= i + 2; x++) {
          for (y = j - 2; y <= j + 2; y++) {
            if (x >= 0 && x < n && y >= 0 && y < m) {
              if (s[x][y] == '.') {
                if (edge[{min(used[x][y], used[i][j]),
                          max(used[x][y], used[i][j])}] == 0 &&
                    used[x][y] != used[i][j]) {
                  edge[{min(used[x][y], used[i][j]),
                        max(used[x][y], used[i][j])}] = 1;
                  adj[used[x][y] - 1].pb(used[i][j] - 1);
                  adj[used[i][j] - 1].pb(used[x][y] - 1);
                }
              }
            }
          }
        }
      }
    }
  }
  ll s = used[x1 - 1][y1 - 1] - 1;
  queue<ll> q;
  v<ll> d(n, INF);
  d[s] = 0;
  q.push(s);
  vis[s] = true;
  while (!q.empty()) {
    ll v = q.front();
    q.pop();
    for (ll u : adj[v]) {
      if (!vis[u]) {
        vis[u] = true;
        q.push(u);
        d[u] = d[v] + 1;
      }
    }
  }
  y = used[x2 - 1][y2 - 1] - 1;
  if (d[y] == INF)
    cout << -1;
  else
    cout << d[y];

  return 0;
}