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

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e3 + 9;
const double ep = 1e-7;
const int mod = 1e9 + 7;
const double pi = acos(-1);
const int INF = 0x3f3f3f3f;
#define debug1 puts("?");
#define debug(x) cout << "##" << (x) << endl;
#define mk make_pair
#define PII pair<int, int>
#define PIII pair<int, PII>
#define PIL pair<int, ll>
#define PLI pair<ll, int>
#define eb emplace_back
#define fi first
#define se second
#define rep(i, n) for (register int i = 1; i <= (n); ++i)
#define sd(x) scanf("%d", &(x))
#define slld(x) scanf("%lld", &(x))
#define sdd(x, y) scanf("%d%d", &(x), &(y))
#define sc(s) scanf("%s", (s))
#define pd(x) printf("%d\\n", (x))
#define plld(x) printf("%lld\\n", (x))
#define pdk(x) printf("%d ", (x))
typedef long long ll;
int h, w, a[maxn], ans = 0;
int ch, cw, dh, dw;
vector<PII> vec;
char s[maxn][maxn];
bool vis[maxn][maxn];
int dir[4][2] = {1, 0, 0, 1, -1, 0, 0, -1};
bool check(int x, int y) {
  if (x <= 0 || x > h || y <= 0 || y > w)
    return 0;
  if (s[x][y] == '#' || vis[x][y])
    return 0;
  return 1;
}
int bfs() {
  int now = 0;
  queue<PII> q;
  q.push(mk(ch, cw));
  vis[ch][cw] = 1;
  while (1) {
    if (q.empty())
      break;
    while (!q.empty()) {
      PII p = q.front();
      q.pop();
      vec.eb(mk(p.fi, p.se));
      for (int i = 0; i < 4; i++) {
        int x = p.fi + dir[i][0], y = p.se + dir[i][1];
        if (!check(x, y))
          continue;
        q.push(mk(x, y));
        vis[x][y] = 1;
        if (x == dh && y == dw)
          return now;
      }
    }
    for (PII j : vec) {
      for (int i = -2; i <= 2; i++) {
        for (int k = -2; k <= 2; k++) {
          int x = j.fi + i, y = j.se + k;
          if (!check(x, y))
            continue;
          q.push(mk(x, y));
          vis[x][y] = 1;
          if (x == dh && y == dw)
            return now + 1;
        }
      }
    }
    vec.clear();
    now++;
  }
  return -1;
}
int main() {
  sdd(h, w);
  sdd(ch, cw);
  sdd(dh, dw);
  rep(i, h) sc(s[i] + 1);
  printf("%d\\n", bfs());
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e3 + 9;
const double ep = 1e-7;
const int mod = 1e9 + 7;
const double pi = acos(-1);
const int INF = 0x3f3f3f3f;
#define debug1 puts("?");
#define debug(x) cout << "##" << (x) << endl;
#define mk make_pair
#define PII pair<int, int>
#define PIII pair<int, PII>
#define PIL pair<int, ll>
#define PLI pair<ll, int>
#define eb emplace_back
#define fi first
#define se second
#define rep(i, n) for (register int i = 1; i <= (n); ++i)
#define sd(x) scanf("%d", &(x))
#define slld(x) scanf("%lld", &(x))
#define sdd(x, y) scanf("%d%d", &(x), &(y))
#define sc(s) scanf("%s", (s))
#define pd(x) printf("%d\\n", (x))
#define plld(x) printf("%lld\\n", (x))
#define pdk(x) printf("%d ", (x))
typedef long long ll;
int h, w, a[maxn], ans = 0;
int ch, cw, dh, dw;
vector<PII> vec[maxn];
char s[maxn][maxn];
bool vis[maxn][maxn];
int dir[4][2] = {1, 0, 0, 1, -1, 0, 0, -1};
bool check(int x, int y) {
  if (x <= 0 || x > h || y <= 0 || y > w)
    return 0;
  if (s[x][y] == '#' || vis[x][y])
    return 0;
  return 1;
}
int bfs() {
  int now = 0;
  queue<PII> q;
  q.push(mk(ch, cw));
  vis[ch][cw] = 1;
  while (1) {
    if (q.empty())
      break;
    while (!q.empty()) {
      PII p = q.front();
      q.pop();
      vec[now].eb(mk(p.fi, p.se));
      for (int i = 0; i < 4; i++) {
        int x = p.fi + dir[i][0], y = p.se + dir[i][1];
        if (!check(x, y))
          continue;
        q.push(mk(x, y));
        vis[x][y] = 1;
        if (x == dh && y == dw)
          return now;
      }
    }
    for (PII j : vec[now]) {
      for (int i = -2; i <= 2; i++) {
        for (int k = -2; k <= 2; k++) {
          int x = j.fi + i, y = j.se + k;
          if (!check(x, y))
            continue;
          q.push(mk(x, y));
          vis[x][y] = 1;
          if (x == dh && y == dw)
            return now + 1;
        }
      }
    }
    now++;
  }
  return -1;
}
int main() {
  sdd(h, w);
  sdd(ch, cw);
  sdd(dh, dw);
  rep(i, h) sc(s[i] + 1);
  printf("%d\\n", bfs());
}