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

#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define pii pair<int, int>
#define pp pair<pair<ll, ll>, pair<ll, ll>>
#define pll pair<ll, ll>
#define ppll pair<ll, pll>
#define pdd pair<double, double>
#define vii vector<int>
#define vll vector<ll>
#define mat vector<vector<ll>>
#define lb lower_bound
#define ub upper_bound
#define pb push_back
#define eb emplace_back
#define fi first
#define sc second
#define rep(i, n) for (ll i = 0; i < n; i++)
#define rep2(i, a, b) for (ll i = a; i < b; i++)
#define repr(i, n) for (ll i = n - 1; i >= 0; i--)
#define all(x) x.begin(), x.end()
#define sz(x) (ll)(x).size()
#define pq priority_queue<ll>
#define pqg priority_queue<ll, vector<ll>, greater<ll>>
#define LB(v, x) (lower_bound(v.begin(), v.end(), x) - v.begin())
#define UB(v, x) (upper_bound(v.begin(), v.end(), x) - v.begin())
#define ERASE(v)                                                               \\
  sort(v.begin(), v.end());                                                    \\
  v.erase(unique(v.begin(), v.end()), v.end())
#define int ll
// #define ll int
using namespace std;
const ll INF = 1 << 29;
const ll LLINF = (1LL << 60LL);
const ll MOD = 1000000007;
const int MAX_V = 500;
// const ll mod = 998244353;
const ll MAX = 2100000;
const double pi = acos(-1);
const double eps = 1e-10;
ll dx[8] = {1, 0, -1, 0, 1, -1, 1, -1};
ll dy[8] = {0, 1, 0, -1, 1, -1, -1, 1};
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 H, W;
ll sx, sy, tx, ty;
string s[1010];

struct edge {
  ll to;
  ll cost;
};
vector<edge> G[1000010];
ll d[1000010];

void dijkstra(int s) {
  priority_queue<pii, vector<pii>, greater<pii>> que;
  fill(d, d + 1000001, LLINF);
  d[s] = 0;
  que.push(pii(0, s));

  while (!que.empty()) {
    pii p = que.top();
    que.pop();
    int v = p.second;
    if (d[v] < p.first)
      continue;
    rep(i, (ll)G[v].size()) {
      edge e = G[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        que.push(pii(d[e.to], e.to));
      }
    }
  }
}

void solve() {
  cin >> H >> W;
  cin >> sx >> sy >> tx >> ty;
  sx--, sy--, tx--, ty--;
  ll S = sx * H + sy;
  ll Go = tx * H + ty;
  rep(i, H) cin >> s[i];
  rep(x, H) {
    rep(y, W) {
      if (s[x][y] == '#')
        continue;
      ll index = x * H + y;
      rep2(i, -2, 3) {
        rep2(j, -2, 3) {
          if (i == 0 && j == 0)
            continue;
          ll nx = x + i, ny = y + j;
          if (nx >= 0 && nx < H && ny >= 0 && ny < W && s[nx][ny] == '.') {
            ll to = nx * H + ny;
            if (abs(i) + abs(j) == 1) {
              G[index].pb({to, 0});
              // G[to].pb({index, 0});
            } else {
              G[index].pb({to, 1});
              // G[to].pb({index, 1});
            }
          }
        }
      }
    }
  }
  dijkstra(S);
  // rep(i,H*W){
  //     cout<<i<<endl;
  //     for(edge j : G[i]){
  //         cout<<j.to<<" "<<j.cost<<", ";
  //     }
  //     cout<<endl;
  // }
  if (d[Go] == LLINF)
    cout << -1 << endl;
  else
    cout << d[Go] << endl;
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);

  ll t = 1;
  rep(i, t) { solve(); }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define pii pair<int, int>
#define pp pair<pair<ll, ll>, pair<ll, ll>>
#define pll pair<ll, ll>
#define ppll pair<ll, pll>
#define pdd pair<double, double>
#define vii vector<int>
#define vll vector<ll>
#define mat vector<vector<ll>>
#define lb lower_bound
#define ub upper_bound
#define pb push_back
#define eb emplace_back
#define fi first
#define sc second
#define rep(i, n) for (ll i = 0; i < n; i++)
#define rep2(i, a, b) for (ll i = a; i < b; i++)
#define repr(i, n) for (ll i = n - 1; i >= 0; i--)
#define all(x) x.begin(), x.end()
#define sz(x) (ll)(x).size()
#define pq priority_queue<ll>
#define pqg priority_queue<ll, vector<ll>, greater<ll>>
#define LB(v, x) (lower_bound(v.begin(), v.end(), x) - v.begin())
#define UB(v, x) (upper_bound(v.begin(), v.end(), x) - v.begin())
#define ERASE(v)                                                               \\
  sort(v.begin(), v.end());                                                    \\
  v.erase(unique(v.begin(), v.end()), v.end())
// #define int ll
// #define ll int
using namespace std;
const ll INF = 1 << 29;
const ll LLINF = (1LL << 60LL);
const ll MOD = 1000000007;
const int MAX_V = 500;
// const ll mod = 998244353;
const ll MAX = 2100000;
const double pi = acos(-1);
const double eps = 1e-10;
ll dx[8] = {1, 0, -1, 0, 1, -1, 1, -1};
ll dy[8] = {0, 1, 0, -1, 1, -1, -1, 1};
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 ans[1010][1010];
bool ok[1010][1010];

void solve() {
  ll H, W;
  cin >> H >> W;
  ll sx, sy, tx, ty;
  cin >> sx >> sy >> tx >> ty;
  sx--, sy--, tx--, ty--;
  string s[200010];
  rep(i, H) cin >> s[i];
  priority_queue<pll, vector<pll>, greater<pll>> que[5010], que2[5010];
  rep(i, 1010) rep(j, 1010) ans[i][j] = LLINF;
  ans[sx][sy] = 0;
  que[0].push({sx, sy});
  ll cnt = 0;

  rep(i, 4910) {

    while (!que[i].empty()) {
      cnt++;
      pll xy = que[i].top();
      que[i].pop();
      que2[i].push(xy);
      ll x = xy.fi, y = xy.sc;
      // if(ans[x][y] < i) continue;
      ok[x][y] = true;
      rep(i, 4) {
        ll nx = x + dx[i], ny = y + dy[i];
        if (nx >= 0 && nx < H && ny >= 0 && ny < W && s[nx][ny] == '.') {
          if (ok[nx][ny] == true)
            continue;
          if (ok[nx][ny] == false && ans[nx][ny] <= ans[x][y])
            continue;
          que[i].push({nx, ny});
          chmin(ans[nx][ny], ans[x][y]);
        }
      }
    }

    while (!que2[i].empty()) {
      cnt++;
      pll xy = que2[i].top();
      que2[i].pop();
      ll x = xy.fi, y = xy.sc;
      rep2(i, -2, 3) {
        rep2(j, -2, 3) {
          if (abs(i) <= 1 && abs(j) <= 1)
            continue;
          ll nx = x + i, ny = y + j;
          if (nx >= 0 && nx < H && ny >= 0 && ny < W && s[nx][ny] == '.') {
            if (ok[nx][ny] == true)
              continue;
            if (ok[nx][ny] == false && ans[nx][ny] <= ans[x][y] + 1)
              continue;
            que[i + 1].push({nx, ny});
            // ok[nx][ny] = true;
            chmin(ans[nx][ny], ans[x][y] + 1);
          }
        }
      }
    }
  }

  // rep(i,H){
  //     rep(j,W){
  //         cout<<ans[i][j]<<", ";
  //     }
  //     cout<<endl;
  // }
  // cout<<endl;

  // cout<<cnt<<endl;
  if (ans[tx][ty] == LLINF)
    cout << -1 << endl;
  else
    cout << ans[tx][ty] << endl;
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);

  ll t = 1;
  rep(i, t) { solve(); }

  return 0;
}