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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>; // dist pairs d[i][j]
using LP = pair<ll, int>; // vertex number pairs

const ll INF = 1e18;
const int di[] = {-1, 0, 1, 0};
const int dj[] = {0, -1, 0, 1};

int main() {
  int h, w, k;
  cin >> h >> w >> k;
  int si, sj, ti, tj;
  cin >> si >> sj >> ti >> tj;
  --si;
  --sj;
  --ti;
  --tj;
  vector<string> g(h);
  rep(i, h) cin >> g[i];

  auto toId = [&](int i, int j, int v) { return (i * w + j) * 4 + v; };

  auto modCeilK = [&](ll x) { // Ceil(x) and return x multiplied by k.
    return (x + k - 1) / k * k;
  };

  vector<ll> dist(h * w * 4, INF);
  priority_queue<LP, vector<LP>, greater<LP>> q; // Heap in ascending order.

  auto push = [&](int i, int j, int v,
                  ll x) { // Judge whether we can reach to x.
    int id = toId(i, j, v);
    if (dist[id] <= x)
      return;
    dist[id] = x;
    q.emplace(x, id);
  };

  rep(v, 4) push(si, sj, v, 0); // Initialize by assigning 0 to all (si, sj, v).
  while (!q.empty()) {
    ll d = q.top().first;
    int id = q.top().second;
    q.pop();
    if (dist[id] != d)
      continue;
    int i = id / w / 4;
    int j = id / 4 % w;
    int v = id % 4;
    // Chnage directions.
    rep(nv, 4) push(i, j, nv, modCeilK(d));
    // Go until change directions.
    {
      int ni = i + di[v], nj = j + dj[v];
      if (0 <= ni && ni < h && 0 <= nj && nj < w) {
        if (g[ni][nj] == '.') {
          push(ni, nj, v, d + 1);
        }
      }
    }
  }

  ll ans = INF;
  rep(v, 4) ans = min(ans, dist[toId(ti, tj, v)]);
  if (ans == INF) {
    cout << -1 << endl;
  } else {
    ans = modCeilK(ans) / k;
    cout << ans << endl;
  }
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#include <fstream>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>; // dist pairs d[i][j]
using LP = pair<ll, int>; // vertex number pairs

const ll INF = 1e18;
const int di[] = {-1, 0, 1, 0};
const int dj[] = {0, -1, 0, 1};

int main() {
  string file;
  cin >> file;
  cout << file << endl;
  // ifstream in(file);
  // cin.rdbuf(in.rdbuf());
  int h, w, k;
  cin >> h >> w >> k;
  cout << h << ' ' << w << ' ' << k << endl;
  int si, sj, ti, tj;
  cin >> si >> sj >> ti >> tj;
  --si;
  --sj;
  --ti;
  --tj;
  vector<string> g(h);
  rep(i, h) cin >> g[i];

  auto toId = [&](int i, int j, int v) { return (i * w + j) * 4 + v; };

  auto modCeilK = [&](ll x) { // Ceil(x) and return x multiplied by k.
    return (x + k - 1) / k * k;
  };

  vector<ll> dist(h * w * 4, INF);
  priority_queue<LP, vector<LP>, greater<LP>> q; // Heap in ascending order.

  auto push = [&](int i, int j, int v,
                  ll x) { // Judge whether we can reach to x.
    int id = toId(i, j, v);
    if (dist[id] <= x)
      return;
    dist[id] = x;
    q.emplace(x, id);
  };

  rep(v, 4) push(si, sj, v, 0); // Initialize by assigning 0 to all (si, sj, v).
  while (!q.empty()) {
    ll d = q.top().first;
    int id = q.top().second;
    q.pop();
    if (dist[id] != d)
      continue;
    int i = id / w / 4;
    int j = id / 4 % w;
    int v = id % 4;
    // Chnage directions.
    rep(nv, 4) push(i, j, nv, modCeilK(d));
    // Go until change directions.
    {
      int ni = i + di[v], nj = j + dj[v];
      if (0 <= ni && ni < h && 0 <= nj && nj < w) {
        if (g[ni][nj] == '.') {
          push(ni, nj, v, d + 1);
        }
      }
    }
  }

  ll ans = INF;
  rep(v, 4) ans = min(ans, dist[toId(ti, tj, v)]);
  if (ans == INF) {
    cout << -1 << endl;
  } else {
    ans = modCeilK(ans) / k;
    cout << ans << endl;
  }
  return 0;
}