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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using ll = long long;
const ll M = 1e9 + 7;

int x[4] = {-1, 0, 1, 0};
int y[4] = {0, -1, 0, 1};

int main() {
  int h, w;
  cin >> h >> w;
  int ch, cw;
  cin >> ch >> cw;
  int dh, dw;
  cin >> dh >> dw;
  ch--, cw--, dh--, dw--;
  vector<vector<char>> s(h, vector<char>(w));
  rep(i, h) rep(j, w) cin >> s[i][j];

  vector<vector<int>> dist(h, vector<int>(w, M));
  deque<pair<int, int>> q;
  dist[ch][cw] = 0;
  q.emplace_back(ch, cw);
  while (!q.empty()) {
    int i = q.front().first;
    int j = q.front().second;
    q.pop_front();
    int d = dist[i][j];

    // コスト0の移動
    rep(v, 4) {
      int ni = i + x[v];
      int nj = j + y[v];
      if (ni < 0 || nj < 0 || ni >= h || nj >= w)
        continue;
      if (s[ni][nj] == '#')
        continue;
      if (dist[ni][nj] <= d)
        continue;
      dist[ni][nj] = d;
      q.emplace_front(ni, nj);
    }

    // コスト1の移動
    for (int ni = i - 2; ni <= i + 2; ni++) {
      for (int nj = j - 2; nj <= j + 2; nj++) {
        if (ni < 0 || nj < 0 || ni >= h || nj >= w)
          continue;
        if (s[ni][nj] == '#')
          continue;
        if (dist[ni][nj] <= d + 1)
          continue;
        dist[ni][nj] = d + 1;
        q.emplace_back(ni, nj);
      }
    }
  }

  int ans = dist[dh][dw];
  if (ans == M)
    cout << -1 << endl;
  else
    cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using ll = long long;
const ll M = 1e9 + 7;

int x[4] = {-1, 0, 1, 0};
int y[4] = {0, -1, 0, 1};

int main() {
  int h, w;
  cin >> h >> w;
  int ch, cw;
  cin >> ch >> cw;
  int dh, dw;
  cin >> dh >> dw;
  vector<vector<char>> s(h, vector<char>(w));
  rep(i, h) rep(j, w) cin >> s[i][j];

  // 領域分け
  int areas = 0;
  vector<vector<int>> A(h, vector<int>(w));
  rep(i, h) rep(j, w) A[i][j] = 0;

  rep(i, h) rep(j, w) {
    if (s[i][j] == '#')
      continue;
    if (A[i][j] != 0)
      continue;

    areas++;
    // A[i][j] = areas;
    queue<pair<int, int>> q;
    q.push(make_pair(i, j));
    while (!q.empty()) {
      pair<int, int> pt;
      pt = q.front();
      q.pop();
      int a = pt.first, b = pt.second;
      if (s[a][b] == '#')
        continue;
      if (A[a][b] != 0)
        continue;
      A[a][b] = areas;
      vector<int> ni, nj;
      rep(k, 4) {
        ni.push_back(a + y[k]);
        nj.push_back(b + x[k]);
      }
      rep(k, 4) {
        if (ni[k] < 0)
          continue;
        if (nj[k] < 0)
          continue;
        if (ni[k] >= h)
          continue;
        if (nj[k] >= w)
          continue;
        q.push(make_pair(ni[k], nj[k]));
      }
    }
  }

  // 所属判定
  int area_c = A[ch - 1][cw - 1];
  int area_d = A[dh - 1][dw - 1];

  // 最初から同じ島の場合
  if (area_c == area_d) {
    cout << 0 << endl;
    return 0;
  }

  // 隣接行列
  // 島xから島yにいける場合は1
  vector<vector<int>> r(areas, vector<int>(areas));
  rep(i, areas) rep(j, areas) r[i][j] = 0;

  rep(i, areas) {
    for (int x = 0; x < w; x++) {
      for (int y = 0; y < h; y++) {
        if (A[y][x] != i + 1)
          continue;
        for (int nx = x - 2; nx <= x + 2; nx++) {
          for (int ny = y - 2; ny <= y + 2; ny++) {
            if (nx < 0 || ny < 0 || nx >= w || ny >= h) {
              continue;
            }
            if (A[ny][nx] != i && A[ny][nx] != 0) {
              r[i][A[ny][nx] - 1] = 1;
              r[A[ny][nx] - 1][i] = 1;
            }
          }
        }
      }
    }
  }

  vector<int> dist(areas, 1e8);
  dist[area_c - 1] = 0;
  queue<int> q;
  rep(i, areas) {
    if (r[area_c - 1][i] == 1 && i != area_c - 1) {
      q.push(i + 1);
      dist[i] = 1;
    }
  }

  int count = 2;
  while (!q.empty()) {
    int p = q.front();
    q.pop();
    rep(i, areas) {
      if (r[p - 1][i] == 1 && i != p - 1) {
        if (dist[i] != 1e8)
          continue;
        q.push(i);
        dist[i] = count;
      }
      count++;
    }
  }

  // debug
  // rep(i,h){
  //   rep(j,w) {
  //     cout << A[i][j] << " ";
  //   }
  //   cout << endl;
  // }

  // cout << area_c << endl;
  // cout << area_d << endl;

  // rep(i,areas){
  //   rep(j,areas) {
  //     cout << r[i][j] << " ";
  //   }
  //   cout << endl;
  // }

  // rep(i,dist.size()){
  //   cout << dist[i] << endl;
  // }

  // cout << "---" << endl;
  // cout << dist[area_d-1] << endl;

  if (dist[area_d - 1] == 1e8) {
    cout << -1 << endl;
  } else {
    cout << dist[area_d - 1] << endl;
  }
  return 0;
}