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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, x, n) for (int i = x; i < (n); i++)
#define pb push_back
#define ALL(v) v.begin(), v.end()
const long long INF = 1LL << 60; // 仮想的な無限大の値;
using namespace std;
using ll = long long;
using P = pair<int, int>;
using LP = pair<ll, int>;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int dx[]{-1, 0, 1, 0};
int dy[]{0, -1, 0, 1};
int main() {
  int H, W, K, si, sj, ti, tj;
  cin >> H >> W >> K >> si >> sj >> ti >> tj;
  --si;
  --sj;
  --ti;
  --tj;
  vector<string> G(H);
  rep(i, H) cin >> G[i];

  auto toId = [&](int i, int j, int v) { return (i * W + j) * 4 + v; };
  auto modCeilK = [&](ll x) { return (x + K - 1) / K * K; };
  vector<ll> dist(H * W * 4, INF);
  priority_queue<LP, vector<LP>, greater<LP>> q;
  auto push = [&](int i, int j, int v, ll x) {
    auto id = toId(i, j, v);
    if (dist[id] <= x)
      return;
    dist[id] = x;
    q.emplace(x, id);
  };
  rep(v, 4) push(si, sj, v, 0);
  while (!q.empty()) {
    ll d = q.top().first;
    int id = q.top().second;
    q.pop();
    if (dist[id] != d)
      continue;
    int i = id / W / 4;
    int j = id / 4 % W;
    int v = id % 4;
    rep(nv, 4) { push(i, j, nv, modCeilK(d)); }
    int ni = i + dx[v], nj = j + dy[v];
    if (!(0 <= ni && ni < H && 0 <= nj && nj < W))
      continue;
    if (G[ni][nj] == '@')
      continue;
    push(ni, nj, v, d + 1);
  }
  ll ans = INF;
  rep(v, 4) chmin(ans, dist[toId(ti, tj, v)]);
  if (ans == INF)
    ans = -1;
  else
    ans = modCeilK(ans) / K;
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, x, n) for (int i = x; i < (n); i++)
#define pb push_back
#define ALL(v) v.begin(), v.end()
const long long INF = 1LL << 60; // 仮想的な無限大の値;
using namespace std;
using ll = long long;
using P = pair<int, int>;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int dx[]{-1, 0, 1, 0};
int dy[]{0, -1, 0, 1};
struct Edge {
  int cost;
  P to;
  Edge(int cost, P to) : to(to), cost(cost) {}
};
using Graph = vector<vector<Edge>>;
int main() {
  int H, W, K, si, sj, ti, tj;
  cin >> H >> W >> K >> si >> sj >> ti >> tj;
  --si;
  --sj;
  --ti;
  --tj;
  vector<string> A(H);
  rep(i, H) cin >> A[i];
  auto c = [](Edge &a, Edge &b) { return a.cost > b.cost; };
  priority_queue<Edge, vector<Edge>, decltype(c)> q(c);

  vector<vector<int>> dp(H);
  rep(i, H) rep(j, W) dp[i].pb(-1);
  Edge e(0, P(si, sj));
  q.emplace(e);
  while (!q.empty()) {
    Edge v = q.top();
    q.pop();
    int vi = v.to.first;
    int vj = v.to.second;
    if (dp[vi][vj] != -1)
      continue;
    dp[vi][vj] = v.cost;
    int f = 0;
    rep2(i, 1, K + 1) {
      int di = i;
      rep(k, 4) {
        if (f & (1 << k))
          continue;
        int x = vi + di * dx[k];
        int y = vj + di * dy[k];
        if (x < 0 || x >= H)
          continue;
        if (y < 0 || y >= W)
          continue;
        if (A[x][y] == '@') {
          f |= 1 << k;
          continue;
        }
        Edge w(v.cost + 1, P(x, y));
        q.emplace(w);
      }
    }
  }
  cout << dp[ti][tj] << endl;
  return 0;
}