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

#include <bits/stdc++.h>
using namespace std;

int main() {
  int h, w;
  cin >> h >> w;
  long long dx[4] = {0, 1, 0, -1};
  long long dy[4] = {1, 0, -1, 0};

  pair<int, int> c, d;
  cin >> c.first >> c.second;
  cin >> d.first >> d.second;
  c.first--;
  c.second--;
  d.first--;
  d.second--;
  vector<string> a(h);
  vector<vector<long long>> b(h);
  for (int i = 0; i < h; i++)
    cin >> a[i];

  for (int i = 0; i < h; i++) {
    b[i].resize(w);
    for (int j = 0; j < w; j++)
      b[i][j] = 1000000000;
  }

  queue<pair<int, int>> q1, q2;
  q1.push(make_pair(c.first, c.second));
  b[c.first][c.second] = 0;
  while (!q1.empty() || !q2.empty()) {
    if (!q1.empty()) {
      auto u = q1.front();
      q1.pop();
      q2.push(make_pair(u.first, u.second));
      for (int i = 0; i < 4; i++) {
        int nx = u.first + dx[i];
        int ny = u.second + dy[i];
        if (ny < 0 || ny >= w || nx < 0 || nx >= h || a[nx][ny] == '#')
          continue;
        if (b[nx][ny] > b[u.first][u.second] && a[nx][ny] == '.') {
          b[nx][ny] = b[u.first][u.second];
          q1.push(make_pair(nx, ny));
        }
      }
    } else {
      auto v = q2.front();
      q2.pop();
      for (int i = -2; i <= 2; i++) {
        for (int j = -2; j <= 2; j++) {
          int nx = v.first + i;
          int ny = v.second + j;
          if (ny < 0 || ny >= w || nx < 0 || nx >= h || a[nx][ny] == '#')
            continue;
          if (b[nx][ny] > (b[v.first][v.second] + 1) && a[nx][ny] == '.') {
            b[nx][ny] = b[v.first][v.second] + 1;
            q1.push(make_pair(nx, ny));
          }
        }
      }
    }
  }
  if (b[d.first][d.second] == 1000000000)
    cout << -1 << endl;
  else
    cout << b[d.first][d.second] << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int main() {
  int h, w;
  cin >> h >> w;
  long long dx[4] = {0, 1, 0, -1};
  long long dy[4] = {1, 0, -1, 0};

  pair<int, int> c, d;
  cin >> c.first >> c.second;
  cin >> d.first >> d.second;
  c.first--;
  c.second--;
  d.first--;
  d.second--;
  vector<string> a(h);
  vector<vector<long long>> b(h);
  for (int i = 0; i < h; i++)
    cin >> a[i];

  for (int i = 0; i < h; i++) {
    b[i].resize(w);
    for (int j = 0; j < w; j++)
      b[i][j] = 1000000000;
  }

  queue<pair<int, int>> q1, q2;
  q1.push(make_pair(c.first, c.second));
  b[c.first][c.second] = 0;
  while (!q1.empty() || !q2.empty()) {
    if (!q1.empty()) {
      auto u = q1.front();
      q1.pop();
      q2.push(make_pair(u.first, u.second));
      for (int i = 0; i < 4; i++) {
        int nx = u.first + dx[i];
        int ny = u.second + dy[i];
        if (ny < 0 || ny >= h || nx < 0 || nx >= w || a[nx][ny] == '#')
          continue;
        if (b[nx][ny] > b[u.first][u.second] && a[nx][ny] == '.') {
          b[nx][ny] = b[u.first][u.second];
          q1.push(make_pair(nx, ny));
        }
      }
    } else {
      auto v = q2.front();
      q2.pop();
      for (int i = -2; i <= 2; i++) {
        for (int j = -2; j <= 2; j++) {
          int nx = v.first + i;
          int ny = v.second + j;
          if (ny < 0 || ny >= h || nx < 0 || nx >= w || a[nx][ny] == '#')
            continue;
          if (b[nx][ny] > (b[v.first][v.second] + 1) && a[nx][ny] == '.') {
            b[nx][ny] = b[v.first][v.second] + 1;
            q1.push(make_pair(nx, ny));
          }
        }
      }
    }
  }
  if (b[d.first][d.second] == 1000000000)
    cout << -1 << endl;
  else
    cout << b[d.first][d.second] << endl;
  return 0;
}