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>;

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;
    int d = dist[i][j];
    q.pop_front();
    if (dist[i][j] != d) {
      continue;
    }
    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: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repr(i, a, b) for (int i = a; i < b; i++)
#define each(i, mp) for (auto &i : mp)
const int MAX_W = 500;
const int MAX_H = 500;
const int INF = 1001001;

int h, w, sy, sx, gy, gx;
char maze[MAX_W][MAX_H];
bool reached[MAX_W][MAX_H];
bool isGool = false;
int d[MAX_W][MAX_H];
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int bfs() {
  deque<P> que;
  rep(i, w + 1) rep(j, h + 1) d[i][j] = INF;
  que.push_back(P(sx, sy));
  d[sx][sy] = 0;

  while (!que.empty()) {
    P p = que.front();
    int now_x = p.first, now_y = p.second;
    int now_cost = d[now_x][now_y];
    que.pop_front();
    // cout << "gx= " << gx << "gy= " << gy << '\\n';
    // cout << "now_x" << now_x << "now_y" << now_y <<  "now_cost" << now_cost
    // << '\\n';
    if (now_x == gx && now_y == gy) {
      break;
    }

    rep(i, 4) {
      int nx = now_x + dx[i], ny = now_y + dy[i];
      // cout << "nx=" << nx << "ny=" << ny << '\\n';
      if (0 < nx && nx <= w && 0 < ny && ny <= h && maze[nx][ny] != '#' &&
          d[nx][ny] > now_cost) {
        // 普通の移動
        que.push_front(P(nx, ny));
        d[nx][ny] = now_cost;
      }
    }
    repr(i, -2, 3) {
      repr(j, -2, 3) {
        int nx = now_x + i, ny = now_y + j;
        if (0 < nx && nx <= w && 0 < ny && ny <= h && maze[nx][ny] != '#' &&
            d[nx][ny] > now_cost + 1) {
          // ワープ
          que.push_back(P(nx, ny));
          d[nx][ny] = now_cost + 1;
        }
      }
    }
  }

  return d[gx][gy];
}

int main() {
  cin >> h >> w;
  cin >> sy >> sx;
  cin >> gy >> gx;

  repr(i, 1, h + 1) repr(j, 1, w + 1) cin >> maze[j][i];
  // cout << "d[gx][gy]= " << d[gx][gy] << '\\n';
  int ans = bfs();
  if (ans == INF) {
    ans = -1;
  }
  cout << ans << '\\n';

  return 0;
}