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

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

#define pb push_back
#define mp make_pair
#define all(n) (n).begin(), (n).end()
#define fi first
#define se second

typedef pair<int, int> PII;
typedef long long ll;

const int MOD = 1e9 + 7;

int a[1005][1005], d[1005][1005];
int h, w, sx, sy, ex, ey;
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
int main() {

  ios::sync_with_stdio(0);
  cin.tie(0);
  memset(d, 0x3f3f3f3f, sizeof(d));
  cin >> h >> w >> sx >> sy >> ex >> ey;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      char c;
      cin >> c;
      if (c == '#')
        a[i][j] = 1;
    }
  }
  sx--, sy--, ex--, ey--;
  deque<PII> q;
  q.push_back(mp(sx, sy));
  d[sx][sy] = 0;
  while (!q.empty()) {
    PII cur = q.front();
    q.pop_front();
    for (int i = 0; i < 4; i++) {
      int nx = cur.fi + dx[i];
      int ny = cur.se + dy[i];
      if (nx < 0 || ny < 0 || nx >= h || ny >= w || a[nx][ny])
        continue;
      if (d[nx][ny] > d[cur.fi][cur.se]) {
        d[nx][ny] = d[cur.fi][cur.se];
        q.push_front(mp(nx, ny));
      }
    }
    for (int i = cur.fi - 2; i <= cur.fi + 2; ++i) {
      for (int j = cur.se - 2; j <= cur.se + 2; ++j) {
        if (i < 0 || j < 0 || i >= h || j >= w || a[i][j])
          continue;
        if (d[i][j] > d[cur.fi][cur.se] + 1) {
          d[i][j] = d[cur.fi][cur.se] + 1;
          q.push_back(mp(i, j));
        }
      }
    }
  }

  cout << (d[ex][ey] > 1e7 ? -1 : d[ex][ey]) << "\\n";
}

Error: WA

Fixed submission:

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

#define pb push_back
#define mp make_pair
#define all(n) (n).begin(), (n).end()
#define fi first
#define se second

typedef pair<int, int> PII;
typedef long long ll;

const int MOD = 1e9 + 7;

int a[1000][1000], vis[1000][1000];
int h, w, x, y, d, e;

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  cin >> h >> w >> x >> y >> d >> e;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      char c;
      cin >> c;
      if (c == '#')
        a[i][j] = 1;
    }
  }
  queue<vector<int>> q, q2;
  q.push({x - 1, y - 1});
  int ans = 0;
  bool ok = false;
  int dx[8] = {1, -1, 0, 0, 1, 1, -1, -1}, dy[8] = {0, 0, 1, -1, 1, -1, 1, -1};
  while (true) {
    if (q.empty())
      break;
    while (!q.empty()) {
      vector<int> b = q.front();
      q.pop();
      if (min(b[0], b[1]) < 0 || b[0] >= h || b[1] >= w || vis[b[0]][b[1]])
        continue;
      vis[b[0]][b[1]] = 1;
      if (a[b[0]][b[1]]) {
        q2.push({b[0], b[1]});
        continue;
      }
      for (int i = 0; i < 8; ++i)
        q.push({b[0] + dx[i], b[1] + dy[i]});
    }
    while (!q2.empty()) {
      // cout << q2.size() << " sz\\n";
      vector<int> b = q2.front();
      q2.pop();
      if (min(b[0], b[1]) < 0 || b[0] >= h || b[1] >= w)
        continue;
      vis[b[0]][b[1]] = 1;
      for (int i = 0; i < 8; i++) {
        if (!a[b[0] + dx[i]][b[1] + dy[i]] && !vis[b[0] + dx[i]][b[1] + dy[i]])
          q.push({b[0] + dx[i], b[1] + dy[i]});
      }
    }
    if (vis[d - 1][e - 1]) {
      ok = true;
      break;
    }
    ans++;
  }

  if (ok)
    cout << ans << "\\n";
  else
    cout << "-1\\n";
}