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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
int main() {
  int fx[5] = {1, -1, 0, 0}, fy[5] = {0, 0, 1, -1};
  int h, w, cx, cy, dx, dy, i, j;
  int INF = (1 << 29);
  cin >> h >> w >> cx >> cy >> dx >> dy;
  cx++, cy++, dx++, dy++;
  int a[1010][1010], b[1010][1010];
  for (i = 0; i <= h + 3; i++) {
    for (j = 0; j <= w + 3; j++) {
      a[i][j] = 1, b[i][j] = INF;
    }
  }
  for (i = 2; i <= h + 1; i++) {
    for (j = 2; j <= w + 1; j++) {
      char s;
      cin >> s;
      if (s == '.') {
        a[i][j] = 0;
      }
    }
  }
  deque<P> que;
  que.push_front(P(cx, cy));
  b[cx][cy] = 0;
  while (!que.empty()) {
    P p = que.front();
    que.pop_front();
    for (i = 0; i < 4; i++) {
      if (a[p.first + fx[i]][p.second + fy[i]] == 0 &&
          b[p.first + fx[i]][p.second + fy[i]] > b[p.first][p.second]) {
        b[p.first + fx[i]][p.second + fy[i]] = b[p.first][p.second];
        que.push_front(P(p.first + fx[i], p.second + fy[i]));
      }
    }
    for (i = p.first - 2; i <= p.first + 2; i++) {
      for (j = p.second - 2; j <= p.second + 2; j++) {
        if (a[i][j] == 0 && b[i][j] > b[p.first][p.second] + 1) {
          b[i][j] = b[p.first][p.second] + 1;
          que.push_back(P(i, j));
        }
      }
    }
  }
  if (b[dx][dy] == INF) {
    cout << -1 << endl;
    return 0;
  }
  cout << b[dx][dy] << endl;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
int main() {
  int fx[5] = {1, 0, -1, 0}, fy[5] = {0, 1, 0, -1};
  int INF = (1 << 29);
  int h, w, cx, cy, dx, dy, i, j, k;
  bool a[1010][1010];
  int b[1010][1010];
  int c[1010][1010];
  cin >> h >> w >> cx >> cy >> dx >> dy;
  for (i = 0; i <= h + 3; i++) {
    for (j = 0; j <= w + 3; j++) {
      a[i][j] = false;
      b[i][j] = INF, c[i][j] = -1;
    }
  }
  cx++, cy++, dx++, dy++;
  for (i = 2; i <= h + 1; i++) {
    for (j = 2; j <= w + 1; j++) {
      char s;
      cin >> s;
      if (s == '.') {
        a[i][j] = true;
      }
    }
    scanf("\\n");
  }
  queue<P> que;
  int count = 0;
  for (i = 2; i <= h + 1; i++) {
    for (j = 2; j <= w + 1; j++) {
      if (a[i][j] == true && c[i][j] == -1) {
        count++;
        que.push(P(i, j));
        c[i][j] = count;
        while (!que.empty()) {
          P p = que.front();
          que.pop();
          for (k = 0; k < 4; k++) {
            if (a[p.first + fx[k]][p.second + fy[k]] == true &&
                c[p.first + fx[k]][p.second + fy[k]] == -1) {
              c[p.first + fx[k]][p.second + fy[k]] = count;
              que.push(P(p.first + fx[k], p.second + fy[k]));
            }
          }
        }
      }
    }
  }
  que.push(P(cx, cy));
  b[cx][cy] = 0;
  vector<bool> fa(1000010, false);
  fa[c[cx][cy]] = true;
  while (!que.empty()) {
    P p = que.front();
    que.pop();
    queue<P> que1;
    for (i = 0; i < 4; i++) {
      if (c[p.first + fx[i]][p.second + fy[i]] == c[p.first][p.second]) {
        que.push(P(p.first + fx[i], p.second + fy[i]));
        for (j = -2; j <= 2; j++) {
          for (k = -2; k <= 2; k++) {
            if (fa[c[p.first + j][p.second + k]] == false &&
                b[p.first + j][p.second + k] > b[p.first][p.second] + 1 &&
                c[p.first + j][p.second + k] != -1 &&
                c[p.first + j][p.second + k] != c[p.first][p.second]) {
              que1.push(P(p.first + j, p.second + k));
              fa[c[p.first + j][p.second + k]] = true;
              b[p.first + j][p.second + k] = b[p.first][p.second] + 1;
            }
          }
        }
      }
    }
    if (que.empty()) {
      while (!que1.empty()) {
        P p1 = que1.front();
        que1.pop();
        que.push(P(p1.first, p1.second));
      }
    }
  }
  if (b[dx][dy] == INF) {
    cout << -1 << endl;
  } else {
    cout << b[dx][dy] << endl;
  }
}