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

#include <bits/stdc++.h>
#define Rushia_mywife                                                          \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);
#define rep(i, head, n) for (int i = (head); i < n; i++)
#define int long long
#define F first
#define S second
#define pb push_back
#define p2(n) (1 << (n))
#define lg2(n) __lg(n)
using namespace std;
using pii = pair<long long, long long>;
using ld = long double;
mt19937 mt_rand(time(0));
const int mod = 1000000007;
const int hnum = 998244353;
const ld PI = acos(-1);

int n, m;
int sx, sy, ex, ey;
int a[1001][1010];
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, -1, 1};

bool can(int x, int y) { return x > 0 && x <= n && y > 0 && y <= m; }

void solve() {
  cin >> n >> m;
  cin >> sx >> sy;
  cin >> ex >> ey;
  rep(i, 1, n + 1) {
    rep(j, 1, m + 1) {
      char x;
      cin >> x;
      if (x == '.')
        a[i][j] = 0;
      else
        a[i][j] = 1;
    }
  }
  deque<pair<pii, int>> q;
  q.push_back({{sx, sy}, 0});
  while (!q.empty()) {
    pair<pii, int> now = q.front();
    q.pop_front();
    if (now.F.F == ex && now.F.S == ey) {
      cout << now.S << '\\n';
      return;
    }
    if (a[now.F.F][now.F.S])
      continue;
    a[now.F.F][now.F.S] = 1;
    rep(i, 0, 4) if (can(now.F.F + dx[i], now.F.S + dy[i]) &&
                     !a[now.F.F + dx[i]][now.F.S + dy[i]])
        q.push_front({{now.F.F + dx[i], now.F.S + dy[i]}, now.S});
    rep(i, -2, 3) rep(j, -2, 3) if (can(now.F.F + i, now.F.S + j) &&
                                    !a[now.F.F + i][now.F.S + j])
        q.push_back({{now.F.F + i, now.F.S + j}, now.S + 1});
  }
  cout << -1 << '\\n';
}

signed main() { Rushia_mywife solve(); }

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define Rushia_mywife                                                          \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);
#define rep(i, head, n) for (int i = (head); i < n; i++)
#define int long long
#define F first
#define S second
#define pb push_back
#define p2(n) (1 << (n))
#define lg2(n) __lg(n)
using namespace std;
using pii = pair<long long, long long>;
using ld = long double;
mt19937 mt_rand(time(0));
const int mod = 1000000007;
const int hnum = 998244353;
const ld PI = acos(-1);

int dx[] = {0, 0,  1, -1, 1, -1, 1,  -1, 2, -2, 0, 0,
            2, -2, 2, -2, 2, 2,  -2, -2, 1, -1, 1, -1};
int dy[] = {1, -1, 0,  0,  1, 1,  -1, -1, 0, 0, 2,  -2,
            2, 2,  -2, -2, 1, -1, 1,  -1, 2, 2, -2, -2};

int n, m;
int sx, sy, ex, ey;
int dis[1010][1010];
int a[1001][1010];

bool can(int x, int y) { return x > 0 && x <= n && y > 0 && y <= m; }

void dfs(int x, int y) {
  a[x][y] = 2;
  rep(i, 0, 4) if (can(x + dx[i], y + dy[i]) && !a[x + dx[i]][y + dy[i]])
      dfs(x + dx[i], y + dy[i]);
}

void solve() {
  rep(i, 0, 1011) rep(j, 0, 1001) dis[i][j] = (int)1e17;
  cin >> n >> m;
  cin >> sx >> sy;
  cin >> ex >> ey;
  rep(i, 1, n + 1) {
    rep(j, 1, m + 1) {
      char x;
      cin >> x;
      if (x == '.')
        a[i][j] = 0;
      else
        a[i][j] = 1;
    }
  }
  dfs(sx, sy);
  queue<pair<pii, int>> q;
  q.push({{ex, ey}, 0});
  while (!q.empty()) {
    pair<pii, int> now = q.front();
    now.S = min(now.S, dis[now.F.F][now.F.S]);
    if (a[now.F.F][now.F.S] == 2) {
      cout << now.S << '\\n';
      return;
    }
    q.pop();
    rep(i, 0, 24) {
      if (can(now.F.F + dx[i], now.F.S + dy[i]) &&
          a[now.F.F + dx[i]][now.F.S + dy[i]] != 1) {
        q.push({{now.F.F + dx[i], now.F.S + dy[i]}, now.S + 1});
        a[now.F.F + dx[i]][now.F.S + dy[i]] =
            max(a[now.F.F + dx[i]][now.F.S + dy[i]], 1LL);
        dis[now.F.F + dx[i]][now.F.S + dy[i]] =
            min(dis[now.F.F + dx[i]][now.F.S + dy[i]], now.S + 1);
      }
    }
  }
  cout << -1 << '\\n';
}

signed main() { Rushia_mywife solve(); }