Problem p02644 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "../../dump.hpp"
#else
#define dump(...)
#endif

#define int ll
#define rep(i, n) for (int i = 0, i##_cond = (n); i < i##_cond; ++i)
#define FOR(i, a, b) for (int i = (a), i##_cond = (b); i < i##_cond; ++i)
#define ROF(i, a, b)                                                           \
  for (int i = (a)-1, i##_cond = (b); i >= i##_cond; --i) // ROF(i,n,0)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend() // sortで降順
#define SUM(a) accumulate(all(a), int(0))
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vs = vector<string>;
using pii = pair<int, int>;
constexpr ll inf = 1ll << 61;
constexpr ll mod = 1e9 + 7;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> T add(const T &a, const T &b) { return (a + b) % mod; }

vi dx = {0, 1, 0, -1}, dy = {1, 0, -1, 0};
int h, w;

bool isin(int x, int y) { return 0 <= x and x < h and 0 <= y and y < w; }

signed main() {
  int k;
  cin >> h >> w >> k;
  int sx, sy, gx, gy;
  cin >> sx >> sy >> gx >> gy;
  sx--;
  sy--;
  gx--;
  gy--;
  vs c(h);
  rep(i, h) cin >> c[i];

  vvi d(h, vi(w, inf));
  d[sx][sy] = 0;
  queue<pii> q;
  q.push(pii(sx, sy));
  while (not q.empty()) {
    auto now = q.front();
    q.pop();

    rep(i, 4) {
      FOR(j, 1, k + 1) {
        int nx = now.first + j * dx[i], ny = now.second + j * dy[i];
        if (not isin(nx, ny) or c[nx][ny] == '@')
          break;
        if (d[nx][ny] == inf) {
          d[nx][ny] = d[now.first][now.second] + 1;
          q.push(pii(nx, ny));
        }
      }
    }
  }
  if (d[gx][gy] == inf)
    cout << -1 << endl;
  else
    cout << d[gx][gy] << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "../../dump.hpp"
#else
#define dump(...)
#endif

#define int ll
#define rep(i, n) for (int i = 0, i##_cond = (n); i < i##_cond; ++i)
#define FOR(i, a, b) for (int i = (a), i##_cond = (b); i < i##_cond; ++i)
#define ROF(i, a, b)                                                           \
  for (int i = (a)-1, i##_cond = (b); i >= i##_cond; --i) // ROF(i,n,0)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend() // sortで降順
#define SUM(a) accumulate(all(a), int(0))
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vs = vector<string>;
using pii = pair<int, int>;
constexpr ll inf = 1ll << 61;
constexpr ll mod = 1e9 + 7;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> T add(const T &a, const T &b) { return (a + b) % mod; }

vi dx = {0, 1, 0, -1}, dy = {1, 0, -1, 0};
int h, w;

bool isin(int x, int y) { return 0 <= x and x < h and 0 <= y and y < w; }

signed main() {
  int k;
  cin >> h >> w >> k;
  int sx, sy, gx, gy;
  cin >> sx >> sy >> gx >> gy;
  sx--;
  sy--;
  gx--;
  gy--;
  vs c(h);
  rep(i, h) cin >> c[i];

  vvi d(h, vi(w, inf));
  d[sx][sy] = 0;
  queue<pii> q;
  q.push(pii(sx, sy));
  while (not q.empty()) {
    auto now = q.front();
    q.pop();

    rep(i, 4) {
      FOR(j, 1, k + 1) {
        int nx = now.first + j * dx[i], ny = now.second + j * dy[i];
        if (not isin(nx, ny) or c[nx][ny] == '@' or
            d[nx][ny] <= d[now.first][now.second])
          break;
        if (d[nx][ny] == inf) {
          d[nx][ny] = d[now.first][now.second] + 1;
          q.push(pii(nx, ny));
        }
      }
    }
  }
  if (d[gx][gy] == inf)
    cout << -1 << endl;
  else
    cout << d[gx][gy] << endl;
}