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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
const int maxn = 1e3 + 5;
char maze[maxn][maxn];
int h, w;
int ch, cw, dh, dw;
int vis[maxn][maxn];
int drow[4] = {0, 0, 1, -1};
int dcol[4] = {1, -1, 0, 0};
bool inboard(int row, int col) {
  if (row >= 0 && row < h && col >= 0 && col < w)
    return true;
  return false;
}
void dfs(int row, int col, int id) {
  vis[row][col] = id;
  for (int i = 0; i < 4; ++i) {
    int nextrow = row + drow[i];
    int nextcol = col + dcol[i];
    if (inboard(nextrow, nextcol) && maze[nextrow][nextcol] == '.' &&
        vis[nextrow][nextcol] == -1)
      dfs(nextrow, nextcol, id);
  }
}
int main(void) {
  cin >> h >> w >> ch >> cw >> dh >> dw;
  for (int i = 0; i < h; ++i)
    for (int j = 0; j < w; ++j)
      cin >> maze[i][j];
  memset(vis, -1, sizeof(vis));
  int id = 0;
  for (int i = 0; i < h; ++i)
    for (int j = 0; j < w; ++j)
      if (maze[i][j] == '.' && vis[i][j] == -1)
        dfs(i, j, id), id++;
  unordered_set<int> edge[id + 1];
  for (int i = 0; i < h; ++i)
    for (int j = 0; j < w; ++j) {
      if (maze[i][j] == '#')
        continue;
      for (int p = i - 2; p <= i + 2; ++p)
        for (int q = j - 2; q <= j + 2; ++q)
          if (inboard(p, q) && maze[p][q] == '.' && vis[p][q] != vis[i][j])
            edge[vis[p][q]].insert(vis[i][j]),
                edge[vis[i][j]].insert(vis[p][q]);
    }
  int start = vis[ch - 1][cw - 1];
  int end = vis[dh - 1][dw - 1];

  queue<int> q;
  int dis[id + 1];
  bool visi[id + 1];
  memset(visi, 0, sizeof(visi));
  q.push(start);
  dis[start] = 0;
  visi[start] = true;
  while (!q.empty()) {
    int now = q.front();
    q.pop();
    for (auto it = edge[now].begin(); it != edge[now].end(); ++it)
      if (visi[*it] == false)
        visi[*it] = true, q.push(*it), dis[*it] = dis[now] + 1;
  }
  if (visi[end] == false)
    cout << -1 << endl;
  else
    cout << dis[end] << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
const int maxn = 1e3 + 5;
char maze[maxn][maxn];
int h, w;
int ch, cw, dh, dw;
int vis[maxn][maxn];
int drow[4] = {0, 0, 1, -1};
int dcol[4] = {1, -1, 0, 0};
bool inboard(int row, int col) {
  if (row >= 0 && row < h && col >= 0 && col < w)
    return true;
  return false;
}
void dfs(int row, int col, int id) {
  vis[row][col] = id;
  for (int i = 0; i < 4; ++i) {
    int nextrow = row + drow[i];
    int nextcol = col + dcol[i];
    if (inboard(nextrow, nextcol) && maze[nextrow][nextcol] == '.' &&
        vis[nextrow][nextcol] == -1)
      dfs(nextrow, nextcol, id);
  }
}
int main(void) {
  cin >> h >> w >> ch >> cw >> dh >> dw;
  for (int i = 0; i < h; ++i)
    for (int j = 0; j < w; ++j)
      cin >> maze[i][j];
  memset(vis, -1, sizeof(vis));
  int id = 0;
  for (int i = 0; i < h; ++i)
    for (int j = 0; j < w; ++j)
      if (maze[i][j] == '.' && vis[i][j] == -1)
        dfs(i, j, id), id++;
  bool edge[id + 1][id + 1];
  memset(edge, 0, sizeof(edge));
  for (int i = 0; i < h; ++i)
    for (int j = 0; j < w; ++j) {
      if (maze[i][j] == '#')
        continue;
      for (int p = i - 2; p <= i + 2; ++p)
        for (int q = j - 2; q <= j + 2; ++q)
          if (inboard(p, q) && maze[p][q] == '.' && vis[p][q] != -1 &&
              vis[i][j] != -1)
            edge[vis[p][q]][vis[i][j]] = true,
            edge[vis[i][j]][vis[p][q]] = true;
    }
  int start = vis[ch - 1][cw - 1];
  int end = vis[dh - 1][dw - 1];

  queue<int> q;
  int dis[id + 1];
  bool visi[id + 1];
  memset(visi, 0, sizeof(visi));
  q.push(start);
  dis[start] = 0;
  visi[start] = true;
  while (!q.empty()) {
    int now = q.front();
    q.pop();
    for (int i = 0; i < id; ++i)
      if (edge[now][i] && visi[i] == false)
        visi[i] = true, q.push(i), dis[i] = dis[now] + 1;
  }
  if (visi[end] == false)
    cout << -1 << endl;
  else
    cout << dis[end] << endl;
  return 0;
}