Problem p02644 (C++)

Buggy submission (status: Time Limit Exceeded):
// header{{{
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
// #include<boost/multiprecision/cpp_int.hpp>
// #include<boost/math/common_factor_rt.hpp>
// #include<boost/multiprecision/cpp_dec_float.hpp>

using namespace std;
// using namespace boost;
// using namespace multiprecision;

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define reps(i, n) for (int i = 1; i <= (n); ++i)
#define all(x) (x).begin(), (x).end()
#define Fixed fixed << setprecision(12)
#define int int64_t
using pii = pair<int, int>;
constexpr int INF = 0x3f3f3f3f;
constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr int mod1 = 1e9 + 7;
constexpr int mod2 = 998244353;

template <class A, class B> inline bool chmax(A &a, const B &b) {
  return b > a && (a = b, true);
}
template <class A, class B> inline bool chmin(A &a, const B &b) {
  return b < a && (a = b, true);
}

template <class T> using min_heap = priority_queue<T, vector<T>, greater<T>>;
template <class T> using max_heap = priority_queue<T>;
template <class A, class B> using umap = unordered_map<A, B>;

inline int square(int a) { return a * a; }
inline int updiv(int a, int b) { return (a + b - 1) / b; }

template <class A, class B> inline int Max(const A &a, const B &b) {
  return (a < b ? b : a);
}
template <class A, class B> inline int Min(const A &a, const B &b) {
  return (a > b ? b : a);
}

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

signed main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);

  int h, w, k, sy, sx, gy, gx;
  cin >> h >> w >> k >> sy >> sx >> gy >> gx;

  --sy;
  --sx;
  --gy;
  --gx;

  vector<string> mat(h);

  rep(i, h) { cin >> mat[i]; }

  auto inside = [&](int y, int x) {
    return (0 <= y && 0 <= x && y < h && x < w);
  };

  vector<vector<int>> min_cost(h, vector<int>(w, LINF));
  // queue<pair<int, int> > que;
  min_heap<tuple<int, int, int>> pq;
  min_cost[sy][sx] = 0;
  // que.emplace(sy, sx);
  pq.emplace(0, sy, sx);

  while (!pq.empty()) {
    // auto [y, x] = que.front();
    auto [v, y, x] = pq.top();
    // que.pop();
    pq.pop();
    if (min_cost[y][x] < v)
      continue;
    rep(i, 4) {
      int ny = y, nx = x;
      rep(j, k) {
        ny += dy[i];
        nx += dx[i];
        if (inside(ny, nx) && mat[ny][nx] != '@') {
          if (chmin(min_cost[ny][nx], min_cost[y][x] + 1)) {
            pq.emplace(min_cost[ny][nx], ny, nx);
          }
        } else {
          break;
        }
      }
    }
  }

  if (min_cost[gy][gx] == LINF) {
    cout << -1 << '\n';
  } else {
    cout << min_cost[gy][gx] << '\n';
  }

  return (0);
}


Fixed submission:
// header{{{
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
// #include<boost/multiprecision/cpp_int.hpp>
// #include<boost/math/common_factor_rt.hpp>
// #include<boost/multiprecision/cpp_dec_float.hpp>

using namespace std;
// using namespace boost;
// using namespace multiprecision;

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define reps(i, n) for (int i = 1; i <= (n); ++i)
#define all(x) (x).begin(), (x).end()
#define Fixed fixed << setprecision(12)
#define int int64_t
using pii = pair<int, int>;
constexpr int INF = 0x3f3f3f3f;
constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr int mod1 = 1e9 + 7;
constexpr int mod2 = 998244353;

template <class A, class B> inline bool chmax(A &a, const B &b) {
  return b > a && (a = b, true);
}
template <class A, class B> inline bool chmin(A &a, const B &b) {
  return b < a && (a = b, true);
}

template <class T> using min_heap = priority_queue<T, vector<T>, greater<T>>;
template <class T> using max_heap = priority_queue<T>;
template <class A, class B> using umap = unordered_map<A, B>;

inline int square(int a) { return a * a; }
inline int updiv(int a, int b) { return (a + b - 1) / b; }

template <class A, class B> inline int Max(const A &a, const B &b) {
  return (a < b ? b : a);
}
template <class A, class B> inline int Min(const A &a, const B &b) {
  return (a > b ? b : a);
}

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

signed main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);

  int h, w, k, sy, sx, gy, gx;
  cin >> h >> w >> k >> sy >> sx >> gy >> gx;

  --sy;
  --sx;
  --gy;
  --gx;

  vector<string> mat(h);

  rep(i, h) { cin >> mat[i]; }

  auto inside = [&](int y, int x) {
    return (0 <= y && 0 <= x && y < h && x < w);
  };

  vector<vector<int>> min_cost(h, vector<int>(w, LINF));
  // queue<pair<int, int> > que;
  min_heap<tuple<int, int, int>> pq;
  min_cost[sy][sx] = 0;
  // que.emplace(sy, sx);
  pq.emplace(0, sy, sx);

  while (!pq.empty()) {
    // auto [y, x] = que.front();
    auto [v, y, x] = pq.top();
    // que.pop();
    pq.pop();
    if (min_cost[y][x] < v)
      continue;
    rep(i, 4) {
      int ny = y, nx = x;
      rep(j, k) {
        ny += dy[i];
        nx += dx[i];
        if (inside(ny, nx) && mat[ny][nx] != '@') {
          if (chmin(min_cost[ny][nx], min_cost[y][x] + 1)) {
            pq.emplace(min_cost[ny][nx], ny, nx);
          } else if (min_cost[ny][nx] != min_cost[y][x] + 1) {
            break;
          }
        } else {
          break;
        }
      }
    }
  }

  if (min_cost[gy][gx] == LINF) {
    cout << -1 << '\n';
  } else {
    cout << min_cost[gy][gx] << '\n';
  }

  return (0);
}