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

#include <bits/stdc++.h>
#define tmp(x) std::cout << "& " << (x) << " &\\n"
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define per(i, a, b) for (int i = (a); i >= (b); --i)
typedef long long ll;
using namespace std;
const int maxn = 1e3 + 10;
const int maxm = 1e6 + 100;
int h, w;
char a[maxn][maxn];
int d[maxn][maxn];
bool vis[maxn][maxn];

int cx, cy, dx, dy, ans;
int xx[20] = {-2, -2, -2, -2, -2, -1, -1, -1, -1, 0,
              0,  1,  1,  1,  1,  2,  2,  2,  2,  2},
    yy[20] = {-2, -1, 0,  1, 2, -2, -1, 1, 2, -2,
              2,  -2, -1, 1, 2, -2, -1, 0, 1, 2};
inline bool check(int x, int y) {
  return x >= 1 && x <= h && y >= 1 && y <= w && a[x][y] == '.';
}
void work() {
  deque<pair<int, int>> q;
  q.push_back(make_pair(cx, cy));
  d[cx][cy] = 0;
  while (!q.empty()) {
    int x = q.front().first, y = q.front().second;
    q.pop_front();
    if (d[x - 1][y] > d[x][y] && check(x - 1, y))
      q.push_front({x - 1, y}), d[x - 1][y] = d[x][y];
    if (d[x + 1][y] > d[x][y] && check(x + 1, y))
      q.push_front({x + 1, y}), d[x + 1][y] = d[x][y];
    if (d[x][y - 1] > d[x][y] && check(x, y - 1))
      q.push_front({x, y - 1}), d[x][y - 1] = d[x][y];
    if (d[x][y + 1] > d[x][y] && check(x, y + 1))
      q.push_front({x, y + 1}), d[x][y + 1] = d[x][y];
    for (int i = 0; i < 20; ++i) {
      int nx = x + xx[i], ny = y + yy[i];
      if (check(nx, ny)) {
        if (d[nx][ny] > d[x][y] + 1)
          d[nx][ny] = d[x][y] + 1, q.push_back({nx, ny});
      }
    }
  }
}
int main() {
  scanf("%d%d", &h, &w);
  scanf("%d%d", &cx, &cy);
  scanf("%d%d", &dx, &dy);
  rep(i, 1, h) { scanf("%s", a[i] + 1); }
  rep(i, 1, h) rep(j, 1, w) d[i][j] = maxm;
  work();
  if (d[dx][dy] == maxm)
    cout << -1;
  else
    cout << d[dx][dy];

  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define tmp(x) std::cout << "& " << (x) << " &\\n"
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define per(i, a, b) for (int i = (a); i >= (b); --i)
typedef long long ll;
#pragma - GCC - optimize("Ofast")

using namespace std;
const int maxn = 1e3 + 10;
const int maxm = 1e6 + 100;
int h, w;
char a[maxn][maxn];
int f[maxn][maxn];
bool vis[maxn][maxn];

int cx, cy, dx, dy;
int st, ed;
bool v2[maxm];
int tot, to[maxm * 30], nxt[maxm * 30], head[maxm];
inline void add(int u, int v) {
  to[++tot] = v, nxt[tot] = head[u], head[u] = tot;
}

void dfs(int x, int y, int v) {
  vis[x][y] = 1;
  f[x][y] = v;
  if (x - 1 >= 1 && a[x - 1][y] == '.' && !vis[x - 1][y])
    dfs(x - 1, y, v);
  if (x + 1 <= h && a[x + 1][y] == '.' && !vis[x + 1][y])
    dfs(x + 1, y, v);
  if (y - 1 >= 1 && a[x][y - 1] == '.' && !vis[x][y - 1])
    dfs(x, y - 1, v);
  if (y + 1 <= w && a[x][y + 1] == '.' && !vis[x][y + 1])
    dfs(x, y + 1, v);
}

pair<int, int> q[maxm];
int main() {
  scanf("%d%d", &h, &w);
  scanf("%d%d", &cx, &cy);
  scanf("%d%d", &dx, &dy);
  rep(i, 1, h) { scanf("%s", a[i] + 1); }
  int cnt = 0;
  rep(i, 1, h) {
    rep(j, 1, w) { f[i][j] = ++cnt; }
  }
  rep(i, 1, h) {
    rep(j, 1, w) {
      if (a[i][j] == '.' && !vis[i][j])
        dfs(i, j, f[i][j]);
    }
  }

  rep(i, 1, h) {
    rep(j, 1, w) {
      if (a[i][j] == '#')
        continue;
      rep(ii, -2, 2) {
        if (i + ii < 1 || i + ii > h)
          continue;
        rep(jj, -2, 2) {
          if ((ii == 0 && jj == 0) || j + jj < 1 || j + jj > w ||
              a[i + ii][j + jj] == '#')
            continue;
          if (f[i][j] == f[i + ii][j + jj])
            continue;
          else {
            int v1 = f[i][j], v22 = f[i + ii][j + jj];
            add(v1, v22);
          }
        }
      }
    }
  }

  st = f[cx][cy];
  ed = f[dx][dy];

  int ans = -1;
  int s = 0, t = 0;
  q[t++] = {st, 0};
  while (s <= t) {
    int x = q[s].first, c = q[s].second;
    ++s;
    v2[x] = 1;
    if (ed == x) {
      ans = c;
      break;
    }
    for (int i = head[x]; i; i = nxt[i]) {
      int u = to[i];
      if (v2[u])
        continue;
      q[t++] = make_pair(u, c + 1);
    }
  }
  if (v2[ed] == 0)
    ans = -1;
  cout << ans;

  return 0;
}