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

#include <bits/stdc++.h>

const int INF = 1e9;
const int MOD = 1e9 + 7;

using namespace std;

typedef long long ll;

#define REP(i, n) for (int i = 0; i < int(n); i++)
#define REPD(i, n) for (int i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (int i = a; i < int(b); i++)
#define FORD(i, a, b) for (int i = b - 1; i >= int(a); i--)
#define WRAP(y, x, h, w) (0 <= y && y < h && 0 <= x && x < w)

#define ALL(x) (x).begin(), (x).end()

int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

int main() {
  int h, w;
  cin >> h >> w;
  pair<int, int> start, goal;
  cin >> start.first >> start.second >> goal.first >> goal.second;
  start.first--;
  start.second--;
  goal.first--;
  goal.second--;
  vector<string> vs(h);
  REP(i, h)
  cin >> vs[i];
  vector<vector<int>> cost(h, vector<int>(w, -1));
  cost[start.first][start.second] = 0;
  queue<pair<int, int>> Q;
  queue<pair<int, int>> Q2;
  Q.push(start);
  int a = 0;
  while (!Q.empty()) {
    while (!Q.empty()) {
      pair<int, int> q = Q.front();
      Q.pop();
      Q2.push(q);
      REP(i, 4) {
        if (WRAP(q.first + dy[i], q.second + dx[i], h, w)) {
          if (vs[q.first + dy[i]][q.second + dx[i]] == '.') {
            if (cost[q.first + dy[i]][q.second + dx[i]] == -1 ||
                cost[q.first + dy[i]][q.second + dx[i]] > a) {
              cost[q.first + dy[i]][q.second + dx[i]] = a;
              Q.push({q.first + dy[i], q.second + dx[i]});
            }
          }
        }
      }
    }
    while (!Q2.empty()) {
      pair<int, int> q = Q2.front();
      Q2.pop();
      REP(i, 5) {
        REP(j, 5) {
          if (WRAP(q.first + i - 2, q.second + j - 2, h, w)) {
            if (vs[q.first + i - 2][q.second + j - 2] == '.') {
              if (cost[q.first + i - 2][q.second + j - 2] == -1 ||
                  cost[q.first + i - 2][q.second + j - 2] > a + 1) {
                cost[q.first + i - 2][q.second + j - 2] = a + 1;
                Q.push({q.first + i - 2, q.second + j - 2});
              }
            }
          }
        }
      }
    }
    a++;
  }
  cout << cost[goal.first][goal.second] << endl;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>

const int INF = 1e9;
const int MOD = 1e9 + 7;

using namespace std;

typedef long long ll;

#define REP(i, n) for (int i = 0; i < int(n); i++)
#define REPD(i, n) for (int i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (int i = a; i < int(b); i++)
#define FORD(i, a, b) for (int i = b - 1; i >= int(a); i--)
#define WRAP(y, x, h, w) (0 <= y && y < h && 0 <= x && x < w)

#define ALL(x) (x).begin(), (x).end()

int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

int main() {
  int h, w;
  cin >> h >> w;
  pair<int, int> start, goal;
  cin >> start.first >> start.second >> goal.first >> goal.second;
  start.first--;
  start.second--;
  goal.first--;
  goal.second--;
  vector<string> vs(h);
  REP(i, h)
  cin >> vs[i];
  vector<vector<int>> cost(h, vector<int>(w, -1));
  cost[start.first][start.second] = 0;
  queue<pair<int, int>> Q;
  queue<pair<int, int>> Q2;
  Q.push(start);
  Q2.push(start);
  int a = 0;
  while (!Q.empty()) {
    while (!Q.empty()) {
      pair<int, int> q = Q.front();
      Q.pop();
      REP(i, 4) {
        if (WRAP(q.first + dy[i], q.second + dx[i], h, w)) {
          if (vs[q.first + dy[i]][q.second + dx[i]] == '.') {
            if (cost[q.first + dy[i]][q.second + dx[i]] == -1 ||
                cost[q.first + dy[i]][q.second + dx[i]] > a) {
              cost[q.first + dy[i]][q.second + dx[i]] = a;
              Q.push({q.first + dy[i], q.second + dx[i]});
              Q2.push({q.first + dy[i], q.second + dx[i]});
            }
          }
        }
      }
    }
    while (!Q2.empty()) {
      pair<int, int> q = Q2.front();
      Q2.pop();
      REP(i, 5) {
        REP(j, 5) {
          if (WRAP(q.first + i - 2, q.second + j - 2, h, w)) {
            if (vs[q.first + i - 2][q.second + j - 2] == '.') {
              if (cost[q.first + i - 2][q.second + j - 2] == -1 ||
                  cost[q.first + dy[i]][q.second + dx[i]] > a + 1) {
                cost[q.first + i - 2][q.second + j - 2] = a + 1;
                Q.push({q.first + i - 2, q.second + j - 2});
              }
            }
          }
        }
      }
    }
    a++;
  }
  cout << cost[goal.first][goal.second] << endl;
}