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

#include <bits/stdc++.h>
#include <iomanip>
using namespace std;
#define reps(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) reps(i, 0, n)
#define Rreps(i, n, e) for (int i = n - 1; i >= e; --i)
#define Rrep(i, n) Rreps(i, n, 0)
#define ALL(a) a.begin(), a.end()
#define fi first
#define se second
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;

ll N, M, H, W, Q, K, A, B;
string S;
const ll MOD = 998244353;
// const ll MOD = (1e+9) + 7;
typedef pair<ll, ll> P;
const ll INF = (1LL << 58);

ll encode(ll x, ll y) { return x * W + y; }
P decode(ll en) { return make_pair(en / W, en % W); }
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  } else
    return false;
}
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  } else
    return false;
}

int main() {
  cin >> H >> W >> K;
  vec x(2), y(2);
  rep(i, 2) cin >> x[i] >> y[i];
  string hasu(W + 1, '@');
  vector<string> G(H + 2, "@");
  rep(i, H) {
    cin >> S;
    S.push_back('@');
    G[i + 1].append(S);
  }
  G[0].append(hasu);
  G[H + 1].append(hasu);
  H += 2;
  W += 2;
  vec dh = {-1, 0, 1, 0}, dw = {0, -1, 0, 1};
  vec dist(H * W, INF);
  deque<P> deq;
  ll hw = H * W;
  deq.emplace_back(0, encode(x[0], y[0]) + hw * 0 + hw * K * 0);
  dist[encode(x[0], y[0])] = 0;
  mat dist_dir(hw, vec(4, -1));
  while (!deq.empty()) {
    P p = deq.front();
    deq.pop_front();
    int dir = p.se / (hw * K);
    p.se %= hw * K;
    ll rest = p.se / hw;
    p.se %= hw;
    P v = decode(p.se);
    if (dist[p.se] < p.fi)
      continue;
    if (rest != 0 && dist_dir[p.se][dir] > rest)
      continue;
    // cout<<v.fi<<' '<<v.se<<' '<<p.fi<<endl;
    rep(i, 4) {
      if (G[v.fi + dh[i]][v.se + dw[i]] == '@')
        continue;
      if (chmin(dist[encode(v.fi + dh[i], v.se + dw[i])],
                dist[p.se] + (rest == 0 || dir != i))) {
        rep(j, 4) dist_dir[encode(v.fi + dh[i], v.se + dw[i])][j] = -1;
        dist_dir[encode(v.fi + dh[i], v.se + dw[i])][i] =
            (dir == i && rest > 0 ? rest - 1 : K - 1);
        ll next = encode(v.fi + dh[i], v.se + dw[i]) +
                  (dir == i && rest > 0 ? hw * (rest - 1) + hw * K * dir
                                        : hw * (K - 1) + hw * K * i);
        if ((rest == 0 || dir != i))
          deq.emplace_back(dist[p.se] + 1, next);
        else
          deq.emplace_front(dist[p.se], next);
      } else if (dist[encode(v.fi + dh[i], v.se + dw[i])] ==
                     dist[p.se] + (rest == 0 || dir != i) &&
                 chmax(dist_dir[encode(v.fi + dh[i], v.se + dw[i])][i],
                       (dir == i && rest > 0 ? rest - 1 : K - 1))) {
        ll next = encode(v.fi + dh[i], v.se + dw[i]) +
                  (dir == i && rest > 0 ? hw * (rest - 1) + hw * K * dir
                                        : hw * (K - 1) + hw * K * i);
        if ((rest == 0 || dir != i))
          deq.emplace_back(dist[p.se] + 1, next);
        else
          deq.emplace_front(dist[p.se], next);
      }
    }
  }
  cout << (dist[encode(x[1], y[1])] == INF ? -1 : dist[encode(x[1], y[1])])
       << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <iomanip>
using namespace std;
#define reps(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) reps(i, 0, n)
#define Rreps(i, n, e) for (int i = n - 1; i >= e; --i)
#define Rrep(i, n) Rreps(i, n, 0)
#define ALL(a) a.begin(), a.end()
#define fi first
#define se second
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;

ll N, M, H, W, Q, K, A, B;
string S;
const ll MOD = 998244353;
// const ll MOD = (1e+9) + 7;
typedef pair<ll, ll> P;
const ll INF = (1LL << 58);

ll encode(ll x, ll y) { return x * W + y; }
P decode(ll en) { return make_pair(en / W, en % W); }
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  } else
    return false;
}
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  } else
    return false;
}

int main() {
  cin >> H >> W >> K;
  vec x(2), y(2);
  rep(i, 2) cin >> x[i] >> y[i];
  string hasu(W + 1, '@');
  vector<string> G(H + 2, "@");
  rep(i, H) {
    cin >> S;
    S.push_back('@');
    G[i + 1].append(S);
  }
  G[0].append(hasu);
  G[H + 1].append(hasu);
  H += 2;
  W += 2;
  vec dh = {-1, 0, 1, 0}, dw = {0, -1, 0, 1};
  vec dist(H * W, INF);
  queue<P> pque;
  pque.emplace(0, encode(x[0], y[0]));
  dist[encode(x[0], y[0])] = 0;
  while (!pque.empty()) {
    P p = pque.front();
    pque.pop();
    int last = (p.fi == 0 ? -1 : p.fi / INF);
    p.fi %= INF;
    if (dist[p.se] < p.fi)
      continue;
    P v = decode(p.se);
    rep(i, 4) {
      if (i == last)
        continue;
      reps(j, 1, K + 1) {
        if (G[v.fi + j * dh[i]][v.se + j * dw[i]] == '@')
          break;
        if (chmin(dist[encode(v.fi + j * dh[i], v.se + j * dw[i])],
                  dist[p.se] + 1)) {
          pque.emplace(dist[p.se] + 1 + INF * (j == K ? 4 : i),
                       encode(v.fi + j * dh[i], v.se + j * dw[i]));
        }
      }
    }
  }
  cout << (dist[encode(x[1], y[1])] == INF ? -1 : dist[encode(x[1], y[1])])
       << endl;
}