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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

// 0-1BFS o(H*W)

const int di[] = {-1, 0, 1, 0};
const int dj[] = {0, -1, 0, 1};

int main() {
  int h, w;
  cin >> h >> w;
  int si, sj;
  cin >> si >> sj;
  int ti, tj;
  cin >> ti >> tj;
  --si;
  --sj;
  --ti;
  --tj;
  vector<string> s(h);
  rep(i, h) cin >> s[i];

  const int INF = 1e9;
  vector<vector<int>> dist(h, vector<int>(w, INF));
  deque<P> q;
  dist[si][sj] = 0;
  q.emplace_back(si, sj);
  while (!q.empty()) {
    int i = q.front().first;
    int j = q.front().second;
    q.pop_front();
    int d = dist[i][j];
    rep(v, 4) {
      int ni = i + di[v], nj = j + dj[v];
      if (ni < 0 || ni >= h || nj < 0 || nj >= w)
        continue;
      if (s[ni][nj] == '#')
        continue;
      if (dist[ni][nj] <= d)
        continue;
      dist[ni][nj] = d;
      q.emplace_front(ni, nj);
    }
    for (int ei = -2; ei <= 2; ++ei) {
      for (int ej = -2; ej <= 2; ++ej) {
        int ni = i + ei, nj = j + ej;
        if (ni < 0 || ni >= h || nj < 0 || 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[ti][tj];
  if (ans == INF)
    ans = -1;
  cout << ans << endl;
  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
using Graph = vector<vector<int>>;

// 四方向への移動ベクトル
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

// 入力
int H, W;
vector<string> field;

// 探索
bool seen[1010][1010]; // seen[h][w] := マス (h, w) が検知済みかどうか
void dfs(int h, int w) {
  seen[h][w] = true;

  // 四方向を探索
  for (int dir = 0; dir < 4; ++dir) {
    int nh = h + dx[dir];
    int nw = w + dy[dir];

    // 場外アウトしたり、移動先が壁の場合はスルー
    if (nh < 0 || nh >= H || nw < 0 || nw >= W)
      continue;
    if (field[nh][nw] == '#')
      continue;

    // 移動先が探索済みの場合
    if (seen[nh][nw])
      continue;

    // 再帰的に探索
    dfs(nh, nw);
  }
}

int main() {
  // 入力受け取り
  cin >> H >> W;
  field.resize(H);
  // s と g のマスを特定する
  int sh, sw, gh, gw;
  cin >> sh >> sw;
  sh--;
  sw--;
  cin >> gh >> gw;
  gh--;
  gw--;
  rep(h, H) cin >> field[h];

  ll ans = 0;
  int g = gh + gw;
  int s = sh + sw;
  memset(seen, 0, sizeof(seen));
  dfs(sh, sw);
  if (seen[gh][gw]) {
    cout << 0 << endl;
    return 0;
  } else {
    while (!seen[gh][gw]) {
      memset(seen, 0, sizeof(seen));
      bool ok = false;
      int i1;
      int j1;
      int ss1 = abs(s - g);
      for (int i = -2; i <= 2; i++) {
        for (int j = -2; j <= 2; j++) {
          if (field[sh + i][sw + j] == '#' || sh + i < 0 || sh + i >= H ||
              sw + j < 0 || sw + j >= W) {
            continue;
          }
          int ss = sw + j + sh + i;
          if (abs(ss - g) >= abs(s - g)) {
            continue;
          }
          ok = true;
          if (abs(ss - g) < ss1) {
            ss1 = abs(ss - g);
            i1 = i;
            j1 = j;
          }
        }
      }
      if (ok) {
        sh += i1;
        sw += j1;
        ans++;
        dfs(sh, sw);
      } else {
        cout << -1 << endl;
        return 0;
      }
    }
  }

  cout << ans << endl;
  return 0;
}