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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
#define ll long long
const int maxn = 1010;
const int maxN = 3000100;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};
int n, m;
int s1, s2, t1, t2;
char mp[maxn][maxn];
int bel[maxn][maxn], tot;
int dfsvis[maxn][maxn];
int nx, ny;
void dfs(int x, int y) {
  bel[x][y] = tot;
  dfsvis[x][y] = 1;
  for (int i = 0; i < 4; ++i) {
    nx = x + dx[i];
    ny = y + dy[i];
    if (nx <= 0 || nx > n || ny <= 0 || ny > m)
      continue;
    if (mp[nx][ny] == '#')
      continue;
    if (dfsvis[nx][ny])
      continue;
    dfs(nx, ny);
  }
}
vector<int> avi[maxN];
int vis[maxN], dis[maxN];
int main() {
  scanf("%d%d", &n, &m);
  scanf("%d%d", &s1, &s2);
  scanf("%d%d", &t1, &t2);
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      mp[i][j] = getchar();
      while (mp[i][j] != '#' && mp[i][j] != '.')
        mp[i][j] = getchar();
    }
  }
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      if (mp[i][j] == '.' && !bel[i][j]) {
        ++tot;
        dfs(i, j);
      }
    }
  }

  /*for (int i=1;i<=n;++i){
      for (int j=1;j<=m;++j){
          cout<<bel[i][j];
      }
      cout<<"\\n";
  }*/

  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      for (int k = -2; k <= 2; ++k) {
        nx = i + k;
        if (nx <= 0 || nx > n)
          continue;
        for (int l = -2; l <= 2; ++l) {
          ny = j + l;
          if (ny <= 0 || ny > m)
            continue;
          if (mp[nx][ny] == '#' || mp[i][j] == '#')
            continue;
          // cout<<bel[i][j]<<" "<<bel[nx][ny]<<endl;
          avi[bel[i][j]].push_back(bel[nx][ny]);
          avi[bel[nx][ny]].push_back(bel[i][j]);
        }
      }
    }
  }
  memset(vis, 0, sizeof(vis));
  queue<int> q;
  q.push(bel[s1][s2]);
  dis[bel[s1][s2]] = 0;
  vis[bel[s1][s2]] = 1;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    // cout<<u<<endl;
    for (int i = 0; i < avi[u].size(); ++i) {
      int v = avi[u][i];
      if (vis[v])
        continue;
      vis[v] = 1;
      dis[v] = dis[u] + 1;
      q.push(v);
      // cout<<v<<"="<<u<<"+1="<<dis[u]+1<<"\\n";
    }
  }
  if (!vis[bel[t1][t2]]) {
    cout << "-1";
  } else {
    cout << dis[bel[t1][t2]];
  }
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
#define ll long long
const int maxn = 1010;
const int maxN = 3000100;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};
int n, m;
int s1, s2, t1, t2;
char mp[maxn][maxn];
int bel[maxn][maxn], tot;
int dfsvis[maxn][maxn];
int nx, ny;
void dfs(int x, int y) {
  bel[x][y] = tot;
  dfsvis[x][y] = 1;
  for (int i = 0; i < 4; ++i) {
    nx = x + dx[i];
    ny = y + dy[i];
    if (nx <= 0 || nx > n || ny <= 0 || ny > m)
      continue;
    if (mp[nx][ny] == '#')
      continue;
    if (dfsvis[nx][ny])
      continue;
    dfs(nx, ny);
  }
}
int avi[maxn][maxn];
int vis[maxN], dis[maxN];
int main() {
  scanf("%d%d", &n, &m);
  scanf("%d%d", &s1, &s2);
  scanf("%d%d", &t1, &t2);
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      mp[i][j] = getchar();
      while (mp[i][j] != '#' && mp[i][j] != '.')
        mp[i][j] = getchar();
    }
  }
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      if (mp[i][j] == '.' && !bel[i][j]) {
        ++tot;
        dfs(i, j);
      }
    }
  }
  int nx, ny;
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      for (int k = -2; k <= 2; ++k) {
        nx = i + k;
        if (nx <= 0 || nx > n)
          continue;
        for (int l = -2; l <= 2; ++l) {
          ny = j + l;
          if (ny <= 0 || ny > m)
            continue;
          if (mp[nx][ny] == '#')
            continue;
          if (bel[i][j] == bel[nx][ny])
            continue;
          avi[bel[i][j]][bel[nx][ny]] = 1;
          avi[bel[nx][ny]][bel[i][j]] = 1;
        }
      }
    }
  }
  memset(vis, 0, sizeof(vis));
  queue<int> q;
  q.push(bel[s1][s2]);
  dis[bel[s1][s2]] = 0;
  vis[bel[s1][s2]] = 1;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    // cout<<u<<endl;
    for (int i = 1; i <= tot; ++i) {
      if (vis[i] || i == u)
        continue;
      if (!avi[u][i])
        continue;
      vis[i] = 1;
      dis[i] = dis[u] + 1;
      q.push(i);
    }
  }
  if (!vis[bel[t1][t2]]) {
    cout << "-1";
  } else {
    cout << dis[bel[t1][t2]];
  }
  return 0;
}