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

#include <algorithm>
#include <array>
#include <cctype>
#include <climits>
#include <cmath>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}

// a と b の最大公約数を返す関数
long long GCD(long long a, long long b) {
  if (b == 0)
    return a;
  else
    return GCD(b, a % b);
}

long long gcd(long long a, long long b) {
  if (a % b == 0) {
    return (b);
  } else {
    return (gcd(b, a % b));
  }
}

long long lcm(long long a, long long b) { return a * b / gcd(a, b); }

// auto mod int
// https://youtu.be/L8grWxBlIZ4?t=9858
// https://youtu.be/ERZuLAxZffQ?t=4807 : optimize
// https://youtu.be/8uowVvQ_-Mo?t=1329 : division
const int mod = 1000000007;
struct mint {
  ll x; // typedef long long ll;
  mint(ll x = 0) : x((x % mod + mod) % mod) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const { return mint(*this) += a; }
  mint operator-(const mint a) const { return mint(*this) -= a; }
  mint operator*(const mint a) const { return mint(*this) *= a; }
  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(mod - 2); }
  mint &operator/=(const mint a) { return *this *= a.inv(); }
  mint operator/(const mint a) const { return mint(*this) /= a; }
};
istream &operator>>(istream &is, const mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }

struct combination {
  vector<mint> fact, ifact;
  combination(int n) : fact(n + 1), ifact(n + 1) {
    fact[0] = 1;
    for (int i = 1; i <= n; ++i)
      fact[i] = fact[i - 1] * i;
    ifact[n] = fact[n].inv();
    for (int i = n; i >= 1; --i)
      ifact[i - 1] = ifact[i] * i;
  }
  mint operator()(int n, int k) {
    if (k < 0 || k > n)
      return 0;
    return fact[n] * ifact[k] * ifact[n - k];
  }
} comb(2000005);

mint g(int r, int c) { return comb(r + c + 2, r + 1) - 1; }

bool comp(int i, int j) { return i > j; }

bool compare_by_b(pair<int, int> a, pair<int, int> b) {
  return a.second < b.second;
}

long tall[200000];
long long diff[200000];
vector<int> que;

// index が条件を満たすかどうか
bool isOK(int index, int key) {
  if (que[index] >= key)
    return true;
  else
    return false;
}

// 汎用的な二分探索のテンプレ
int binary_search(int key) {
  int left = -1; // 「index = 0」が条件を満たすこともあるので、初期値は -1
  int right = (int)que.size(); // 「index =
                               // a.size()-1」が条件を満たさないこともあるので、初期値は
                               // a.size()

  /* どんな二分探索でもここの書き方を変えずにできる！ */
  while (right - left > 1) {
    int mid = left + (right - left) / 2;

    if (isOK(mid, key))
      right = mid;
    else
      left = mid;
  }

  /* left は条件を満たさない最大の値、right は条件を満たす最小の値になっている
   */
  return right;
}

long long modpow(long long a, long long n, long long mod) {
  long long res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}

int kind[1000][1000];
int area[1000][1000];
int h, w;
int sh, sw;
int gh, gw;
vector<vector<int>> uni;
queue<pair<int, int>> nn;
vector<int> shortCount;
queue<pair<int, int>> nextArea;

bool isArea(int y, int x) { return x >= 0 && y >= 0 && y < h && x < w; }

int main() {
  cin >> h >> w;
  cin >> sh >> sw;
  cin >> gh >> gw;

  for (int i = 0; i < h; i++) {
    string s;
    cin >> s;
    for (int j = 0; j < w; j++) {
      kind[i][j] = (s[j] == '#' ? -1 : 0);
      // kind[i][j] = ((i + j) % 2 == 0 ? 0 : -1);
      area[i][j] = -1;
    }
  }

  int areaCount = 1;
  uni.push_back({});

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (kind[i][j] == -1) {
        continue;
      }

      if (area[i][j] != -1) {
        continue;
      }

      uni.push_back({});

      nn.push(make_pair(i, j));
      while (nn.size() > 0) {
        pair<int, int> check = nn.front();
        nn.pop();
        if (kind[check.first][check.second] == -1) {
          continue;
        }
        if (area[check.first][check.second] != -1) {
          continue;
        }

        area[check.first][check.second] = areaCount;

        if (isArea(check.first + 1, check.second)) {
          nn.push(make_pair(check.first + 1, check.second));
        }

        if (isArea(check.first - 1, check.second)) {
          nn.push(make_pair(check.first - 1, check.second));
        }

        if (isArea(check.first, check.second + 1)) {
          nn.push(make_pair(check.first, check.second + 1));
        }

        if (isArea(check.first, check.second - 1)) {
          nn.push(make_pair(check.first, check.second - 1));
        }
      }

      areaCount++;
    }
  }

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      int a = area[i][j];
      if (a == -1) {
        continue;
      }

      for (int k = -2; k <= 2; k++) {
        for (int l = -2; l <= 2; l++) {
          if (k == 0 && l == 0) {
            continue;
          }
          if (!isArea(i + k, j + l)) {
            continue;
          }

          if (area[i + k][j + l] == -1) {
            continue;
          }

          if (a == area[i + k][j + l]) {
            continue;
          }

          uni[a].push_back(area[i + k][j + l]);
          uni[area[i + k][j + l]].push_back(a);
        }
      }
    }
  }

  for (int i = 0; i < areaCount; i++) {
    shortCount.push_back(-1);
    unique(uni[i].begin(), uni[i].end());
  }

  nextArea.push(make_pair(area[sh - 1][sw - 1], 0));
  while (nextArea.size() > 0) {
    pair<int, int> sa = nextArea.front();
    nextArea.pop();

    if (shortCount[sa.first] != -1) {
      continue;
    }
    shortCount[sa.first] = sa.second;

    if (sa.first == area[gh - 1][gw - 1]) {
      cout << sa.second;
      return 0;
    }

    for (int i = 0; i < uni[sa.first].size(); i++) {
      nextArea.push(make_pair(uni[sa.first][i], sa.second + 1));
    }
  }

  cout << -1;
}


Error: WA

Fixed submission:

#include <algorithm>
#include <array>
#include <cctype>
#include <climits>
#include <cmath>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}

// a と b の最大公約数を返す関数
long long GCD(long long a, long long b) {
  if (b == 0)
    return a;
  else
    return GCD(b, a % b);
}

long long gcd(long long a, long long b) {
  if (a % b == 0) {
    return (b);
  } else {
    return (gcd(b, a % b));
  }
}

long long lcm(long long a, long long b) { return a * b / gcd(a, b); }

// auto mod int
// https://youtu.be/L8grWxBlIZ4?t=9858
// https://youtu.be/ERZuLAxZffQ?t=4807 : optimize
// https://youtu.be/8uowVvQ_-Mo?t=1329 : division
const int mod = 1000000007;
struct mint {
  ll x; // typedef long long ll;
  mint(ll x = 0) : x((x % mod + mod) % mod) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const { return mint(*this) += a; }
  mint operator-(const mint a) const { return mint(*this) -= a; }
  mint operator*(const mint a) const { return mint(*this) *= a; }
  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(mod - 2); }
  mint &operator/=(const mint a) { return *this *= a.inv(); }
  mint operator/(const mint a) const { return mint(*this) /= a; }
};
istream &operator>>(istream &is, const mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }

struct combination {
  vector<mint> fact, ifact;
  combination(int n) : fact(n + 1), ifact(n + 1) {
    fact[0] = 1;
    for (int i = 1; i <= n; ++i)
      fact[i] = fact[i - 1] * i;
    ifact[n] = fact[n].inv();
    for (int i = n; i >= 1; --i)
      ifact[i - 1] = ifact[i] * i;
  }
  mint operator()(int n, int k) {
    if (k < 0 || k > n)
      return 0;
    return fact[n] * ifact[k] * ifact[n - k];
  }
} comb(2000005);

mint g(int r, int c) { return comb(r + c + 2, r + 1) - 1; }

bool comp(int i, int j) { return i > j; }

bool compare_by_b(pair<int, int> a, pair<int, int> b) {
  return a.second < b.second;
}

long tall[200000];
long long diff[200000];
vector<int> que;

// index が条件を満たすかどうか
bool isOK(int index, int key) {
  if (que[index] >= key)
    return true;
  else
    return false;
}

// 汎用的な二分探索のテンプレ
int binary_search(int key) {
  int left = -1; // 「index = 0」が条件を満たすこともあるので、初期値は -1
  int right = (int)que.size(); // 「index =
                               // a.size()-1」が条件を満たさないこともあるので、初期値は
                               // a.size()

  /* どんな二分探索でもここの書き方を変えずにできる！ */
  while (right - left > 1) {
    int mid = left + (right - left) / 2;

    if (isOK(mid, key))
      right = mid;
    else
      left = mid;
  }

  /* left は条件を満たさない最大の値、right は条件を満たす最小の値になっている
   */
  return right;
}

long long modpow(long long a, long long n, long long mod) {
  long long res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}

int kind[1000][1000];
int area[1000][1000];
int h, w;
int sh, sw;
int gh, gw;
bool tunagaru[1000][1000];
queue<pair<int, int>> nn;
vector<int> shortCount;
queue<pair<int, int>> nextArea;

bool isArea(int y, int x) { return x >= 0 && y >= 0 && y < h && x < w; }

int main() {
  cin >> h >> w;
  cin >> sh >> sw;
  cin >> gh >> gw;

  for (int i = 0; i < h; i++) {
    string s;
    cin >> s;
    for (int j = 0; j < w; j++) {
      kind[i][j] = (s[j] == '#' ? -1 : 0);
      area[i][j] = -1;
      tunagaru[i][j] = false;
    }
  }

  int areaCount = 1;

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (kind[i][j] == -1) {
        continue;
      }

      if (area[i][j] != -1) {
        continue;
      }

      nn.push(make_pair(i, j));
      while (nn.size() > 0) {
        pair<int, int> check = nn.front();
        nn.pop();
        if (kind[check.first][check.second] == -1) {
          continue;
        }
        if (area[check.first][check.second] != -1) {
          continue;
        }

        area[check.first][check.second] = areaCount;

        if (isArea(check.first + 1, check.second)) {
          nn.push(make_pair(check.first + 1, check.second));
        }

        if (isArea(check.first - 1, check.second)) {
          nn.push(make_pair(check.first - 1, check.second));
        }

        if (isArea(check.first, check.second + 1)) {
          nn.push(make_pair(check.first, check.second + 1));
        }

        if (isArea(check.first, check.second - 1)) {
          nn.push(make_pair(check.first, check.second - 1));
        }
      }

      areaCount++;
    }
  }

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      int a = area[i][j];
      if (a == -1) {
        continue;
      }

      for (int k = -2; k <= 2; k++) {
        for (int l = -2; l <= 2; l++) {
          if (k == 0 && l == 0) {
            continue;
          }
          if (!isArea(i + k, j + l)) {
            continue;
          }

          if (a == area[i + k][j + l]) {
            continue;
          }

          tunagaru[a][area[i + k][j + l]] = true;
          tunagaru[area[i + k][j + l]][a] = true;
        }
      }
    }
  }

  for (int i = 0; i < areaCount; i++) {
    shortCount.push_back(-1);
  }

  nextArea.push(make_pair(area[sh - 1][sw - 1], 0));
  while (nextArea.size() > 0) {
    pair<int, int> sa = nextArea.front();
    nextArea.pop();

    if (shortCount[sa.first] != -1) {
      continue;
    }
    shortCount[sa.first] = sa.second;

    if (sa.first == area[gh - 1][gw - 1]) {
      cout << sa.second;
      return 0;
    }

    for (int i = 0; i < areaCount; i++) {
      if (tunagaru[sa.first][i]) {
        nextArea.push(make_pair(i, sa.second + 1));
      }
    }
  }

  cout << -1;
}