Language: C++


#include <iostream>
#include <algorithm>
#include <utility>
#include <numeric>
#include <vector>
#include <array>

#include <tuple>
#include <queue>

template <class T, size_t N, size_t I = 0>
decltype(auto) gen_vec(const size_t (&list)[N], typename std::enable_if<(I == N), const T&>::type value = T()) { 
  return value; 
}

template <class T, size_t N, size_t I = 0>
decltype(auto) gen_vec(const size_t (&list)[N], typename std::enable_if<(I != N), const T&>::type value = T()) { 
  return std::vector(list[I], gen_vec<T, N, I + 1>(list, value)); 
}

using i32 = int32_t;
using i64 = int64_t;
using u32 = uint32_t;
using u64 = uint64_t;

constexpr i32 inf32 = (i32(1) << 30) - 1;
constexpr i64 inf64 = (i64(1) << 62) - 1;

constexpr i32 dx[] = { 1, 0, -1, 0 };
constexpr i32 dy[] = { 0, 1, 0, -1 };

int main() {
  i32 H, W, K;
  std::cin >> H >> W >> K;
  i32 x1, y1, x2, y2;
  std::cin >> x1 >> y1 >> x2 >> y2;
  auto water = gen_vec<bool>({ H + 2, W + 2 });
  for (i32 i = 1; i <= H; ++i) {
    for (i32 j = 1; j <= W; ++j) {
      char c;
      std::cin >> c;
      water[i][j] = (c == '.');
    }
  }
  auto dist = gen_vec<std::pair<i32, i32>>({ H + 2, W + 2, 4 }, std::make_pair(inf32, inf32));
  using state = std::tuple<i32, i32, i32, i32, i32>;
  std::priority_queue<state, std::vector<state>, std::greater<state>> que;
  auto push = [&](i32 d, i32 r, i32 x, i32 y, i32 k) {
    if (!water[x][y]) {
      return;
    }
    if (dist[x][y][k] > std::make_pair(d, r)) {
      dist[x][y][k] = std::make_pair(d, r);
      que.emplace(d, r, x, y, k);
    }
  };
  for (i32 k = 0; k < 4; ++k) {
    push(0, 0, x1, y1, k);
  }
  while (!que.empty()) {
    auto [d, r, x, y, t] = que.top();
    show(x, y, t, d, r);
    que.pop();
    if (std::make_pair(d, r) > dist[x][y][t]) {
      continue;
    }
    if (r < 0) {
      push(d, r + 1, x + dx[t], y + dy[t], t);
    }
    for (i32 k = 0; k < 4; ++k) {
      push(d + 1, -(K - 1), x + dx[k], y + dy[k], k);
    }
  }
  std::pair<i32, i32> ans(inf32, inf32);
  for (i32 k = 0; k < 4; ++k) {
    if (ans > dist[x2][y2][k]) {
      ans = dist[x2][y2][k];
    }
  }
  if (ans.first == inf32) {
    std::cout << "-1\n";
  }
  else {
    std::cout << ans.first << '\n';
  }
  return 0;
}


Compiler output:
a.cc:24:13: error: 'uint32_t' does not name a type
   24 | using u32 = uint32_t;
      |             ^~~~~~~~
a.cc:11:1: note: 'uint32_t' is defined in header '<cstdint>'; this is probably fixable by adding '#include <cstdint>'
   10 | #include <queue>
  +++ |+#include <cstdint>
   11 | 
a.cc:25:13: error: 'uint64_t' does not name a type
   25 | using u64 = uint64_t;
      |             ^~~~~~~~
a.cc:25:13: note: 'uint64_t' is defined in header '<cstdint>'; this is probably fixable by adding '#include <cstdint>'
a.cc: In function 'int main()':
a.cc:38:34: warning: narrowing conversion of '(H + 2)' from 'i32' {aka 'int'} to 'size_t' {aka 'long unsigned int'} [-Wnarrowing]
   38 |   auto water = gen_vec<bool>({ H + 2, W + 2 });
      |                                ~~^~~
a.cc:38:41: warning: narrowing conversion of '(W + 2)' from 'i32' {aka 'int'} to 'size_t' {aka 'long unsigned int'} [-Wnarrowing]
   38 |   auto water = gen_vec<bool>({ H + 2, W + 2 });
      |                                       ~~^~~
a.cc:46:48: warning: narrowing conversion of '(H + 2)' from 'i32' {aka 'int'} to 'size_t' {aka 'long unsigned int'} [-Wnarrowing]
   46 |   auto dist = gen_vec<std::pair<i32, i32>>({ H + 2, W + 2, 4 }, std::make_pair(inf32, inf32));
      |                                              ~~^~~
a.cc:46:55: warning: narrowing conversion of '(W + 2)' from 'i32' {aka 'int'} to 'size_t' {aka 'long unsigned int'} [-Wnarrowing]
   46 |   auto dist = gen_vec<std::pair<i32, i32>>({ H + 2, W + 2, 4 }, std::make_pair(inf32, inf32));
      |                                                     ~~^~~
a.cc:63:5: error: 'show' was not declared in this scope
   63 |     show(x, y, t, d, r);
      |     ^~~~