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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repr(i, n) for (int i = (n); i >= 0; --i)
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define FORR(i, m, n) for (int i = (m); i >= (n); --i)
#define equals(a, b) (fabs((a) - (b)) < EPS)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const ll mod = 1000000007;
// const ll mod = 998244353;
const int inf = 1e9 + 10;
const ll INF = 1e18;
const ld EPS = 1e-10;
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
typedef tuple<ll, ll, ll, ll> tp;

ll h, w, k;
ll sy, sx, gy, gx;
vector<vector<char>> c;

vector<vector<ll>> d[4];

void dijkstra() {
  priority_queue<tp, vector<tp>, greater<tp>> pq;
  rep(i, 4) {
    pq.push(make_tuple(0, i, sy, sx));
    d[i][sy][sx] = 0;
  }

  while (!pq.empty()) {
    tp res = pq.top();
    pq.pop();
    ll resd = get<0>(res), rdir = get<1>(res), ry = get<2>(res),
       rx = get<3>(res);
    if (resd > d[rdir][ry][rx])
      continue;
    // cout << resd << ' ' << rdir << ' ' << ry << ' ' << rx << '\\n';
    ll ny = ry + dy[rdir], nx = rx + dx[rdir];
    if (ny < 0 || ny >= h || nx < 0 || nx >= w)
      continue;
    if (c[ny][nx] == '@')
      continue;
    rep(ndir, 4) {
      ll nxtd = resd + 1;
      if (rdir != ndir && nxtd % k)
        nxtd += (k - nxtd % k);
      if (chmin(d[ndir][ny][nx], nxtd)) {
        pq.push(make_tuple(nxtd, ndir, ny, nx));
      }
    }
  }
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout << fixed << setprecision(25);

  cin >> h >> w >> k >> sy >> sx >> gy >> gx;
  sy--, sx--, gy--, gx--;
  c.resize(h, vector<char>(w));
  rep(i, h) {
    rep(j, w) { cin >> c[i][j]; }
  }

  rep(i, 4) d[i].resize(h, vector<ll>(w, INF));
  dijkstra();
  ll ans = INF;
  rep(i, 4) chmin(ans, d[i][gy][gx]);
  if (ans == INF) {
    cout << -1 << '\\n';
    return 0;
  }
  ans = (ans + k - 1) / k;
  cout << ans << '\\n';
  /*
  rep(i, 4) {
    rep(j, h) {
      rep(k, w) {
        cout << (d[i][j][k] == INF ? -1 : d[i][j][k]) << ' ';
      }
      cout << '\\n';
    }
  }
  */

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repr(i, n) for (int i = (n); i >= 0; --i)
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define FORR(i, m, n) for (int i = (m); i >= (n); --i)
#define equals(a, b) (fabs((a) - (b)) < EPS)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const ll mod = 1000000007;
// const ll mod = 998244353;
const int inf = 1e9 + 10;
const ll INF = 1e18;
const ld EPS = 1e-10;
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int h, w, k;
int sy, sx, gy, gx;
vector<vector<char>> c;

vector<vector<int>> d;

void bfs() {
  d[sy][sx] = 0;
  queue<pair<int, int>> q;
  q.push({sy, sx});
  while (!q.empty()) {
    int y = q.front().first, x = q.front().second;
    q.pop();
    rep(dir, 4) {
      int ry = y + dy[dir], rx = x + dx[dir];
      if (ry < 0 || ry >= h || rx < 0 || rx >= w || c[y][x] == '@')
        continue;
      bool ok = false;
      int pdir = (dir + 2) % 4;
      int py = y + dy[pdir], px = x + dx[pdir];
      if (py >= 0 && py < h && px >= 0 && px < w) {
        if (d[y][x] - d[py][px] == 1 && k > 1)
          ok = true;
        if (d[y][x] == d[py][px]) {
          int ppy = y + dy[pdir] * (k - 1), ppx = x + dx[pdir] * (k - 1);
          if (ppy < 0 || ppy >= h || ppx < 0 || ppx >= w)
            ok = true;
          if (d[y][x] != d[ppy][ppx])
            ok = true;
        }
      }
      int dist = d[y][x];
      if (!ok)
        dist++;
      if (chmin(d[ry][rx], dist))
        q.push({ry, rx});
    }
  }
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout << fixed << setprecision(25);

  cin >> h >> w >> k >> sy >> sx >> gy >> gx;
  sy--, sx--, gy--, gx--;
  c.resize(h, vector<char>(w));
  rep(i, h) {
    rep(j, w) { cin >> c[i][j]; }
  }

  d.resize(h, vector<int>(w, inf));
  bfs();
  cout << (d[gy][gx] == inf ? -1 : d[gy][gx]) << '\\n';

  return 0;
}