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

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#include <string>
#include <vector>

using namespace std;
using ll = long long int;

#define rep(i, n) for (ll i = 0; i < (n); i++)

#define INF 1000000000

struct entry {
  ll cost;
  ll x;
  ll y;
  ll dir;

  bool operator<(const entry &a) const { return this->cost < a.cost; }

  bool operator>(const entry &a) const { return this->cost > a.cost; }
};

ll H, W, K;
vector<string> C;
vector<vector<ll>> d[4];

void search(ll x0, ll y0) {
  ll dirs[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
  priority_queue<entry, std::vector<entry>, greater<entry>> queue;
  queue.push({0, x0, y0, 0});

  while (!queue.empty()) {
    entry e = queue.top();
    queue.pop();

    if (e.cost >= d[e.dir][e.x][e.y]) {
      continue;
    }
    d[e.dir][e.x][e.y] = e.cost;

    // go straight
    ll x = e.x + dirs[e.dir][0];
    ll y = e.y + dirs[e.dir][1];

    if (0 <= x && x < H && 0 <= y && y < W && C[x][y] != '@') {
      //      cout << e.x << "," << e.y << "," << e.dir << endl;
      queue.push({e.cost + 1, x, y, e.dir});
    }

    // rotate
    queue.push({((e.cost + K - 1) / K) * K, e.x, e.y, (e.dir + 1) % 4});
  }
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  cin >> H >> W >> K;

  ll x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  y1--;
  x2--;
  y2--;

  rep(j, H) {
    string s;
    cin >> s;
    C.emplace_back(s);
  }

  rep(j, H) {
    vector<ll> v(W, INF);
    rep(r, 4) { d[r].emplace_back(v); }
  }

  search(x1, y1);
  //  cout << "search complete " << endl;
  //  cout << x2 << "," << y2 << endl;
  if (d[0][x2][y2] == INF) {
    cout << -1 << endl;
  } else {
    cout << (d[0][x2][y2] + K - 1) / K << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#include <string>
#include <vector>

using namespace std;
using ll = long long int;

#define rep(i, n) for (ll i = 0; i < (n); i++)

#define INF 1000000000

struct entry {
  ll cost;
  ll x;
  ll y;

  bool operator<(const entry &a) const { return this->cost < a.cost; }

  bool operator>(const entry &a) const { return this->cost > a.cost; }
};

ll H, W, K;
vector<string> C;
vector<vector<ll>> d;

void search(ll x0, ll y0) {
  priority_queue<entry, std::vector<entry>, greater<entry>> queue;
  queue.push({0, x0, y0});
  d[y0][x0] = 0;

  while (!queue.empty()) {
    entry e = queue.top();
    queue.pop();
    //    if (e.cost >= d[e.y][e.x]) { continue; }

    ll dirs[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
    rep(v, 4) {
      ll x = e.x + dirs[v][0];
      ll y = e.y + dirs[v][1];

      for (ll s = 1; 0 <= x && x < H && 0 <= y && y < W && s <= K; s++) {
        //        cout << x << ", " << y << d[x][y] << endl;
        if (d[x][y] <= e.cost) {
          break;
        }

        if (C[x][y] == '@') {
          break;
        }

        d[x][y] = e.cost + 1;
        queue.push({e.cost + 1, x, y});

        x += dirs[v][0];
        y += dirs[v][1];
      }
    }
  }
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  cin >> H >> W >> K;

  ll x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  y1--;
  x2--;
  y2--;

  rep(j, H) {
    string s;
    cin >> s;
    C.emplace_back(s);
  }

  rep(j, H) {
    vector<ll> v(W, INF);
    d.emplace_back(v);
  }

  search(x1, y1);
  //  cout << "search complete " << endl;
  //  cout << x2 << "," << y2 << endl;
  if (d[x2][y2] == INF) {
    cout << -1 << endl;
  } else {
    cout << d[x2][y2] << endl;
  }
  return 0;
}