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

#include <bits/stdc++.h>

using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, n) for (int i = 1; i <= n; i++)
typedef long long ll;

char maze[1005][1005];
int dist[1005][1005];

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

const int ddx[5] = {2, 1, 0, -1, -2};
const int ddy[5] = {2, 1, 0, -1, -2};

int main() {
  int h, w, ch, cw, dh, dw;
  cin >> h >> w >> ch >> cw >> dh >> dw;
  ch--, cw--, dh--, dw--;
  rep(i, h) rep(j, w) cin >> maze[i][j];
  rep(i, h) rep(j, w) dist[i][j] = -1;
  queue<pair<int, int>> a;
  queue<pair<int, int>> b;
  dist[ch][cw] = 0;
  a.emplace(ch, cw);
  while (!a.empty() || !b.empty()) {
    if (!a.empty()) {
      auto p = a.front();
      a.pop();
      b.emplace(p);
      rep(i, 4) {
        int ny = p.first + dy[i];
        int nx = p.second + dx[i];
        if (ny < 0 || ny >= h || nx < 0 || nx >= w)
          continue;
        if (dist[ny][nx] != -1)
          continue;
        if (maze[ny][nx] == '.') {
          a.emplace(ny, nx);
          dist[ny][nx] = dist[p.first][p.second];
        }
      }
    } else {
      auto p = b.front();
      b.pop();
      rep(i, 5) rep(j, 5) {
        int nny = p.first + ddy[i];
        int nnx = p.second + ddx[j];
        if (nny < 0 || nny >= h || nnx < 0 || nnx >= w)
          continue;
        if (dist[nny][nnx] != -1)
          continue;
        if (maze[nny][nnx] == '.') {
          a.emplace(nny, nnx);
          dist[nny][nnx] = dist[p.first][p.second] + 1;
        }
      }
    }
  }
  int ans = dist[dh][dw];
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

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

char maze[1005][1005];
int dist[1005][1005];

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

const int ddx[5] = {2, 1, 0, -1, -2};
const int ddy[5] = {2, 1, 0, -1, -2};

int main() {
  int h, w, ch, cw, dh, dw;
  cin >> h >> w >> ch >> cw >> dh >> dw;
  ch--, cw--, dh--, dw--;
  rep(i, h) rep(j, w) cin >> maze[i][j];
  rep(i, h) rep(j, w) dist[i][j] = -1;
  queue<pair<int, int>> a;
  queue<pair<int, int>> b;
  dist[ch][cw] = 0;
  a.emplace(ch, cw);
  while (!a.empty() || !b.empty()) {
    if (!a.empty()) {
      auto p = a.front();
      a.pop();
      b.emplace(p);
      rep(i, 4) {
        int ny = p.first + dy[i];
        int nx = p.second + dx[i];
        if (dist[ny][nx] != -1 || ny < 0 || ny >= h || nx < 0 || nx >= w)
          continue;
        if (maze[ny][nx] == '.') {
          a.emplace(ny, nx);
          dist[ny][nx] = dist[p.first][p.second];
        }
      }
    } else {
      auto p = b.front();
      b.pop();
      rep(i, 5) rep(j, 5) {
        int nny = p.first + ddy[i];
        int nnx = p.second + ddx[j];
        if (dist[nny][nnx] != -1 || nny < 0 || nny >= h || nnx < 0 || nnx >= w)
          continue;
        if (maze[nny][nnx] == '.') {
          a.emplace(nny, nnx);
          dist[nny][nnx] = dist[p.first][p.second] + 1;
        }
      }
    }
  }
  int ans = dist[dh][dw];
  cout << ans << endl;
  return 0;
}