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

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define reps(i, n) for (int i = 1; i <= (n); ++i)
#define repd(i, n) for (int i = (n)-1; i >= 0; --i)
#define repds(i, n) for (int i = (n); i > 0; --i)
#define loop(i, x, n) for (int i = (x); i < (n); ++i)
#define loops(i, x, n) for (int i = (x); i <= (n); ++i)
#define loopd(i, x, n) for (int i = (x); i > (n); --i)
#define loopds(i, x, n) for (int i = (x); i >= (n); --i)
#define itrep(i, a) for (auto i = begin(a); i != end(a); ++i)
#define itrepd(i, a) for (auto i = rbegin(a); i != rend(a); ++i)
#define each(i, x) for (auto &&i : x)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sz(a) ((int)a.size())
#define pow2(n) (n * n)
#define int ll
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
template <class T>
using priority_queuel = priority_queue<T, vector<T>, greater<T>>;
constexpr ll inf = static_cast<ll>(1e18);
constexpr int iinf = static_cast<int>(1e9);
constexpr double dinf = 1e10;
constexpr ld ldinf = 1e18;
constexpr double eps = 1e-9;
ll mod = static_cast<ll>(1e9 + 7);
template <class T, class... Args> constexpr void mins(T &a, Args... args) {
  a = min<T>({a, args...});
}
template <class T, class... Args> constexpr void maxs(T &a, Args... args) {
  a = max<T>({a, args...});
}
template <class F> class FixPoint : private F {
public:
  explicit constexpr FixPoint(F &&f) : F(std::forward<F>(f)) {}
  template <class... Args>
  constexpr decltype(auto) operator()(Args &&...args) const {
    return F::operator()(*this, std::forward<Args>(args)...);
  }
};
template <class F> constexpr decltype(auto) funcs(F &&f) {
  return FixPoint<F>{std::forward<F>(f)};
}
template <class T> std::vector<T> vecs(int n, T &&val) {
  return std::vector<T>(std::forward<int>(n), std::forward<T>(val));
}
template <class... Args> decltype(auto) vecs(int n, Args &&...args) {
  return std::vector<decltype(vecs(std::forward<Args>(args)...))>(
      std::forward<int>(n), vecs(std::forward<Args>(args)...));
}
template <class V, class T> void fill(V &x, const T &val) { x = val; }
template <class V, class T> void fill(vector<V> &vec, const T &val) {
  for (auto &&v : vec)
    fill(v, val);
}

template <class T, class U> struct fpair {
  T first;
  U second;
  fpair(T fi = T(), U se = U()) : first(fi), second(se) {}
  bool operator<(const fpair &x) const { return first < x.first; }
  bool operator>(const fpair &x) const { return first > x.first; }
};

void solve() {
  int h, w, k;
  cin >> h >> w >> k;
  int sx, sy, gx, gy;
  cin >> sx >> sy >> gx >> gy;
  --sx, --sy, --gx, --gy;
  vector<string> c(h);
  rep(i, h) cin >> c[i];
  auto d = vecs(h, w, 4, pair<int, int>(inf, 0));
  priority_queuel<fpair<pair<int, int>, pair<pair<int, int>, int>>> que;
  rep(i, 4) d[sx][sy][i] = {0, 0};
  que.emplace(make_pair(0LL, 0LL), make_pair(make_pair(sx, sy), 0LL));
  int dx[] = {1, 0, -1, 0};
  int dy[] = {0, 1, 0, -1};
  while (!que.empty()) {
    auto p = que.top();
    que.pop();
    int x = p.second.first.first;
    int y = p.second.first.second;
    int dir = p.second.second;
    if (d[x][y][dir] != p.first)
      continue;
    rep(i, 4) {
      int nx = x + dx[i];
      int ny = y + dy[i];
      if (nx < 0 || nx >= h || ny < 0 || ny >= w || c[nx][ny] == '@')
        continue;
      pair<int, int> nc = d[x][y][dir];
      if (i == dir) {
        if (nc.second == 0) {
          ++nc.first;
          nc.second = -(k - 1);
        } else
          ++nc.second;
      } else {
        ++nc.first;
        nc.second = -(k - 1);
      }

      if (d[nx][ny][i] > nc) {
        d[nx][ny][i] = nc;
        que.emplace(nc, make_pair(make_pair(nx, ny), i));
      }
    }
  }

  int ans = inf;
  rep(i, 4) mins(ans, d[gx][gy][i].first);
  cout << (ans < inf ? ans : -1) << endl;
}

signed main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  solve();
  cout.flush();
  return 0;
}


Error: TLE

Fixed submission:

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define reps(i, n) for (int i = 1; i <= (n); ++i)
#define repd(i, n) for (int i = (n)-1; i >= 0; --i)
#define repds(i, n) for (int i = (n); i > 0; --i)
#define loop(i, x, n) for (int i = (x); i < (n); ++i)
#define loops(i, x, n) for (int i = (x); i <= (n); ++i)
#define loopd(i, x, n) for (int i = (x); i > (n); --i)
#define loopds(i, x, n) for (int i = (x); i >= (n); --i)
#define itrep(i, a) for (auto i = begin(a); i != end(a); ++i)
#define itrepd(i, a) for (auto i = rbegin(a); i != rend(a); ++i)
#define each(i, x) for (auto &&i : x)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sz(a) ((int)a.size())
#define pow2(n) (n * n)
#define int ll
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
template <class T>
using priority_queuel = priority_queue<T, vector<T>, greater<T>>;
constexpr ll inf = static_cast<ll>(1e18);
constexpr int iinf = static_cast<int>(1e9);
constexpr double dinf = 1e10;
constexpr ld ldinf = 1e18;
constexpr double eps = 1e-9;
ll mod = static_cast<ll>(1e9 + 7);
template <class T, class... Args> constexpr void mins(T &a, Args... args) {
  a = min<T>({a, args...});
}
template <class T, class... Args> constexpr void maxs(T &a, Args... args) {
  a = max<T>({a, args...});
}
template <class F> class FixPoint : private F {
public:
  explicit constexpr FixPoint(F &&f) : F(std::forward<F>(f)) {}
  template <class... Args>
  constexpr decltype(auto) operator()(Args &&...args) const {
    return F::operator()(*this, std::forward<Args>(args)...);
  }
};
template <class F> constexpr decltype(auto) funcs(F &&f) {
  return FixPoint<F>{std::forward<F>(f)};
}
template <class T> std::vector<T> vecs(int n, T &&val) {
  return std::vector<T>(std::forward<int>(n), std::forward<T>(val));
}
template <class... Args> decltype(auto) vecs(int n, Args &&...args) {
  return std::vector<decltype(vecs(std::forward<Args>(args)...))>(
      std::forward<int>(n), vecs(std::forward<Args>(args)...));
}
template <class V, class T> void fill(V &x, const T &val) { x = val; }
template <class V, class T> void fill(vector<V> &vec, const T &val) {
  for (auto &&v : vec)
    fill(v, val);
}

template <class T, class U> struct fpair {
  T first;
  U second;
  fpair(T fi = T(), U se = U()) : first(fi), second(se) {}
  bool operator<(const fpair &x) const { return first < x.first; }
  bool operator>(const fpair &x) const { return first > x.first; }
};

void solve() {
  int h, w, k;
  cin >> h >> w >> k;
  int sx, sy, gx, gy;
  cin >> sx >> sy >> gx >> gy;
  --sx, --sy, --gx, --gy;
  vector<string> c(h);
  rep(i, h) cin >> c[i];

  auto toId = [&](int x, int y, int z) { return w * x + y + h * w * z; };
  auto ceilK = [&](int val) { return (val + k - 1) / k * k; };

  int dx[] = {1, 0, -1, 0};
  int dy[] = {0, 1, 0, -1};
  vector<int> d(h * w * 4, inf);
  priority_queue<fpair<int, int>> que;
  d[toId(sx, sy, 0)] = 0;
  que.emplace(0, toId(sx, sy, 0));
  while (!que.empty()) {
    auto p = que.top();
    que.pop();
    int v = p.second;
    if (d[v] != p.first)
      continue;
    int z = v / (h * w);
    int x = (v - (h * w) * z) / w;
    int y = (v - (h * w) * z) % w;
    rep(i, 4) {
      if (d[toId(x, y, i)] > ceilK(d[v])) {
        d[toId(x, y, i)] = ceilK(d[v]);
        que.emplace(d[toId(x, y, i)], toId(x, y, i));
      }
    }

    int nx = x + dx[z];
    int ny = y + dy[z];
    if (nx < 0 || nx >= h || ny < 0 || ny >= w || c[nx][ny] == '@')
      continue;
    if (d[toId(nx, ny, z)] > d[v] + 1) {
      d[toId(nx, ny, z)] = d[v] + 1;
      que.emplace(d[toId(nx, ny, z)], toId(nx, ny, z));
    }
  }

  int ans = inf;
  rep(i, 4) mins(ans, d[toId(gx, gy, i)]);
  cout << (ans < inf ? (ans + k - 1) / k : -1) << endl;
}

signed main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  solve();
  cout.flush();
  return 0;
}