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

#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 = 5000;
const int MAX_H = 5000;
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';
  ll ans = bfs();
  if (ans == INF) {
    ans = -1;
  }
  cout << ans << '\\n';

  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';
  ll ans = bfs();
  if (ans == INF) {
    ans = -1;
  }
  cout << ans << '\\n';

  return 0;
}