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

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

#define rep(i, n) rep2(i, 0, n)
#define rep2(i, m, n) for (ll i = m; i < (n); i++)

using ll = long long;
using pii = pair<int, int>;
using Vi = vector<int>;

int main() {
  int H, W, Sx, Sy, Gx, Gy;
  cin >> H >> W >> Sx >> Sy >> Gx >> Gy;
  vector<vector<int>> num(
      1010, vector<int>(
                1010, 10000000)); // そのマスにワープ何回で辿り着けるか記録する

  char s;
  rep(i, H) {
    rep(j, W) {
      cin >> s;
      if (s == '.') {
        num[i + 1][j + 1] = -1;
      }
    }
  }

  // BFSの準備
  queue<pair<int, int>> que0;

  queue<pair<int, int>> que;

  que.push(make_pair(Sx, Sy));
  que0.push(make_pair(Sx, Sy));
  num[Sx][Sy] = 0;
  int p = 0;

  while (!que.empty()) {

    while (!que.empty()) {
      pair<int, int> v = que.front();
      que.pop();

      int x, y;
      tie(x, y) = v;

      if (num[x + 1][y] == -1) {
        num[x + 1][y] = p;
        que0.push(make_pair(x + 1, y));
        que.push(make_pair(x + 1, y));
      }
      if (num[x - 1][y] == -1) {
        num[x - 1][y] = p;
        que0.push(make_pair(x - 1, y));
        que.push(make_pair(x - 1, y));
      }
      if (num[x][y + 1] == -1) {
        num[x][y + 1] = p;
        que0.push(make_pair(x, y + 1));
        que.push(make_pair(x, y + 1));
      }
      if (num[x][y - 1] == -1) {
        num[x][y - 1] = p;
        que0.push(make_pair(x, y - 1));
        que.push(make_pair(x, y - 1));
      }
    }

    p++;
    int t = que0.size();
    for (int i = 0; i < t; i++) {
      pair<int, int> v = que0.front();
      que0.pop();
      int x, y;
      tie(x, y) = v;
      for (int i = max(0, x - 2); i <= min(1010, x + 2); i++) {
        for (int j = max(0, y - 2); j <= min(1010, y + 2); j++) {
          if (num[i][j] == -1) {
            num[i][j] = p;
            que.push(make_pair(i, j));
            que0.push(make_pair(i, j));
          }
        }
      }
    }
  }

  cout << num[Gx][Gy] << endl;
}


Error: -11

Fixed submission:

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

#define rep(i, n) rep2(i, 0, n)
#define rep2(i, m, n) for (ll i = m; i < (n); i++)

using ll = long long;
using pii = pair<int, int>;
using Vi = vector<int>;

int main() {
  int H, W, Sx, Sy, Gx, Gy;
  cin >> H >> W >> Sx >> Sy >> Gx >> Gy;
  vector<vector<int>> num(
      1010,
      vector<int>(1010, -1)); // そのマスにワープ何回で辿り着けるか記録する
  vector<vector<bool>> map(1010, vector<bool>(1010, false));

  char s;
  rep(i, H) {
    rep(j, W) {
      cin >> s;
      if (s == '.') {
        num[i + 1][j + 1] = 100000000;
      }
    }
  }

  // BFSの準備
  queue<pair<int, int>> que0;

  queue<pair<int, int>> que;

  que.push(make_pair(Sx, Sy));
  que0.push(make_pair(Sx, Sy));
  num[Sx][Sy] = 0;

  while (!que.empty()) {
    pair<int, int> v = que.front();
    que.pop();

    int x, y;
    tie(x, y) = v;

    if (num[x + 1][y] < 0) {
      num[x + 1][y] = 0;
      que0.push(make_pair(x + 1, y));
      que.push(make_pair(x + 1, y));
    }
    if (num[x - 1][y] < 0) {
      num[x - 1][y] = 0;
      que0.push(make_pair(x - 1, y));
      que.push(make_pair(x - 1, y));
    }
    if (num[x][y + 1] < 0) {
      num[x][y + 1] = 0;
      que0.push(make_pair(x, y + 1));
      que.push(make_pair(x, y + 1));
    }
    if (num[x][y - 1] < 0) {
      num[x][y - 1] = 0;
      que0.push(make_pair(x, y - 1));
      que.push(make_pair(x, y - 1));
    }
  }

  while (!que0.empty()) {
    pair<int, int> v = que0.front();
    que0.pop();
    int x, y;
    tie(x, y) = v;
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        if (num[x + i][y + j] == -1) {
          num[x + i][y + j] = 1;
        }
      }
    }
  }

  rep(i, H + 1) {
    rep(j, W + 1) { cout << num[i][j] << " "; }
    cout << endl;
  }
}