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

// #include <bits/stdc++.h>
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define _GLIBCii_DEBUG                        // ArrajOutofBounds
#define rep(i, N) for (int i = 0; i < N; i++) // 制御変数iを用いてN回転
using namespace std;
using ll = long long;

// 0:上 1:左 2:下 3:右
const int di[] = {-1, 0, 1, 0};
const int dj[] = {0, -1, 0, 1};

int main() {
  int H, W, Ch, Cw, Dh, Dw;
  cin >> H >> W >> Ch >> Cw >> Dh >> Dw;
  vector<string> S(H);
  vector<vector<int>> ans(H, vector<int>(W, 999999));
  rep(i, H) cin >> S[i];
  Ch--;
  Cw--;
  Dh--;
  Dw--;
  ans[Ch][Cw] = 0;
  deque<pair<int, int>> q;
  q.push_front(pair<int, int>(Ch, Cw));
  while (!q.empty()) {
    int i = q.front().first;
    int j = q.front().second;
    int count = ans[i][j];
    q.pop_front();
    for (int k = 0; k < 4; k++) {
      int new_i = i + di[k];
      int new_j = j + dj[k];
      if (new_i < 0 || new_i >= H || new_j < 0 || new_j >= W)
        continue;
      if (S[new_i][new_j] == '#')
        continue;
      if (ans[new_i][new_j] <= count)
        continue;
      ans[new_i][new_j] = count;
      q.push_front(pair<int, int>(new_i, new_j));
    }
    for (int ji = -2; ji <= 2; ji++) {
      for (int jj = -2; jj <= 2; jj++) {
        int new_i = i + ji;
        int new_j = j + jj;
        if (new_i < 0 || new_i >= H || new_j < 0 || new_j >= W)
          continue;
        if (S[new_i][new_j] == '#')
          continue;
        if (ans[new_i][new_j] <= count + 1)
          continue;
        ans[new_i][new_j] = count + 1;
        q.push_back(pair<int, int>(new_i, new_j));
      }
    }
  }
  rep(i, H) rep(j, W) if (ans[i][j] == 999999) ans[i][j] = -1;

  cout << ans[Dh][Dw] << endl;
  return 0;
}

Error: 0

Fixed submission:

// #include <bits/stdc++.h>
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define _GLIBCXX_DEBUG                        // ArrayOutofBounds
#define rep(i, N) for (int i = 0; i < N; i++) // 制御変数iを用いてN回転
using namespace std;
using ll = long long;

int H, W, Dh, Dw;

void solve(vector<string> &S, vector<vector<int>> &ans, int h, int w) {
  if (w < W - 1) {
    if (S[h][w + 1] == '.' && ans[h][w + 1] > ans[h][w]) {
      ans[h][w + 1] = ans[h][w];
      solve(S, ans, h, w + 1);
    } else if (S[h][w + 1] == '#') {
      for (int i = max(0, h - 2); i < min(H, h + 3); i++) {
        for (int j = max(0, w - 2); j < min(W, w + 3); j++) {
          if (i == h && j == w)
            continue;
          if (S[i][j] == '.') {
            if (ans[i][j] > ans[h][w] + 1) {
              ans[i][j] = ans[h][w] + 1;
              solve(S, ans, i, j);
            }
          }
        }
      }
    }
  }
  if (w > 0) {
    if (S[h][w - 1] == '.' && ans[h][w - 1] > ans[h][w]) {
      ans[h][w - 1] = ans[h][w];
      solve(S, ans, h, w - 1);
    } else if (S[h][w - 1] == '#') {
      for (int i = max(0, h - 2); i < min(H, h + 3); i++) {
        for (int j = max(0, w - 2); j < min(W, w + 3); j++) {
          if (i == h && j == w)
            continue;
          if (S[i][j] == '.') {
            if (ans[i][j] > ans[h][w] + 1) {
              ans[i][j] = ans[h][w] + 1;
              solve(S, ans, i, j);
            }
          }
        }
      }
    }
  }
  if (h < H - 1) {
    if (S[h + 1][w] == '.' && ans[h + 1][w] > ans[h][w]) {
      ans[h + 1][w] = ans[h][w];
      solve(S, ans, h + 1, w);
    } else if (S[h + 1][w] == '#') {
      for (int i = max(0, h - 2); i < min(H, h + 3); i++) {
        for (int j = max(0, w - 2); j < min(W, w + 3); j++) {
          if (i == h && j == w)
            continue;
          if (S[i][j] == '.') {
            if (ans[i][j] > ans[h][w] + 1) {
              ans[i][j] = ans[h][w] + 1;
              solve(S, ans, i, j);
            }
          }
        }
      }
    }
  }
  if (h > 0) {
    if (S[h - 1][w] == '.' && ans[h - 1][w] > ans[h][w]) {
      ans[h - 1][w] = ans[h][w];
      solve(S, ans, h + 1, w);
    } else if (S[h - 1][w] == '#') {
      for (int i = max(0, h - 2); i < min(H, h + 3); i++) {
        for (int j = max(0, w - 2); j < min(W, w + 3); j++) {
          if (i == h && j == w)
            continue;
          if (S[i][j] == '.') {
            if (ans[i][j] > ans[h][w] + 1) {
              ans[i][j] = ans[h][w] + 1;
              solve(S, ans, i, j);
            }
          }
        }
      }
    }
  }
}

int main() {
  int Ch, Cw;
  cin >> H >> W >> Ch >> Cw >> Dh >> Dw;
  vector<string> S(H);
  vector<vector<int>> ans(H, vector<int>(W, 999999));
  rep(i, H) cin >> S[i];
  Ch--;
  Cw--;
  Dh--;
  Dw--;
  ans[Ch][Cw] = 0;
  solve(S, ans, Ch, Cw);
  rep(i, H) {
    rep(j, W) {
      if (ans[i][j] == 999999)
        ans[i][j] = -1;
    }
  }

  cout << ans[Dh][Dw] << endl;
  return 0;
}