Problem p02644 (C++) — buggy submission, verdict: Time Limit Exceeded

/*
ここの解説を元にACした: https://betrue12.hateblo.jp/entry/2020/06/14/232948
*/
#include <iostream>
#include <queue>
#include <string>
#include <tuple>
#include <vector>
#define rep(i, start, end) for (int i = (int)start; i < (int)end; ++i)
#define rrep(i, start, end) for (int i = (int)start - 1; i >= (int)end; --i)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return 0;
}

using state = tuple<int, int, int>;

const int INF = 1 << 30;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int H, W, K;
  cin >> H >> W >> K;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  --x1, --y1, --x2, --y2;
  vector<string> grid(H);
  for (auto &g : grid) {
    cin >> g;
  }
  priority_queue<state, vector<state>, greater<state>> que;
  vector<vector<int>> dp(H, vector<int>(W, INF));
  vector<int> dh = {-1, 0, 1, 0};
  vector<int> dw = {0, -1, 0, 1};
  dp[x1][y1] = 0;
  que.emplace(0, x1, y1);
  while (!que.empty()) {
    state now = que.top();
    que.pop();
    int dist = get<0>(now);
    int h = get<1>(now);
    int w = get<2>(now);
    if (dist > dp[h][w]) {
      continue;
    }
    rep(i, 0, 4) {
      int nh = h;
      int nw = w;
      rep(_, 0, K) {
        nh += dh[i];
        nw += dw[i];
        if (nh < 0 || nh >= H || nw < 0 || nw >= W || grid[nh][nw] == '@') {
          break;
        }
        if (dp[nh][nw] <= dist) {
          break;
        }
        if (chmin(dp[nh][nw], dist + 1)) {
          que.emplace(dp[nh][nw], nh, nw);
        }
      }
    }
  }
  if (dp[x2][y2] == INF) {
    cout << -1 << endl;
  } else {
    cout << dp[x2][y2] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

/*
ここの解説を元にACした: https://betrue12.hateblo.jp/entry/2020/06/14/232948
*/
#include <iostream>
#include <queue>
#include <string>
#include <tuple>
#include <vector>
#define rep(i, start, end) for (int i = (int)start; i < (int)end; ++i)
#define rrep(i, start, end) for (int i = (int)start - 1; i >= (int)end; --i)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return 0;
}

using state = tuple<int, int, int>;

const int INF = 1 << 30;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int H, W, K;
  cin >> H >> W >> K;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  --x1, --y1, --x2, --y2;
  vector<string> grid(H);
  for (auto &g : grid) {
    cin >> g;
  }
  priority_queue<state, vector<state>, greater<state>> que;
  vector<vector<int>> dp(H, vector<int>(W, INF));
  vector<int> dh = {-1, 0, 1, 0};
  vector<int> dw = {0, -1, 0, 1};
  dp[x1][y1] = 0;
  que.emplace(0, x1, y1);
  while (!que.empty()) {
    state now = que.top();
    que.pop();
    int dist = get<0>(now);
    int h = get<1>(now);
    int w = get<2>(now);
    if (dist > dp[h][w]) {
      continue;
    }
    rep(i, 0, 4) {
      int nh = h;
      int nw = w;
      rep(_, 0, K) {
        nh += dh[i];
        nw += dw[i];
        if (nh < 0 || nh >= H || nw < 0 || nw >= W || grid[nh][nw] == '@') {
          break;
        }
        if (dp[nh][nw] <= dist) {
          break;
        }
        dp[nh][nw] = dist + 1;
        que.emplace(dp[nh][nw], nh, nw);
      }
    }
  }
  if (dp[x2][y2] == INF) {
    cout << -1 << endl;
  } else {
    cout << dp[x2][y2] << endl;
  }
  return 0;
}