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

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

using ll = long long;
using Vi = vector<int>;
using Vl = vector<ll>;
using Pii = pair<int, int>;
using Pll = pair<ll, ll>;

constexpr int I_INF = numeric_limits<int>::max();
constexpr ll L_INF = numeric_limits<ll>::max();

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

struct Pos {
  int x, y;
};

void solve() {
  int H, W;
  ll K;
  cin >> H >> W >> K;
  Pos s, t;
  cin >> s.x >> s.y >> t.x >> t.y;
  --s.x, --s.y, --t.x, --t.y;
  vector<vector<bool>> cell(H, vector<bool>(W));
  for (int i = 0; i < H; i++) {
    string S;
    cin >> S;
    for (int j = 0; j < W; j++) {
      if (S[j] == '@')
        cell[i][j] = true;
    }
  }

  if (cell[s.x][s.y] == 1) {
    cout << -1 << "\\n";
    return;
  }

  constexpr Pos step[] = {{1, 0}, {0, -1}, {-1, 0}, {0, 1}};

  using Vert = tuple<int, int, int>;
  auto l2v = [&](ll a) -> Vert {
    int v = a % 4;
    a /= 4;
    int y = a % W;
    a /= W;
    int x = a;
    return {x, y, v};
  };
  auto v2l = [&](const Vert &a) -> ll {
    const auto &[x, y, v] = a;
    return (ll)x * 4 * W + y * 4 + v;
  };

  using Tup = pair<ll, ll>;
  unordered_map<ll, ll> dp;
  priority_queue<Tup, vector<Tup>, greater<Tup>> q;
  q.emplace(0, v2l({s.x, s.y, 0}));
  dp[v2l({s.x, s.y, 0})] = 0;
  while (!q.empty()) {
    auto [c, p] = q.top();
    q.pop();
    if (c > dp[p])
      continue;
    auto [x, y, v] = l2v(p);
    int xn, yn;
    ll cn;
    // 前進
    xn = x + step[v].x;
    yn = y + step[v].y;
    if (xn >= 0 && xn < H && yn >= 0 && yn < W) {
      if (!cell[xn][yn]) {
        cn = c + 1;
        ll nxt = v2l({xn, yn, v});
        if (dp.find(nxt) == dp.end()) {
          dp[nxt] = L_INF;
        }
        if (cn < dp[nxt]) {
          dp[nxt] = cn;
          q.emplace(cn, nxt);
        }
      }
    }
    // 回転
    v = (v + 1) % 4;
    xn = x;
    yn = y;
    cn = (c + K - 1) / K * K;
    ll nxt = v2l({xn, yn, v});
    if (dp.find(nxt) == dp.end()) {
      dp[nxt] = L_INF;
    }
    if (cn < dp[nxt]) {
      dp[nxt] = cn;
      q.emplace(cn, nxt);
    }
  }

  ll ans = L_INF;
  for (ll i = 0; i < 4; i++) {
    ll p = v2l({t.x, t.y, i});
    if (dp.find(p) == dp.end())
      continue;
    if (ll tmp = dp[p]; tmp != L_INF) {
      ans = min(ans, (tmp + K - 1) / K);
    }
  }

  if (ans != L_INF)
    cout << ans << "\\n";
  else
    cout << -1 << "\\n";
}

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

int main() {
#ifdef FASTIO
  cin.tie(0), cout.tie(0);
  ios::sync_with_stdio(false);
#endif
#ifdef FILEINPUT
  ifstream ifs("./in_out/input.txt");
  cin.rdbuf(ifs.rdbuf());
#endif
#ifdef FILEOUTPUT
  ofstream ofs("./in_out/output.txt");
  cout.rdbuf(ofs.rdbuf());
#endif
  solve();
  cout << flush;
  return 0;
}

Error: TLE

Fixed submission:

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

using ll = long long;
using Vi = vector<int>;
using Vl = vector<ll>;
using Pii = pair<int, int>;
using Pll = pair<ll, ll>;

constexpr int I_INF = numeric_limits<int>::max();
constexpr ll L_INF = numeric_limits<ll>::max();

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

struct Pos {
  int x, y;
};

void solve() {
  int H, W;
  ll K;
  cin >> H >> W >> K;
  Pos s, t;
  cin >> s.x >> s.y >> t.x >> t.y;
  --s.x, --s.y, --t.x, --t.y;
  vector<vector<bool>> cell(H, vector<bool>(W));
  for (int i = 0; i < H; i++) {
    string S;
    cin >> S;
    for (int j = 0; j < W; j++) {
      if (S[j] == '@')
        cell[i][j] = true;
    }
  }

  if (cell[s.x][s.y] == 1) {
    cout << -1 << "\\n";
    return;
  }

  constexpr Pos step[] = {{1, 0}, {0, -1}, {-1, 0}, {0, 1}};

  using Tup = tuple<ll, int, int, int>; // cost, x, y, v
  using Vert = tuple<int, int, int>;
  map<Vert, ll> dp;
  priority_queue<Tup, vector<Tup>, greater<Tup>> q;
  q.emplace(0, s.x, s.y, 0);
  dp[{s.x, s.y, 0}] = 0;
  while (!q.empty()) {
    auto [c, x, y, v] = q.top();
    q.pop();
    if (c > dp[{x, y, v}])
      continue;
    int xn, yn;
    ll cn;
    // 前進
    xn = x + step[v].x;
    yn = y + step[v].y;
    if (xn >= 0 && xn < H && yn >= 0 && yn < W) {
      if (!cell[xn][yn]) {
        cn = c + 1;
        Vert nxt = {xn, yn, v};
        if (dp.find(nxt) == dp.end()) {
          dp[nxt] = L_INF;
        }
        if (cn < dp[nxt]) {
          dp[nxt] = cn;
          q.emplace(cn, xn, yn, v);
        }
      }
    }
    // 回転
    v = (v + 1) % 4;
    xn = x;
    yn = y;
    cn = (c + K - 1) / K * K;
    Vert nxt = {xn, yn, v};
    if (dp.find(nxt) == dp.end()) {
      dp[nxt] = L_INF;
    }
    if (cn < dp[nxt]) {
      dp[nxt] = cn;
      q.emplace(cn, xn, yn, v);
    }
  }

  ll ans = L_INF;
  for (ll i = 0; i < 4; i++) {
    if (dp.find({t.x, t.y, i}) == dp.end())
      continue;
    if (ll tmp = dp[{t.x, t.y, i}]; tmp != L_INF) {
      ans = min(ans, (tmp + K - 1) / K);
    }
  }

  if (ans != L_INF)
    cout << ans << "\\n";
  else
    cout << -1 << "\\n";
}

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

int main() {
#ifdef FASTIO
  cin.tie(0), cout.tie(0);
  ios::sync_with_stdio(false);
#endif
#ifdef FILEINPUT
  ifstream ifs("./in_out/input.txt");
  cin.rdbuf(ifs.rdbuf());
#endif
#ifdef FILEOUTPUT
  ofstream ofs("./in_out/output.txt");
  cout.rdbuf(ofs.rdbuf());
#endif
  solve();
  cout << flush;
  return 0;
}