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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < int(n); i++)
using namespace std;
using ll = long long;
const int INF = (1 << 30) - 1;
const long long LINF = (1LL << 62) - 1;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

using P = pair<int, int>;
using T = tuple<P, int, int, int>;

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int h, w, k, x1, y1, x2, y2;
  cin >> h >> w >> k >> x1 >> y1 >> x2 >> y2;
  --x1, --y1, --x2, --y2;
  vector<string> s(h);
  rep(i, h) cin >> s[i];
  vector<vector<vector<P>>> grid(
      h, vector<vector<P>>(w, vector<P>(4, {INF, INF})));
  rep(i, 4) grid[x1][y1][i] = {1, 0};
  priority_queue<T, vector<T>, greater<T>> que;
  rep(i, 4) que.emplace(make_pair(1, 0), x1, y1, i);
  while (!que.empty()) {
    P dist;
    int x, y, d;
    tie(dist, x, y, d) = que.top();
    que.pop();
    if (dist > grid[x][y][d])
      continue;
    rep(i, 4) {
      if (chmin(grid[x][y][i], make_pair(dist.first + 1, 0)))
        que.emplace(make_pair(dist.first + 1, 0), x, y, i);
    }
    if (x + dx[d] < 0 || x + dx[d] >= h || y + dy[d] < 0 || y + dy[d] >= w)
      continue;
    if (s[x + dx[d]][y + dy[d]] == '@')
      continue;
    if (make_pair(dist.first, dist.second + 1) <
        grid[x + dx[d]][y + dy[d]][d]) {
      if (dist.second < k) {
        grid[x + dx[d]][y + dy[d]][d] = make_pair(dist.first, dist.second + 1);
        que.emplace(make_pair(dist.first, dist.second + 1), x + dx[d],
                    y + dy[d], d);
      } else {
        grid[x + dx[d]][y + dy[d]][d] = make_pair(dist.first + 1, 1);
        que.emplace(make_pair(dist.first + 1, 1), x + dx[d], y + dy[d], d);
      }
    }
  }
  int ans = INF;
  rep(i, 4) chmin(ans, grid[x2][y2][i].first);
  if (ans == INF)
    cout << -1 << endl;
  else
    cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < int(n); i++)
using namespace std;
using ll = long long;
const int INF = (1 << 30) - 1;
const long long LINF = (1LL << 62) - 1;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

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

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int h, w, k, x1, y1, x2, y2;
  cin >> h >> w >> k >> x1 >> y1 >> x2 >> y2;
  --x1, --y1, --x2, --y2;
  vector<string> s(h);
  rep(i, h) cin >> s[i];
  vector<vector<int>> grid(h, vector<int>(w, INF));
  grid[x1][y1] = 0;
  priority_queue<T, vector<T>, greater<T>> que;
  que.emplace(0, x1, y1);
  while (!que.empty()) {
    int dist, x, y;
    tie(dist, x, y) = que.top();
    que.pop();
    if (grid[x][y] > dist)
      continue;
    rep(i, 4) {
      for (int j = 1; j <= k; j++) {
        if (x + dx[i] * j < 0 || x + dx[i] * j >= h || y + dy[i] * j < 0 ||
            y + dy[i] * j >= w)
          break;
        if (s[x + dx[i] * j][y + dy[i] * j] == '@')
          break;
        if (chmin(grid[x + dx[i] * j][y + dy[i] * j], dist + 1))
          que.emplace(dist + 1, x + dx[i] * j, y + dy[i] * j);
      }
    }
  }
  if (grid[x2][y2] == INF)
    cout << -1 << endl;
  else
    cout << grid[x2][y2] << endl;
  return 0;
}