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

// g++  7.4.0

#include <bits/stdc++.h>
#include <iostream>
using namespace std;
typedef int ll;
#define MAXN 1005
ll sx, sy, dx, dy, N, M;
ll vis[MAXN][MAXN];
ll ddx[] = {1, -1, 0, 0};
ll ddy[] = {0, 0, 1, -1};
map<pair<ll, ll>, ll> mp;

bool inBounds(ll x, ll y) { return (x >= 0 && x < N && y >= 0 && y < M); }

void DFS(ll x, ll y, string s[], ll c) {
  vis[x][y] = c;
  for (ll i = 0; i < 4; ++i) {
    ll nx = x + ddx[i];
    ll ny = y + ddy[i];
    if (inBounds(nx, ny) && !vis[nx][ny] && (s[nx][ny] == '.'))
      DFS(nx, ny, s, c);
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  cin >> N >> M;
  cin >> sx >> sy >> dx >> dy;
  string s[N];

  for (ll i = 0; i < N; ++i)
    cin >> s[i];

  /*for(ll i=0;i<N;++i)
      cout<<s[i]<<endl;*/

  ll c = 1;
  for (ll i = 0; i < N; ++i) {
    for (ll j = 0; j < M; ++j) {
      if (!vis[i][j] && (s[i][j] == '.'))
        DFS(i, j, s, c++);
    }
  }

  vector<ll> adj[c];
  ll vis2[c] = {0};
  ll dis[c] = {0};

  for (ll i = 0; i < N; ++i) {
    for (ll j = 0; j < M; ++j) {
      if (s[i][j] == '#')
        continue;

      for (ll a = (-2); a <= 2; ++a) {
        for (ll b = (-2); b <= 2; ++b) {
          if (inBounds(i + a, j + b) && (s[i + a][j + b] == '.') &&
              (vis[i][j] != vis[i + a][j + b]) &&
              (mp[{vis[i][j], vis[i + a][j + b]}] == 0))
            adj[vis[i][j]].push_back(vis[i + a][j + b]);
        }
      }
    }
  }

  ll src = vis[sx - 1][sy - 1];
  ll des = vis[dx - 1][dy - 1];

  // cout<<src<<" "<<des<<endl;
  if (src == 0 || des == 0) {
    cout << -1 << endl;
    return (0);
  }

  queue<ll> q;
  q.push(src);
  vis2[src] = 1;

  while (!q.empty()) {
    ll val = q.front();
    q.pop();

    for (auto it : adj[val]) {
      if (!vis2[it]) {
        dis[it] = dis[val] + 1;
        q.push(it);
        vis2[it] = 1;
      }
    }
  }

  if (!vis2[des])
    cout << -1 << endl;
  else
    cout << dis[des] << endl;
}

Error: 0

Fixed submission:

// g++  7.4.0

#include <bits/stdc++.h>
#include <iostream>
using namespace std;
typedef int ll;
#define MAXN 1005
ll sx, sy, dx, dy, N, M;
ll vis[MAXN][MAXN];
ll ddx[] = {1, -1, 0, 0};
ll ddy[] = {0, 0, 1, -1};
vector<ll> adj[MAXN * MAXN];
ll vis2[MAXN];
ll dis[MAXN];
map<pair<ll, ll>, ll> mp;

bool inBounds(ll x, ll y) { return (x >= 0 && x < N && y >= 0 && y < M); }

void DFS(ll x, ll y, string s[], ll c) {
  vis[x][y] = c;
  for (ll i = 0; i < 4; ++i) {
    ll nx = x + ddx[i];
    ll ny = y + ddy[i];
    if (inBounds(nx, ny) && !vis[nx][ny] && (s[nx][ny] == '.'))
      DFS(nx, ny, s, c);
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  cin >> N >> M;
  cin >> sx >> sy >> dx >> dy;
  string s[N];

  for (ll i = 0; i < N; ++i)
    cin >> s[i];

  /*for(ll i=0;i<N;++i)
      cout<<s[i]<<endl;*/

  ll c = 1;
  for (ll i = 0; i < N; ++i) {
    for (ll j = 0; j < M; ++j) {
      if (!vis[i][j] && (s[i][j] == '.'))
        DFS(i, j, s, c++);
    }
  }

  for (ll i = 0; i < N; ++i) {
    for (ll j = 0; j < M; ++j) {
      if (s[i][j] == '#')
        continue;

      for (ll a = (-2); a <= 2; ++a) {
        for (ll b = (-2); b <= 2; ++b) {
          if (inBounds(i + a, j + b) && (s[i + a][j + b] == '.') &&
              (vis[i][j] != vis[i + a][j + b]) &&
              (mp[{vis[i][j], vis[i + a][j + b]}] == 0))
            adj[vis[i][j]].push_back(vis[i + a][j + b]);
        }
      }
    }
  }

  ll src = vis[sx - 1][sy - 1];
  ll des = vis[dx - 1][dy - 1];

  // cout<<src<<" "<<des<<endl;
  if (src == 0 || des == 0) {
    cout << -1 << endl;
    return (0);
  }

  queue<ll> q;
  q.push(src);
  vis2[src] = 1;

  while (!q.empty()) {
    ll val = q.front();
    q.pop();

    for (auto it : adj[val]) {
      if (!vis2[it]) {
        dis[it] = dis[val] + 1;
        q.push(it);
        vis2[it] = 1;
      }
    }
  }

  if (!vis2[des])
    cout << -1 << endl;
  else
    cout << dis[des] << endl;
}