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;
typedef long long ll;
typedef pair<ll, ll> P;

const ll INF = 1e18;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};

int main() {
  ll h, w, k;
  cin >> h >> w >> k;
  ll x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  x2--;
  y1--;
  y2--;
  vector<string> g(h);
  rep(i, h) cin >> g[i];

  vector<ll> dist(h * w * 4, INF);
  ll start = ((x1 * w) + y1) * 4;
  ll goal = ((x2 * w) + y2) * 4;
  //  cout<<start<<"s"<<goal<<"g"<<endl;
  priority_queue<P, vector<P>, greater<P>> q;
  rep(i, 4) {
    ll id = start + i;
    q.push(make_pair(0, id));
    dist[id] = 0;
  }

  while (!q.empty()) {
    auto Q = q.top();
    q.pop();
    ll d = Q.first;
    ll id = Q.second;
    //  cout<<id<<"id"<<d<<"d"<<dist[id]<<"dist"<<endl;
    if (dist[id] != d)
      continue;
    //  dist[id]=d;
    ll vnow = id % 4;
    ll jnow = (id - vnow) / 4 % w;
    ll inow = ((id - vnow) / 4 - jnow) / w;
    ll D = ((d + k - 1) / k) * k;
    // cout<<D<<"D"<<endl;

    rep(i, 4) {
      ll idnow = (inow * w + jnow) * 4 + i;
      if (dist[idnow] > D) {
        q.push(make_pair(D, idnow));
        //  cout<<D<<"D"<<idnow<<"ID"<<endl;
        dist[idnow] = D;
      }

      ll ni = inow + dx[i];
      ll nj = jnow + dy[i];
      if (i != vnow)
        continue;
      if (ni < 0 or ni >= h or nj < 0 or nj >= w)
        continue;

      if (g[ni][nj] == '@')
        continue;
      if (dist[(ni * w + nj) * 4 + i] <= (d + 1))
        continue;
      q.push(make_pair(d + 1, (ni * w + nj) * 4 + i));
      //    cout<<((ni*w+nj)*4+i)<<"nid"<<d+1<<"nd"<<endl;

      dist[(ni * w + nj) * 4 + i] = d + 1;
    }
  }

  ll ans = INF;
  rep(i, 4) { ans = min(ans, dist[goal + i]); }
  // cout<<ans;
  if (ans == INF)
    cout << -1;
  else {
    //  cout<<ans<<endl;
    cout << (ans + k - 1) / k;
  }
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;

const ll INF = 1e18;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};

int main() {
  ll h, w, k;
  cin >> h >> w >> k;
  ll x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  x2--;
  y1--;
  y2--;
  vector<string> g(h);
  rep(i, h) cin >> g[i];

  vector<ll> dist(h * w * 4, INF);
  ll start = ((x1 * w) + y1) * 4;
  ll goal = ((x2 * w) + y2) * 4;
  //  cout<<start<<"s"<<goal<<"g"<<endl;
  priority_queue<P, vector<P>, greater<P>> q;
  rep(i, 4) {
    ll id = start + i;
    q.push(make_pair(0, id));
    dist[id] = 0;
  }

  while (!q.empty()) {
    auto Q = q.top();
    q.pop();
    ll d = Q.first;
    ll id = Q.second;
    //  cout<<id<<"id"<<d<<"d"<<dist[id]<<"dist"<<endl;
    if (dist[id] != d)
      continue;
    //  dist[id]=d;
    ll vnow = id % 4;
    ll jnow = (id - vnow) / 4 % w;
    ll inow = ((id - vnow) / 4 - jnow) / w;
    ll D = ((d + k - 1) / k) * k;
    // cout<<D<<"D"<<endl;

    rep(i, 4) {
      ll idnow = (inow * w + jnow) * 4 + i;
      if (dist[idnow] > D) {
        q.push(make_pair(D, idnow));
        //  cout<<D<<"D"<<idnow<<"ID"<<endl;
        dist[idnow] = D;
      }

      ll ni = inow + dx[i];
      ll nj = jnow + dy[i];
      if (i != vnow)
        continue;
      if (ni < 0 or ni >= h or nj < 0 or nj > w)
        continue;

      if (g[ni][nj] == '@')
        continue;
      if (dist[(ni * w + nj) * 4 + i] <= (d + 1))
        continue;
      q.push(make_pair(d + 1, (ni * w + nj) * 4 + i));
      //    cout<<((ni*w+nj)*4+i)<<"nid"<<d+1<<"nd"<<endl;

      dist[(ni * w + nj) * 4 + i] = d + 1;
    }
  }

  ll ans = INF;
  rep(i, 4) { ans = min(ans, dist[goal + i]); }
  // cout<<ans;
  if (ans == INF)
    cout << -1;
  else {
    //  cout<<ans<<endl;
    cout << (ans + k - 1) / k;
  }
}