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

#include <bits/stdc++.h>

using namespace std;

int mov[4][2] = {{-1, 0}, {+1, 0}, {0, -1}, {0, +1}};

int n, m, comp[1123][1123], done[1123456], dist[1123456], vis[1123][1123];
char mat[1123][1123];
vector<int> adjM[1123456];

void dfs(int x, int y, int f) {
  comp[x][y] = f;

  for (int i = 0; i < 4; ++i) {
    int viz_x = x + mov[i][0];
    int viz_y = y + mov[i][1];

    if (viz_x < 1 || viz_x > n || viz_y < 1 || viz_y > m)
      continue;

    if (mat[viz_x][viz_y] != '.' or comp[viz_x][viz_y] != -1)
      continue;

    dfs(viz_x, viz_y, f);
  }
}

void dfs2(int x, int y, int f) {
  vis[x][y] = 1;
  for (int i = 0; i < 4; ++i) {

    int viz_x = x + mov[i][0];
    int viz_y = y + mov[i][1];

    if (viz_x < 1 || viz_x > n || viz_y < 1 || viz_y > m)
      continue;

    if (mat[viz_x][viz_y] != '.' or comp[viz_x][viz_y] != f or
        vis[viz_x][viz_y])
      continue;

    dfs2(viz_x, viz_y, f);
  }

  for (int i = -2; i <= 2; ++i) {
    for (int j = -2; j <= 2; ++j) {
      int viz_x = x + i;
      int viz_y = y + j;

      if (viz_x < 1 || viz_x > n || viz_y < 1 || viz_y > m)
        continue;
      if (mat[viz_x][viz_y] != '.' or comp[viz_x][viz_y] == -1 or
          comp[viz_x][viz_y] == f)
        continue;

      adjM[f].push_back(comp[viz_x][viz_y]);
      adjM[comp[viz_x][viz_y]].push_back(f);
    }
  }
}

void calc_dist(int u) {
  deque<int> queue;

  queue.push_back(u);

  while (!queue.empty()) {
    int v = queue.front();
    queue.pop_front();

    for (auto w : adjM[v]) {
      if (dist[w] != -1)
        continue;

      dist[w] = dist[v] + 1;
      queue.push_back(w);
    }
  }
}

int main() {
  int ini_x, ini_y, fim_x, fim_y, cont = -1;

  scanf("%d %d", &n, &m);
  scanf("%d %d", &ini_x, &ini_y);
  scanf("%d %d", &fim_x, &fim_y);

  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      scanf(" %c", &mat[i][j]);
      comp[i][j] = -1;
    }
  }

  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      if (mat[i][j] == '.' and comp[i][j] == -1) {
        ++cont;
        dfs(i, j, cont);
      }
    }
  }

  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      if (comp[i][j] != -1 and !done[comp[i][j]]) {
        dfs2(i, j, comp[i][j]);
        done[comp[i][j]] = 1;
      }
    }
  }

  memset(dist, -1, sizeof dist);

  dist[comp[ini_x][ini_y]] = 0;
  calc_dist(comp[ini_x][ini_y]);

  printf("%d\\n", dist[comp[fim_x][fim_y]]);

  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int mov[4][2] = {{-1, 0}, {+1, 0}, {0, -1}, {0, +1}};

int n, m, comp[1123][1123], done[1123456], dist[1123456];
char mat[1123][1123];
vector<int> adjM[1123456];

void dfs(int x, int y, int f) {
  comp[x][y] = f;

  for (int i = 0; i < 4; ++i) {
    int viz_x = x + mov[i][0];
    int viz_y = y + mov[i][1];

    if (viz_x < 1 || viz_x > n || viz_y < 1 || viz_y > m)
      continue;

    if (mat[viz_x][viz_y] != '.' or comp[viz_x][viz_y] != -1)
      continue;

    dfs(viz_x, viz_y, f);
  }
}

void dfs2(int x, int y, int f) {

  for (int i = 0; i < 4; ++i) {

    int viz_x = x + mov[i][0];
    int viz_y = y + mov[i][1];

    if (viz_x < 1 || viz_x > n || viz_y < 1 || viz_y > m)
      continue;

    if (mat[viz_x][viz_y] != '.' or comp[viz_x][viz_y] != f)
      continue;

    dfs2(viz_x, viz_y, f);
  }

  for (int i = -2; i <= 2; ++i) {
    for (int j = -2; j <= 2; ++j) {
      int viz_x = x + i;
      int viz_y = y + j;

      if (viz_x < 1 || viz_x > n || viz_y < 1 || viz_y > m)
        continue;
      if (mat[viz_x][viz_y] != '.' or comp[viz_x][viz_y] == -1 or
          comp[viz_x][viz_y] == f)
        continue;

      adjM[f].push_back(comp[viz_x][viz_y]);
      adjM[comp[viz_x][viz_y]].push_back(f);
    }
  }
}

void calc_dist(int u) {
  deque<int> queue;

  queue.push_back(u);

  while (!queue.empty()) {
    int v = queue.front();
    queue.pop_front();

    for (auto w : adjM[v]) {
      if (dist[w] != -1)
        continue;
      dist[w] = dist[v] + 1;
      queue.push_back(w);
    }
  }
}

int main() {
  int ini_x, ini_y, fim_x, fim_y, cont = -1;

  scanf("%d %d", &n, &m);
  scanf("%d %d", &ini_x, &fim_x);
  scanf("%d %d", &ini_y, &fim_y);

  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      scanf(" %c", &mat[i][j]);
      comp[i][j] = -1;
    }
  }

  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      if (mat[i][j] == '.' and comp[i][j] == -1) {
        ++cont;
        dfs(i, j, cont);
      }
    }
  }

  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      if (comp[i][j] != -1 and !done[comp[i][j]]) {
        dfs2(i, j, comp[i][j]);
        done[comp[i][j]] = 1;
      }
    }
  }

  memset(dist, -1, sizeof dist);

  dist[comp[ini_x][ini_y]] = 0;
  calc_dist(comp[ini_x][ini_y]);

  printf("%d\\n", dist[comp[fim_x][fim_y]]);

  return 0;
}