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

#include <bits/stdc++.h>
using namespace std;
using ll = int_fast64_t;
const int mod = 1e9 + 7;
const int inf = (1 << 30) - 1;
const ll infll = (1LL << 61) - 1;
#define fast() ios::sync_with_stdio(false), cin.tie(nullptr)

template <typename A, typename B> string to_string(pair<A, B> p);

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);

string to_string(const string &s) { return '"' + s + '"'; }

string to_string(const char *s) { return to_string((string)s); }

string to_string(bool b) { return (b ? "true" : "false"); }

string to_string(vector<bool> v) {
  bool first = true;
  string res = "{";
  for (int i = 0; i < static_cast<int>(v.size()); i++) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(v[i]);
  }
  res += "}";
  return res;
}

template <size_t N> string to_string(bitset<N> v) {
  string res = "";
  for (size_t i = 0; i < N; i++) {
    res += static_cast<char>('0' + v[i]);
  }
  return res;
}

template <typename A> string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}

template <typename A, typename B> string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
         to_string(get<2>(p)) + ")";
}

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
         to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}

void debug_out() { cerr << endl; }

template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}

int main() {
  fast();

  using P = pair<int, int>;

  // d[0] : うえ, d[1] : みぎ, d[2] : した, d[3] : ひだり
  int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
  int H, W, K;
  cin >> H >> W >> K;
  vector<string> grid(H);
  vector<vector<vector<P>>> d(H);

  int sx, sy, gx, gy;

  cin >> sx >> sy >> gx >> gy;
  sx--, sy--, gx--, gy--;

  for (int i = 0; i < H; i++) {
    cin >> grid[i];
  }

  for (int i = 0; i < H; i++) // init
  {
    d[i] = vector<vector<P>>(W, vector<P>(4, P(inf, inf)));
  }

  // (最短距離, その方向に進んだ回数), (x, y), dir
  deque<pair<P, int>> que;
  for (int i = 0; i < 4; i++) {
    d[sx][sy][i] = P(1, 0);
    que.push_back({P(sx, sy), i});
  }

  while (!que.empty()) {
    auto v = que.front();
    que.pop_front();
    int vx = v.first.first, vy = v.first.second, dir = v.second;

    for (int i = 0; i < 4; i++) // 向きを変える
    {
      if (i == dir)
        continue;
      P ncost = make_pair(d[vx][vy][dir].first + 1, 0);

      if (d[vx][vy][i] <= ncost)
        continue;

      d[vx][vy][i] = ncost;

      que.push_back({P(vx, vy), i});
    }

    // 直進する
    int nx = vx + dx[dir], ny = vy + dy[dir];

    if (nx < 0 || nx >= H || ny < 0 || ny >= W) // grid の外
      continue;
    if (grid[nx][ny] == '@') // 行き止まり
      continue;

    int df = d[vx][vy][dir].first, ds = d[vx][vy][dir].second;
    P ncost = make_pair(df + (ds + 1) / (K + 1), ds % K + 1);
    if (d[nx][ny][dir] <= ncost)
      continue;
    d[nx][ny][dir] = ncost;

    if (ds >= K)
      que.push_back({P(nx, ny), dir});
    else
      que.push_front({P(nx, ny), dir});
  }

  int min_cost = (*min_element(d[gx][gy].begin(), d[gx][gy].end())).first;

  cout << (min_cost == inf ? -1 : min_cost) << "\\n";
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = int_fast64_t;
const int mod = 1e9 + 7;
const int inf = (1 << 30) - 1;
const ll infll = (1LL << 61) - 1;
#define fast() ios::sync_with_stdio(false), cin.tie(nullptr)

template <typename A, typename B> string to_string(pair<A, B> p);

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);

string to_string(const string &s) { return '"' + s + '"'; }

string to_string(const char *s) { return to_string((string)s); }

string to_string(bool b) { return (b ? "true" : "false"); }

string to_string(vector<bool> v) {
  bool first = true;
  string res = "{";
  for (int i = 0; i < static_cast<int>(v.size()); i++) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(v[i]);
  }
  res += "}";
  return res;
}

template <size_t N> string to_string(bitset<N> v) {
  string res = "";
  for (size_t i = 0; i < N; i++) {
    res += static_cast<char>('0' + v[i]);
  }
  return res;
}

template <typename A> string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}

template <typename A, typename B> string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
         to_string(get<2>(p)) + ")";
}

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
         to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}

void debug_out() { cerr << endl; }

template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}

int main() {
  fast();

  using P = pair<int, int>;

  // d[0] : うえ, d[1] : みぎ, d[2] : した, d[3] : ひだり
  int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
  int H, W, K;
  cin >> H >> W >> K;
  vector<string> grid(H);
  vector<vector<vector<P>>> d(H);

  int sx, sy, gx, gy;

  cin >> sx >> sy >> gx >> gy;
  sx--, sy--, gx--, gy--;

  for (int i = 0; i < H; i++) {
    cin >> grid[i];
  }

  for (int i = 0; i < H; i++) // init
  {
    d[i] = vector<vector<P>>(W, vector<P>(4, P(inf, inf)));
  }

  // (最短距離, その方向に進んだ回数), (x, y), dir
  deque<pair<P, int>> que;
  for (int i = 0; i < 4; i++) {
    d[sx][sy][i] = P(1, 0);
    que.push_back({P(sx, sy), i});
  }

  while (!que.empty()) {
    auto v = que.front();
    que.pop_front();
    int vx = v.first.first, vy = v.first.second, dir = v.second;

    for (int i = 0; i < 4; i++) // 向きを変える
    {
      if (i == dir)
        continue;
      P ncost = make_pair(d[vx][vy][dir].first + 1, 0);

      if (d[vx][vy][i] < ncost)
        continue;

      d[vx][vy][i] = ncost;

      que.push_back({P(vx, vy), i});
    }

    // 直進する
    int nx = vx + dx[dir], ny = vy + dy[dir];

    if (nx < 0 || nx >= H || ny < 0 || ny >= W) // grid の外
      continue;
    if (grid[nx][ny] == '@') // 行き止まり
      continue;

    int df = d[vx][vy][dir].first, ds = d[vx][vy][dir].second;
    P ncost = make_pair(df + (ds + 1) / (K + 1), ds % K + 1);
    if (d[nx][ny][dir] < ncost)
      continue;
    d[nx][ny][dir] = ncost;

    if (ds >= K)
      que.push_back({P(nx, ny), dir});
    else
      que.push_front({P(nx, ny), dir});
  }

  int min_cost = (*min_element(d[gx][gy].begin(), d[gx][gy].end())).first;

  cout << (min_cost == inf ? -1 : min_cost) << "\\n";
}