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

#include <bits/stdc++.h>
using namespace std;
#define endl '\\n'
#define ll long long
#define FASTINOUT                                                              \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
int r, c, px, py, ex, ey;
char a[1009][1009];
bool visited[1009][1009];
int ox[4] = {0, 0, 1, -1}, oy[] = {1, -1, 0, 0};
bool ok(int x, int y) {
  if (x >= 0 && x < r && y >= 0 && y < c && a[x][y] != '#')
    return 1;
  return 0;
}
void bfs() {
  deque<pair<pair<int, int>, int>> d;
  d.push_back({{px - 1, py - 1}, 0});
  while (!d.empty()) {
    pair<pair<int, int>, int> t = d.front();
    d.pop_front();
    int cx = t.first.first;
    int cy = t.first.second;
    int cnt = t.second;
    if (cx == ex - 1 && cy == ey - 1) {
      cout << cnt << endl;
      exit(0);
    }
    if (visited[cx][cy])
      continue;
    visited[cx][cy] = 1;
    for (int i = 0; i < 4; i++) {
      int x = cx + ox[i];
      int y = cy + oy[i];
      if (ok(x, y))
        d.push_front({{x, y}, cnt});
    }
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        int x = cx + i;
        int y = cy + j;
        if (ok(x, y))
          d.push_back({{x, y}, cnt + 1});
      }
    }
  }
  cout << -1 << endl;
  exit(0);
}
int main() {
  FASTINOUT;
  cin >> r >> c >> px >> py >> ex >> ey;
  for (int i = 0; i < r; i++) {
    for (int j = 0; j < c; j++) {
      cin >> a[i][j];
    }
  }
  bfs();
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\\n'
#define FASTINOUT                                                              \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
int r, c, px, py, ex, ey, ans = 1e9;
char a[1009][1009];
bool visited[1009][1009][2];
int ox[] = {1, -1, 0, 0};
int oy[] = {0, 0, 1, -1};
int xx[] = {1, 1, 1,  1,  1,  0,  0,  0,  0,  0,  2,  2, 2,
            2, 2, -1, -1, -1, -1, -1, -2, -2, -2, -2, -2};
int yy[] = {0,  1,  2, -1, -2, 0,  1,  2, -1, -2, 0,  1, 2,
            -1, -2, 0, 1,  2,  -1, -2, 0, 1,  2,  -1, -2};
int dis[1009][1009];
bool check(int x, int y, int t) {
  if (x > 0 && x <= r && y > 0 && y <= c && !visited[x][y][t] && a[x][y] == '.')
    return 1;
  return 0;
}
void dfs(int x, int y, int type) {
  /* if (x==2&&y==2)
   {
       cout<<cnt<<"   "<<type<<endl;
   }*/
  visited[x][y][type] = 1;
  for (int i = 0; i < 4; i++) {
    int mx = x + ox[i];
    int my = y + oy[i];
    dis[mx][my] = min(dis[mx][my], dis[x][y]);
    if (check(mx, my, 0)) {
      dfs(mx, my, 0);
    }
  }
  for (int i = 0; i < 25; i++) {
    int mx = x + xx[i];
    int my = y + yy[i];
    dis[mx][my] = min(dis[mx][my], dis[x][y] + 1);
    if (check(mx, my, 1)) {
      dfs(mx, my, 1);
    }
  }
}
int main() {
  FASTINOUT;
  cin >> r >> c >> px >> py >> ex >> ey;
  for (int i = 1; i <= r; i++) {
    for (int j = 1; j <= c; j++) {
      cin >> a[i][j];
    }
  }
  for (int i = 1; i <= r; i++) {
    for (int j = 1; j <= c; j++) {
      dis[i][j] = 1e9;
    }
  }
  dis[px][py] = 0;
  dfs(px, py, 0);
  cout << (dis[ex][ey] == 1e9 ? -1 : dis[ex][ey]);
  return 0;
}