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

#include <bits/stdc++.h>
using namespace std;

typedef int_fast32_t int32;
typedef int_fast64_t int64;

const int32 inf = 1e9 + 7;
const int32 MOD = 1000000007;
const int64 llinf = 1e18;

#define YES(n) cout << ((n) ? "YES\\n" : "NO\\n")
#define Yes(n) cout << ((n) ? "Yes\\n" : "No\\n")
#define POSSIBLE(n) cout << ((n) ? "POSSIBLE\\n" : "IMPOSSIBLE\\n")
#define ANS(n) cout << (n) << "\\n"
#define REP(i, n) for (int64 i = 0; i < (n); ++i)
#define FOR(i, a, b) for (int64 i = (a); i < (b); i++)
#define FORR(i, a, b) for (int64 i = (a); i >= (b); i--)
#define all(obj) (obj).begin(), (obj).end()
#define rall(obj) (obj).rbegin(), (obj).rend()
#define fi first
#define se second
#define pb(a) push_back(a)
typedef pair<int32, int32> pii;
typedef pair<int64, int64> pll;

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;
}

typedef tuple<pii, int32, int32, int32> tup4;

int32 dx[4] = {1, 0, -1, 0};
int32 dy[4] = {0, 1, 0, -1};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int32 h, w, k;
  cin >> h >> w >> k;
  int32 x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  --x1, --y1, --x2, --y2;
  vector<string> c(h);
  REP(i, h) cin >> c[i];
  priority_queue<tup4, vector<tup4>, greater<tup4>> que;
  vector<vector<vector<pii>>> dist(
      h, vector<vector<pii>>(w, vector<pii>(4, pii(inf, 0))));
  REP(i, 4) {
    dist[x1][y1][i] = pii(0, 0);
    que.emplace(pii(0, 0), x1, y1, i);
  }
  while (!que.empty()) {
    auto [d, cx, cy, dir] = que.top();
    que.pop();
    if (dist[cx][cy][dir] < d)
      continue;
    REP(i, 4) {
      if (i != dir) {
        auto nd = d;
        if (nd.se > 0) {
          nd.fi++;
          nd.se = 0;
        }
        if (chmin(dist[cx][cy][i], nd)) {
          que.emplace(nd, cx, cy, i);
        }
      } else {
        int32 nx = cx + dx[i];
        int32 ny = cy + dy[i];
        if (nx < 0 || h <= nx)
          continue;
        if (ny < 0 || w <= ny)
          continue;
        if (c[nx][ny] == '@')
          continue;
        pii nd = d;
        nd.se++;
        while (nd.se >= k) {
          nd.fi++;
          nd.se -= k;
        }
        if (chmin(dist[nx][ny][dir], nd))
          que.emplace(nd, nx, ny, dir);
      }
    }
  }
  int32 ans = inf;
  REP(i, 4) {
    if (dist[x2][y2][i].se > 0) {
      dist[x2][y2][i].fi++;
    }
    chmin(ans, dist[x2][y2][i].fi);
  }
  if (ans == inf)
    ans = -1;
  ANS(ans);
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef int_fast32_t int32;
typedef int_fast64_t int64;

const int32 inf = 1e9 + 7;
const int32 MOD = 1000000007;
const int64 llinf = 1e18;

#define YES(n) cout << ((n) ? "YES\\n" : "NO\\n")
#define Yes(n) cout << ((n) ? "Yes\\n" : "No\\n")
#define POSSIBLE(n) cout << ((n) ? "POSSIBLE\\n" : "IMPOSSIBLE\\n")
#define ANS(n) cout << (n) << "\\n"
#define REP(i, n) for (int64 i = 0; i < (n); ++i)
#define FOR(i, a, b) for (int64 i = (a); i < (b); i++)
#define FORR(i, a, b) for (int64 i = (a); i >= (b); i--)
#define all(obj) (obj).begin(), (obj).end()
#define rall(obj) (obj).rbegin(), (obj).rend()
#define fi first
#define se second
#define pb(a) push_back(a)
typedef pair<int32, int32> pii;
typedef pair<int64, int64> pll;

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;
}

int32 dx[4] = {1, 0, -1, 0};
int32 dy[4] = {0, 1, 0, -1};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int32 h, w, k;
  cin >> h >> w >> k;
  int32 x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  --x1, --y1, --x2, --y2;
  vector<string> c(h);
  REP(i, h) cin >> c[i];
  queue<pii> que;
  vector<vector<int32>> dist(h, vector<int32>(w, inf));
  dist[x1][y1] = 0;
  que.emplace(x1, y1);
  while (!que.empty()) {
    auto [cx, cy] = que.front();
    que.pop();
    REP(i, 4) {
      FOR(d, 1, k + 1) {
        int32 nx = cx + dx[i] * d;
        int32 ny = cy + dy[i] * d;
        if (nx < 0 || h <= nx)
          break;
        if (ny < 0 || w <= ny)
          break;
        if (c[nx][ny] == '@')
          break;
        if (chmin(dist[nx][ny], dist[cx][cy] + 1))
          que.emplace(nx, ny);
        if (nx == x2 && ny == y2) {
          ANS(dist[x2][y2]);
          return 0;
        }
      }
    }
  }
  ANS(-1);
  return 0;
}