Problem p02644 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdint>
#include <cstdlib>
#include <iostream>
#include <queue>
#include <tuple>
#include <vector>
template <typename T> void fin(T const &t) {
  std::cout << t << std::endl;
  exit(0);
}

template <typename T>
std::vector<std::vector<T>> mk2d(size_t s1, size_t s2, T const &val) {
  std::vector<T> tmp(s2, val);
  return std::vector<std::vector<T>>(s1, tmp);
}

template <typename T> std::vector<T> mk1d(size_t s, T const &val) {
  return std::vector<T>(s, val);
}

int const dx[4] = {1, 0, -1, 0};
int const dy[4] = {0, 1, 0, -1};
int64_t const INF = 1e18;
int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  int H, W, K;
  std::cin >> H >> W >> K;
  int xs, ys, xg, yg;
  std::cin >> xs >> ys >> xg >> yg;
  auto bm = mk2d(H + 2, W + 2, '@');
  for (int h = 1; h <= H; ++h)
    for (int w = 1; w <= W; ++w)
      std::cin >> bm[h][w];
  auto dtmp = mk2d(H + 2, W + 2, INF);
  auto dist = mk1d(4, dtmp);
  using TP = std::tuple<int64_t, int, int, int>;
  std::priority_queue<TP, std::vector<TP>, std::greater<TP>> q;
  q.emplace(0, xs, ys, 0);
  q.emplace(1, xs, ys, 0);
  q.emplace(2, xs, ys, 0);
  q.emplace(3, xs, ys, 0);
  while (!q.empty()) {
    int d, x, y;
    int64_t c;
    std::tie(c, d, x, y) = q.top();
    q.pop();
    if (c >= dist[d][x][y])
      continue;
    dist[d][x][y] = c;
    for (int j = 1; j <= 3; ++j) {
      int64_t nc = ((c + K - 1) / K) * K;
      q.emplace(nc, (d + j) % 4, x, y);
    }
    int nx = x + dx[d];
    int ny = y + dy[d];
    if (bm[nx][ny] == '.')
      q.emplace(c + 1, d, nx, ny);
  }
  int64_t ans = dist[0][xg][yg];
  if (ans == INF)
    ans = -1;
  else
    ans = (ans + K - 1) / K;
  fin(ans);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdint>
#include <cstdlib>
#include <iostream>
#include <queue>
#include <tuple>
#include <vector>
template <typename T> void fin(T const &t) {
  std::cout << t << std::endl;
  exit(0);
}

template <typename T>
std::vector<std::vector<T>> mk2d(size_t s1, size_t s2, T const &val) {
  std::vector<T> tmp(s2, val);
  return std::vector<std::vector<T>>(s1, tmp);
}

template <typename T> std::vector<T> mk1d(size_t s, T const &val) {
  return std::vector<T>(s, val);
}

int const dx[4] = {1, 0, -1, 0};
int const dy[4] = {0, 1, 0, -1};
int64_t const INF = 1e18;
int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  int H, W, K;
  std::cin >> H >> W >> K;
  int xs, ys, xg, yg;
  std::cin >> xs >> ys >> xg >> yg;
  auto bm = mk2d(H + 2, W + 2, '@');
  for (int h = 1; h <= H; ++h)
    for (int w = 1; w <= W; ++w)
      std::cin >> bm[h][w];
  auto dtmp = mk2d(H + 2, W + 2, INF);
  auto dist = mk1d(4, dtmp);
  using TP = std::tuple<int64_t, int, int, int>;
  std::priority_queue<TP, std::vector<TP>, std::greater<TP>> q;
  q.emplace(0, 0, xs, ys);
  q.emplace(0, 1, xs, ys);
  q.emplace(0, 2, xs, ys);
  q.emplace(0, 3, xs, ys);
  while (!q.empty()) {
    int d, x, y;
    int64_t c;
    std::tie(c, d, x, y) = q.top();
    q.pop();
    if (c >= dist[d][x][y])
      continue;
    dist[d][x][y] = c;
    for (int j = 1; j <= 3; ++j) {
      int64_t nc = ((c + K - 1) / K) * K;
      q.emplace(nc, (d + j) % 4, x, y);
    }
    int nx = x + dx[d];
    int ny = y + dy[d];
    if (bm[nx][ny] == '.')
      q.emplace(c + 1, d, nx, ny);
  }
  int64_t ans = dist[0][xg][yg];
  if (ans == INF)
    ans = -1;
  else
    ans = (ans + K - 1) / K;
  fin(ans);
  return 0;
}