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

#include <iostream>
#include <queue>
#include <vector>

using ll = long long;
using P = std::pair<int, int>;
using LP = std::pair<ll, int>;

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

int main() {
  int h, w, k;
  int si, sj, gi, gj;
  std::cin >> h >> w >> k;
  std::cin >> si >> sj >> gi >> gj;
  --si;
  --sj;
  --gi;
  --gj;
  std::vector<std::string> g(h);
  for (int i = 0; i < h; ++i)
    std::cin >> g[i];

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

  auto modCeilK = [&](ll x) { return (x + k - 1) / k * k; };
  std::vector<ll> dist(h * w * 4, INF);

  std::priority_queue<LP, std::vector<LP>, std::greater<LP>> q;
  auto push = [&](int i, int j, int v, ll x) {
    int id = toId(i, j, v);
    if (dist[id] <= x)
      return;
    dist[id] = x;
    q.emplace(x, id);
  };

  for (int v = 0; v < 4; ++v)
    push(si, sj, v, 0);

  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;

    for (int nv = 0; nv < 4; ++nv)
      push(i, j, nv, modCeilK(d));
    {
      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;
  for (int v = 0; v < 4; ++v)
    ans = std::min(ans, dist[toId(gi, gj, v)]);
  if (ans == INF) {
    std::cout << -1 << std::endl;
  } else {
    ans = modCeilK(ans) / k;
    std::cout << ans << std::endl;
  }

  return 0;
}


Error: 0

Fixed submission:

#include <iostream>
#include <queue>

#define INF -1
#define MAX_H 1000010
#define MAX_W 1000010

std::vector<std::vector<char>> maze;
std::vector<std::vector<int>> dist;
int h, w, k;
int sx, sy, gx, gy;
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};

int main() {
  std::queue<std::pair<int, int>> que;
  std::cin >> h >> w >> k >> sx >> sy >> gx >> gy;
  --sx;
  --sy;
  --gx;
  --gy;
  maze = std::vector<std::vector<char>>(h);
  dist = std::vector<std::vector<int>>(w);
  for (int i = 0; i < h; ++i) {
    maze[i] = std::vector<char>(w);
    dist[i] = std::vector<int>(w, INF);
  }
  for (int hh = 0; hh < h; ++hh) {
    for (int ww = 0; ww < w; ++ww)
      std::cin >> maze[hh][ww];
  }
  que.push(std::make_pair(sx, sy));
  dist[sx][sy] = 0;

  while (!que.empty()) {
    std::pair<int, int> p = que.front();
    que.pop();
    int x = p.first, y = p.second;

    if (x == gx && y == gy)
      break;

    for (int i = 0; i < 4; ++i) {
      int xx = x + dx[i];
      int yy = y + dy[i];
      int kk = 1;

      while (kk <= k) {
        if (xx < 0 || h <= xx || yy < 0 || w <= yy)
          break;
        if (maze[xx][yy] == '@')
          break;

        if (maze[xx][yy] != '@' && dist[xx][yy] == INF) {
          dist[xx][yy] = dist[x][y] + 1;
          que.push(std::make_pair(xx, yy));
          // std::cout << "pushed:(" << xx << ", " << yy << ")\\n";
        }
        xx += dx[i];
        yy += dy[i];
        ++kk;
      }
    }
  }

  //     for (int hh=0; hh<h; ++hh) {
  //         for (int ww=0; ww<w; ++ww) {
  //             std::cout << dist[hh][ww] << '\\t';
  //         }
  //         std::cout << '\\n';
  //     }

  std::cout << dist[gx][gy] << std::endl;
  return 0;
}