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

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
typedef vector<ll> vl;
typedef vector<bool> vb;
typedef vector<string> vs;
typedef vector<char> vc;
typedef queue<ll> ql;
typedef deque<ll> dql;
typedef priority_queue<ll /*, vl, greater<ll>*/> pql; // 降順(/*昇順*/)
typedef set<ll> sl;
typedef pair<ll, ll> pl;
typedef vector<vl> vvl;
typedef vector<pl> vpl;
#define rep(i, n) for (ll i = 0; i < ll(n); i++)
#define rep2(i, k, n) for (ll i = ll(k); i <= ll(n); i++)
#define rep3(i, n, k) for (ll i = ll(n); i >= ll(k); i--)
#define all(v) (v).begin(), (v).end()
ll mod(ll a, ll b) {
  if (b == 0)
    return 0;
  return (a % b + b) % b;
}
bool chmin(ll &a, ll b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
bool chmax(ll &a, ll b) {
  if (b > a) {
    a = b;
    return 1;
  }
  return 0;
}
const ll INF = 1LL << 60;
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
const ll MAX = 1e9;
const char newl = '\\n';

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

  ll h, w, ch, cw, dh, dw;
  cin >> h >> w >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;

  vector<vc> s(h, vc(w));
  rep(i, h) rep(j, w) cin >> s[i][j];

  vvl magic(h, vl(w, INF));
  magic[ch][cw] = 0;

  ll num = 0;
  queue<pl> q;
  q.push(pl(ch, cw));

  while (1) {
    bool f = 0;
    vpl Q;

    while (!q.empty()) {
      pl p = q.front();
      q.pop();
      Q.push_back(p);
      ll x = p.first, y = p.second;

      if (x - 1 >= 0 and s[x - 1][y] == '.') {
        if (chmin(magic[x - 1][y], magic[x][y]))
          q.push(pl(x - 1, y));
      }
      if (x + 1 < h and s[x + 1][y] == '.') {
        if (chmin(magic[x + 1][y], magic[x][y]))
          q.push(pl(x + 1, y));
      }
      if (y - 1 >= 0 and s[x][y - 1] == '.') {
        if (chmin(magic[x][y - 1], magic[x][y]))
          q.push(pl(x, y - 1));
      }
      if (y + 1 < w and s[x][y + 1] == '.') {
        if (chmin(magic[x][y + 1], magic[x][y]))
          q.push(pl(x, y + 1));
      }
    }

    for (pl P : Q) {
      ll X = P.first, Y = P.second;
      rep2(i, -2, 2)
          rep2(j, -2, 2) if (X + i >= 0 and X + i < h and Y + j >= 0 and
                             Y + j < w and magic[X + i][Y + j] == INF and
                             s[X + i][Y + j] == '.') {
        f = 1;
        magic[X + i][Y + j] = num + 1;
        q.push(pl(X + i, Y + j));
      }
    }
    num++;

    if (!f)
      break;
  }

  if (magic[dh][dw] == INF)
    cout << -1 << newl;
  else
    cout << magic[dh][dw] << newl;
  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
typedef vector<ll> vl;
typedef vector<bool> vb;
typedef vector<string> vs;
typedef vector<char> vc;
typedef queue<ll> ql;
typedef deque<ll> dql;
typedef priority_queue<ll /*, vl, greater<ll>*/> pql; // 降順(/*昇順*/)
typedef set<ll> sl;
typedef pair<ll, ll> pl;
typedef vector<vl> vvl;
typedef vector<pl> vpl;
#define rep(i, n) for (ll i = 0; i < ll(n); i++)
#define rep2(i, k, n) for (ll i = ll(k); i <= ll(n); i++)
#define rep3(i, n, k) for (ll i = ll(n); i >= ll(k); i--)
#define all(v) (v).begin(), (v).end()
ll mod(ll a, ll b) {
  if (b == 0)
    return 0;
  return (a % b + b) % b;
}
bool chmin(ll &a, ll b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
bool chmax(ll &a, ll b) {
  if (b > a) {
    a = b;
    return 1;
  }
  return 0;
}
const ll INF = 1LL << 60;
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
const ll MAX = 1e9;
const char newl = '\\n';

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

  ll h, w, ch, cw, dh, dw;
  cin >> h >> w >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;

  vector<vc> s(h, vc(w));
  rep(i, h) rep(j, w) cin >> s[i][j];

  vvl magic(h, vl(w, INF));
  magic[ch][cw] = 0;

  ll num = 0;
  queue<pl> q;
  q.push(pl(ch, cw));

  while (1) {
    bool f = 0;
    vpl Q;

    while (!q.empty()) {
      pl p = q.front();
      q.pop();
      Q.push_back(p);
      ll x = p.first, y = p.second;

      if (x - 1 >= 0 and s[x - 1][y] == '.') {
        if (chmin(magic[x - 1][y], magic[x][y]))
          q.push(pl(x - 1, y));
      }
      if (x + 1 < h and s[x + 1][y] == '.') {
        if (chmin(magic[x + 1][y], magic[x][y]))
          q.push(pl(x - 1, y));
      }
      if (y - 1 >= 0 and s[x][y - 1] == '.') {
        if (chmin(magic[x][y - 1], magic[x][y]))
          q.push(pl(x - 1, y));
      }
      if (y + 1 < w and s[x][y + 1] == '.') {
        if (chmin(magic[x][y + 1], magic[x][y]))
          q.push(pl(x - 1, y));
      }
    }

    for (pl P : Q) {
      ll X = P.first, Y = P.second;
      rep2(i, -2, 2)
          rep2(j, -2, 2) if (X + i >= 0 and X + i < h and Y + j >= 0 and
                             Y + j < w and magic[X + i][Y + j] == INF and
                             s[X + i][Y + j] == '.') {
        f = 1;
        magic[X + i][Y + j] = num + 1;
        q.push(pl(X + i, Y + j));
      }
    }
    num++;

    if (!f)
      break;
  }

  if (magic[dh][dw] == INF)
    cout << -1 << newl;
  else
    cout << magic[dh][dw] << newl;
  return 0;
}