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

#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define all(a) a.begin(), a.end()
#define pb push_back
#define mp make_pair
typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<ll, int> pli;
typedef pair<int, int> pii;
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define qrep(que, ite) for (auto ite = begin(que); ite != end(que); ite++)
const int max_n = 1e5;
const ll mod = 1e9 + 7;
const ll INF = 1e17;
const int inf = 1e5;
typedef long double ld;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
ll gcd(ll a, ll b) { return a ? gcd(b % a, a) : b; }
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
struct UnionFind {
  vector<int> par;

  UnionFind(int n) : par(n, -1) {}
  void init(int n) { par.assign(n, -1); }

  int root(int x) {
    if (par[x] < 0)
      return x;
    else
      return par[x] = root(par[x]);
  }

  bool issame(int x, int y) { return root(x) == root(y); }

  bool merge(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    if (par[x] > par[y])
      swap(x, y); // merge technique
    par[x] += par[y];
    par[y] = x;
    return true;
  }

  int size(int x) { return -par[root(x)]; }
};
struct SegmentTree {
  int N;
  vector<int> node;

public:
  void intit(vector<int> v) {
    int sz = v.size();
    N = 1;
    while (N < sz)
      N *= 2;
    node.resize(N);
    for (int i = 0; i < sz; i++)
      node[i + N - 1] = v[i];
    for (int i = N - 2; i >= 0; i--)
      node[i] = min(node[i * 2 + 1], node[i * 2 + 2]);
  }
  void update(int x, int val) {
    x += N - 1;
    node[x + N - 1] = val;
    while (x > 0) {
      x = (x - 1) / 2;
      node[x] = min(node[x * 2 + 1], node[x * 2 + 2]);
    }
  }
  int getmin(int a, int b, int k, int l, int r) {
    if (b <= l || r <= a)
      return inf;
    else if (a <= l && r <= b)
      return node[k];
    else {
      int vl = getmin(a, b, 2 * k + 1, l, (l + r) / 2);
      int vr = getmin(a, b, 2 * k + 2, (l + r) / 2, r);
      return min(vl, vr);
    }
  }
};
vector<ll> divisor(ll n) {
  vector<ll> res;
  for (ll i = 1; i * i <= n; i++) {
    while (n % i == 0) {
      ++res[i];
      n /= i;
    }
  }
  if (n != 1)
    res[n] = 1;
  return res;
}
int main() {
  ll H, W;
  cin >> H >> W;
  ll sx, sy, gx, gy;
  cin >> sy >> sx >> gy >> gx;
  sx--;
  sy--;
  gx--;
  gy--;
  string s[H];
  rep(i, 0, H) cin >> s[i];
  ll d[1500][1500], ok[1000][1000];
  rep(i, 0, 1500) rep(j, 0, 1500) {
    d[i][j] = INF;
    ok[i][j] = 0;
  }
  d[sy][sx] = 0;
  queue<P> que, visited;
  que.push(make_pair(sx, sy));
  while (que.size() || visited.size()) {
    if (que.size()) {
      pii p = que.front();
      que.pop();
      ll x = p.fi, y = p.se;
      visited.push(make_pair(x, y));
      rep(i, 0, 4) {
        ll nx = x + dx[i], ny = y + dy[i];
        if (nx < 0 || ny < 0 || nx >= W || ny >= H)
          continue;
        if (s[ny][nx] == '.' && d[ny][nx] > d[y][x]) {
          que.push(make_pair(nx, ny));
          d[ny][nx] = d[y][x];
        }
      }
    } else {
      pii p = visited.front();
      ll x = p.fi, y = p.se;
      visited.pop();
      rep(i, -2, 3) rep(j, -2, 3) {
        ll nx = x + i, ny = y + j;
        if (nx < 0 || ny < 0 || nx >= W || ny >= H || s[ny][nx] == '#')
          continue;
        if (abs(i) + abs(j) == 1)
          continue;
        if (d[ny][nx] > d[y][x] + 1) {
          d[ny][nx] = d[y][x] + 1;
          que.push(make_pair(nx, ny));
        }
      }
    }
  }
  if (d[gy][gx] == INF) {
    cout << -1 << endl;
  } else {
    cout << d[gy][gx] << endl;
  }
  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define all(a) a.begin(), a.end()
#define pb push_back
#define mp make_pair
typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<ll, int> pli;
typedef pair<int, int> pii;
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define qrep(que, ite) for (auto ite = begin(que); ite != end(que); ite++)
const int max_n = 1e5;
const ll mod = 1e9 + 7;
const ll INF = 1e17;
const int inf = 1e5;
typedef long double ld;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
ll gcd(ll a, ll b) { return a ? gcd(b % a, a) : b; }
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
struct UnionFind {
  vector<int> par;

  UnionFind(int n) : par(n, -1) {}
  void init(int n) { par.assign(n, -1); }

  int root(int x) {
    if (par[x] < 0)
      return x;
    else
      return par[x] = root(par[x]);
  }

  bool issame(int x, int y) { return root(x) == root(y); }

  bool merge(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    if (par[x] > par[y])
      swap(x, y); // merge technique
    par[x] += par[y];
    par[y] = x;
    return true;
  }

  int size(int x) { return -par[root(x)]; }
};
struct SegmentTree {
  int N;
  vector<int> node;

public:
  void intit(vector<int> v) {
    int sz = v.size();
    N = 1;
    while (N < sz)
      N *= 2;
    node.resize(N);
    for (int i = 0; i < sz; i++)
      node[i + N - 1] = v[i];
    for (int i = N - 2; i >= 0; i--)
      node[i] = min(node[i * 2 + 1], node[i * 2 + 2]);
  }
  void update(int x, int val) {
    x += N - 1;
    node[x + N - 1] = val;
    while (x > 0) {
      x = (x - 1) / 2;
      node[x] = min(node[x * 2 + 1], node[x * 2 + 2]);
    }
  }
  int getmin(int a, int b, int k, int l, int r) {
    if (b <= l || r <= a)
      return inf;
    else if (a <= l && r <= b)
      return node[k];
    else {
      int vl = getmin(a, b, 2 * k + 1, l, (l + r) / 2);
      int vr = getmin(a, b, 2 * k + 2, (l + r) / 2, r);
      return min(vl, vr);
    }
  }
};
vector<ll> divisor(ll n) {
  vector<ll> res;
  for (ll i = 1; i * i <= n; i++) {
    while (n % i == 0) {
      ++res[i];
      n /= i;
    }
  }
  if (n != 1)
    res[n] = 1;
  return res;
}
int main() {
  int H, W;
  cin >> H >> W;
  int sx, sy, gx, gy;
  cin >> sy >> sx >> gy >> gx;
  sx--;
  sy--;
  gx--;
  gy--;
  string s[H];
  rep(i, 0, H) cin >> s[i];
  int d[1000][1000], ok[1000][1000];
  rep(i, 0, H) rep(j, 0, W) {
    d[i][j] = inf;
    ok[i][j] = 0;
  }
  d[sy][sx] = 0;
  queue<pii> que, visited;
  que.push(make_pair(sx, sy));
  while (que.size() || visited.size()) {
    if (que.size()) {
      pii p = que.front();
      que.pop();
      int x = p.fi, y = p.se;
      visited.push(make_pair(x, y));
      rep(i, 0, 4) {
        int nx = x + dx[i], ny = y + dy[i];
        if (s[ny][nx] == '.' && d[ny][nx] > d[y][x]) {
          que.push(make_pair(nx, ny));
          d[ny][nx] = d[y][x];
        }
      }
    } else {
      pii p = visited.front();
      int x = p.fi, y = p.se;
      visited.pop();
      rep(i, -2, 3) rep(j, -2, 3) {
        int nx = x + i, ny = y + j;
        if (nx < 0 || ny < 0 || nx >= W || ny >= H || s[ny][nx] == '#')
          continue;
        if (abs(i) + abs(j) == 1)
          continue;
        if (d[ny][nx] > d[y][x] + 1) {
          d[ny][nx] = d[y][x] + 1;
          que.push(make_pair(nx, ny));
        }
      }
    }
  }
  if (d[gy][gx] == inf) {
    cout << -1 << endl;
  } else {
    cout << d[gy][gx] << endl;
  }
  return 0;
}