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

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
string S[1000];
int area_id[1000][1000];
int move_h[4] = {-1, 1, 0, 0};
int move_w[4] = {0, 0, -1, 1};
void dfs(int cur_h, int cur_w, int id, int H, int W) {
  area_id[cur_h][cur_w] = id;

  for (int i = 0; i < 4; i++) {
    int next_h = cur_h + move_h[i];
    int next_w = cur_w + move_w[i];
    if (0 <= next_h && next_h < H && 0 <= next_w && next_w < W &&
        area_id[next_h][next_w] < 0 && S[next_h][next_w] != '#') {
      dfs(next_h, next_w, id, H, W);
    }
  }
}

int d[1000000];
set<int> edges[1000000];

int main() {
  int H;
  int W;
  cin >> H >> W;

  int C_h;
  int D_h;
  int C_w;
  int D_w;

  cin >> C_h >> C_w;
  cin >> D_h >> D_w;
  C_h--;
  C_w--;
  D_h--;
  D_w--;

  for (int i = 0; i < H; i++) {
    cin >> S[i];
  }

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      area_id[i][j] = -1;
    }
  }

  int total_id = 0;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (area_id[i][j] < 0 && S[i][j] != '#') {
        dfs(i, j, total_id, H, W);
        total_id++;
      }
    }
  }

  for (int i = 0; i < total_id; i++) {
    d[i] = INT_MAX;
  }

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (S[i][j] == '#') {
        continue;
      }
      int cur_id = area_id[i][j];
      for (int k = -2; k <= 2; k++) {
        for (int l = -2; l <= 2; l++) {
          int adj_h = i + k;
          int adj_w = j + l;
          if (0 <= adj_h && adj_h < H && 0 <= adj_w && adj_w < W &&
              area_id[adj_h][adj_w] != cur_id && S[adj_h][adj_w] != '#') {
            edges[cur_id].insert(area_id[adj_h][adj_w]);
          }
        }
      }
    }
  }

  d[area_id[C_h][C_w]] = 0;
  queue<int> q;
  q.push(area_id[C_h][C_w]);
  while (!q.empty()) {
    int cur = q.front();
    q.pop();
    for (auto it = edges[cur].begin(); it != edges[cur].end(); it++) {
      int next = *it;
      if (d[next] == INT_MAX) {
        d[next] = 1 + d[cur];
        q.push(next);
      }
    }
  }

  if (d[area_id[D_h][D_w]] == INT_MAX) {
    cout << -1 << endl;
  } else {
    cout << d[area_id[D_h][D_w]] << endl;
  }

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
string S[1000];
int area_id[1000][1000];
int move_h[4] = {-1, 1, 0, 0};
int move_w[4] = {0, 0, -1, 1};
void dfs(int cur_h, int cur_w, int id, int H, int W) {
  area_id[cur_h][cur_w] = id;

  for (int i = 0; i < 4; i++) {
    int next_h = cur_h + move_h[i];
    int next_w = cur_w + move_w[i];
    if (0 <= next_h && next_h < H && 0 <= next_w && next_w < W &&
        area_id[next_h][next_w] < 0 && S[next_h][next_w] != '#') {
      dfs(next_h, next_w, id, H, W);
    }
  }
}

int d[1000000];
vector<int> edges[1000000];

int main() {
  int H;
  int W;
  cin >> H >> W;

  int C_h;
  int D_h;
  int C_w;
  int D_w;

  cin >> C_h >> C_w;
  cin >> D_h >> D_w;
  C_h--;
  C_w--;
  D_h--;
  D_w--;

  for (int i = 0; i < H; i++) {
    cin >> S[i];
  }

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      area_id[i][j] = -1;
    }
  }

  int total_id = 0;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (area_id[i][j] < 0 && S[i][j] != '#') {
        dfs(i, j, total_id, H, W);
        total_id++;
      }
    }
  }

  for (int i = 0; i < total_id; i++) {
    d[i] = INT_MAX;
  }

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      int cur_id = area_id[i][j];
      for (int k = -2; k <= 2; k++) {
        for (int l = -2; l <= 2; l++) {
          int adj_h = i + k;
          int adj_w = j + l;
          if (0 <= adj_h && adj_h < H && 0 <= adj_w && adj_w < W &&
              area_id[adj_h][adj_w] != cur_id) {
            edges[cur_id].push_back(area_id[adj_h][adj_w]);
          }
        }
      }
    }
  }

  d[area_id[C_h][C_w]] = 0;
  queue<int> q;
  q.push(area_id[C_h][C_w]);
  while (!q.empty()) {
    int cur = q.front();
    q.pop();
    for (int i = 0; i < edges[cur].size(); i++) {
      int next = edges[cur][i];
      if (d[next] == INT_MAX) {
        d[next] = 1 + d[cur];
        q.push(next);
      }
    }
  }

  if (d[area_id[D_h][D_w]] == INT_MAX) {
    cout << -1 << endl;
  } else {
    cout << d[area_id[D_h][D_w]] << endl;
  }

  return 0;
}