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

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define reps(i, n) for (int i = 1, i##_len = (n); i <= i##_len; ++i)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; --i)
#define rreps(i, n) for (int i = ((int)(n)); i > 0; --i)
#define INF 1000000000
#define MOD 1000000007
#define CHOOSE(a) CHOOSE2 a
#define CHOOSE2(a0, a1, a2, a3, a4, x, ...) x
#define debug_1(x1) cout << #x1 << ": " << x1 << endl
#define debug_2(x1, x2)                                                        \\
  cout << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << endl
#define debug_3(x1, x2, x3)                                                    \\
  cout << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": "      \\
       << x3 << endl
#define debug_4(x1, x2, x3, x4)                                                \\
  cout << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": "      \\
       << x3 << ", " #x4 << ": " << x4 << endl
#define debug_5(x1, x2, x3, x4, x5)                                            \\
  cout << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": "      \\
       << x3 << ", " #x4 << ": " << x4 << ", " #x5 << ": " << x5 << endl
#ifdef _DEBUG
#define debug(...)                                                             \\
  CHOOSE((__VA_ARGS__, debug_5, debug_4, debug_3, debug_2, debug_1, ~))        \\
  (__VA_ARGS__)
#else
#define debug(...)
#endif

using namespace std;
using ll = long long;
typedef pair<int, int> P;

int dx[4] = {1, -1, 0, 0}, dy[4] = {0, 0, 1, -1};

int main() {
  int sy, sx, gy, gx;
  int r, c;
  cin >> r >> c >> sy >> sx >> gy >> gx;
  sy--, sx--, gy--, gx--;
  vector<string> field(r);
  vector<vector<int>> cost(r, vector<int>(c, INF));
  rep(i, r) cin >> field[i];

  queue<P> que, visited;
  que.push(P(sy, sx));
  cost[sy][sx] = 0;

  while (!que.empty() || !visited.empty()) {
    if (!que.empty()) {
      P p = que.front();
      visited.push(P(p.first, p.second));
      que.pop();

      // 4方向探索
      rep(i, 4) {
        int ny = dy[i] + p.first;
        int nx = dx[i] + p.second;

        // 移動可能か判断
        if (0 > nx || nx >= c || 0 > ny || ny >= r || field[ny][nx] == '#')
          continue;

        if (cost[ny][nx] > cost[p.first][p.second]) {
          cost[ny][nx] = cost[p.first][p.second];
          que.push(P(ny, nx));
        }
      }
    } else {
      P p = visited.front();
      visited.pop();
      for (int jy = -2; jy <= 2; ++jy) {
        for (int ix = -2; ix <= 2; ++ix) {
          int ny = jy + p.first;
          int nx = ix + p.second;

          if (0 > nx || nx >= c || 0 > ny || ny >= r || field[ny][nx] == '#')
            continue;

          if (cost[ny][nx] > cost[p.first][p.second] + 1) {
            cost[ny][nx] = cost[p.first][p.second] + 1;
            que.push(P(ny, nx));
          }
        }
      }
    }
  }

  if (cost[gy][gx] == INF)
    cout << -1 << endl;
  else
    cout << cost[gy][gx] << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define reps(i, n) for (int i = 1, i##_len = (n); i <= i##_len; ++i)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; --i)
#define rreps(i, n) for (int i = ((int)(n)); i > 0; --i)
#define INF 1000000000
#define MOD 1000000007
#define CHOOSE(a) CHOOSE2 a
#define CHOOSE2(a0, a1, a2, a3, a4, x, ...) x
#define debug_1(x1) cout << #x1 << ": " << x1 << endl
#define debug_2(x1, x2)                                                        \\
  cout << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << endl
#define debug_3(x1, x2, x3)                                                    \\
  cout << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": "      \\
       << x3 << endl
#define debug_4(x1, x2, x3, x4)                                                \\
  cout << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": "      \\
       << x3 << ", " #x4 << ": " << x4 << endl
#define debug_5(x1, x2, x3, x4, x5)                                            \\
  cout << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": "      \\
       << x3 << ", " #x4 << ": " << x4 << ", " #x5 << ": " << x5 << endl
#ifdef _DEBUG
#define debug(...)                                                             \\
  CHOOSE((__VA_ARGS__, debug_5, debug_4, debug_3, debug_2, debug_1, ~))        \\
  (__VA_ARGS__)
#else
#define debug(...)
#endif

using namespace std;
using ll = long long;
typedef pair<int, int> P;

vector<string> field;
int r, c;
int cost[500][500];
int dx[4] = {1, -1, 0, 0}, dy[4] = {0, 0, 1, -1};

int bfs(int sy, int sx, int gy, int gx) {
  queue<P> que, visited;
  // costの初期化
  rep(i, r) rep(j, c) { cost[i][j] = INF; }
  // スタート地点をキューに入れ、その点の距離を0にする
  que.push(P(sy, sx));
  cost[sy][sx] = 0;

  while (!que.empty() || !visited.empty()) {
    if (!que.empty()) {
      P p = que.front();
      visited.push(P(p.first, p.second));
      que.pop();

      // 4方向探索
      rep(i, 4) {
        int ny = dy[i] + p.first;
        int nx = dx[i] + p.second;

        // 移動可能か判断
        if (0 > nx || nx >= c || 0 > ny || ny >= r || field[ny][nx] == '#')
          continue;

        if (cost[ny][nx] > cost[p.first][p.second]) {
          cost[ny][nx] = cost[p.first][p.second];
          que.push(P(ny, nx));
        }
      }
    } else {
      P p = visited.front();
      visited.pop();
      for (int jy = -2; jy <= 2; ++jy) {
        for (int ix = -2; ix <= 2; ++ix) {
          int ny = jy + p.first;
          int nx = ix + p.second;

          if (0 > nx || nx >= c || 0 > ny || ny >= r || field[ny][nx] == '#')
            continue;

          if (cost[ny][nx] > cost[p.first][p.second] + 1) {
            cost[ny][nx] = cost[p.first][p.second] + 1;
            que.push(P(ny, nx));
          }
        }
      }
    }
  }
  return cost[gy][gx];
}

int main() {
  int sy, sx, gy, gx;
  cin >> r >> c >> sy >> sx >> gy >> gx;
  sy--, sx--, gy--, gx--;
  field.resize(r);
  rep(i, r) cin >> field[i];
  int ans = bfs(sy, sx, gy, gx);
  if (ans == INF)
    cout << -1 << endl;
  else
    cout << ans << endl;

  return 0;
}