Problem p02579 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
int x[4] = {-1, 0, 1, 0};
int y[4] = {0, 1, 0, -1};
queue<P> q;
char c[1100][1100];
int b[1100][1100];
int h, w, ch, dh, cw, dw;
int main() {
  cin >> h >> w;
  cin >> ch >> cw >> dh >> dw;
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      cin >> c[i][j];
    }
  }
  // memset(b,-1,sizeof(b));
  q.push(make_pair(ch, cw));
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      b[i][j] = -1;
    }
  }
  b[ch][cw] = 0;
  vector<vector<P>> v(100000);
  v[0].push_back(make_pair(ch, cw));
  int now;
  while (1) {
    int cnt = 0;
    while (!q.empty()) {
      P tmp = q.front();
      q.pop();
      int dx = tmp.first;
      int dy = tmp.second;
      now = b[dx][dy];
      // if(dx == dh && dy == dw){
      //     cout << b[dh][dw] << endl;
      //     return 0;
      // }
      for (int i = 0; i < 4; i++) {
        int xx = dx + x[i];
        int yy = dy + y[i];
        if (xx >= 1 && xx <= h && yy >= 1 && yy <= w && c[xx][yy] != '#' &&
            b[xx][yy] == -1) {
          b[xx][yy] = b[dx][dy];
          q.push(make_pair(xx, yy));
          v[now].push_back(make_pair(xx, yy));
        }
      }
    }
    for (int i = 0; i < v[now].size(); i++) {
      int dx = v[now][i].first;
      int dy = v[now][i].second;
      for (int j = -2; j <= 2; j++) {
        for (int k = -2; k <= 2; k++) {
          int xx = dx + j;
          int yy = dy + k;
          if (xx >= 1 && xx <= h && yy >= 1 && yy <= w && c[xx][yy] != '#' &&
              b[xx][yy] == -1) {
            b[xx][yy] = b[dx][dy] + 1;
            q.push(make_pair(xx, yy));
            int t = b[xx][yy];
            v[t].push_back(make_pair(xx, yy));
            cnt++;
          }
        }
      }
    }
    if (!cnt)
      break;
  }
  cout << b[dh][dw] << endl;
  // for(int i = 1;i<=h;i++){
  //     for(int j = 1;j<=w;j++){
  //         printf("%2d ",b[i][j]);
  //     }
  //     cout << endl;
  // }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
int x[4] = {-1, 0, 1, 0};
int y[4] = {0, 1, 0, -1};
queue<P> q;
char c[1100][1100];
int b[1100][1100];
int h, w, ch, dh, cw, dw;
int main() {
  cin >> h >> w;
  cin >> ch >> cw >> dh >> dw;
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      cin >> c[i][j];
    }
  }
  // memset(b,-1,sizeof(b));
  q.push(make_pair(ch, cw));
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      b[i][j] = -1;
    }
  }
  b[ch][cw] = 0;
  vector<vector<P>> v(1000010);
  v[0].push_back(make_pair(ch, cw));
  int now;
  while (1) {
    int cnt = 0;
    while (!q.empty()) {
      P tmp = q.front();
      q.pop();
      int dx = tmp.first;
      int dy = tmp.second;
      now = b[dx][dy];
      // if(dx == dh && dy == dw){
      //     cout << b[dh][dw] << endl;
      //     return 0;
      // }
      for (int i = 0; i < 4; i++) {
        int xx = dx + x[i];
        int yy = dy + y[i];
        if (xx >= 1 && xx <= h && yy >= 1 && yy <= w && c[xx][yy] != '#' &&
            b[xx][yy] == -1) {
          b[xx][yy] = b[dx][dy];
          q.push(make_pair(xx, yy));
          v[now].push_back(make_pair(xx, yy));
        }
      }
    }
    for (int i = 0; i < v[now].size(); i++) {
      int dx = v[now][i].first;
      int dy = v[now][i].second;
      for (int j = -2; j <= 2; j++) {
        for (int k = -2; k <= 2; k++) {
          int xx = dx + j;
          int yy = dy + k;
          if (xx >= 1 && xx <= h && yy >= 1 && yy <= w && c[xx][yy] != '#' &&
              b[xx][yy] == -1) {
            b[xx][yy] = b[dx][dy] + 1;
            q.push(make_pair(xx, yy));
            int t = b[xx][yy];
            v[t].push_back(make_pair(xx, yy));
            cnt++;
          }
        }
      }
    }
    if (!cnt)
      break;
  }
  cout << b[dh][dw] << endl;
  // for(int i = 1;i<=h;i++){
  //     for(int j = 1;j<=w;j++){
  //         printf("%2d ",b[i][j]);
  //     }
  //     cout << endl;
  // }

  return 0;
}