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

#include <bits/stdc++.h>

using namespace std;

using i64 = long long;
#define forn(a, e) for (i64 a = 0; a < (i64)(e); a++)
#define forr(a, s, e) for (i64 a = s; a < (i64)(e); a++)
#define fore(e, a) for (auto &e : a)

#ifdef LOCAL
#define logv(a)                                                                \\
  {                                                                            \\
    cerr << #a << " = ";                                                       \\
    fore(e, a) { cerr << e << " "; }                                           \\
    cerr << "\\n";                                                              \\
  }
#define logvp(a)                                                               \\
  {                                                                            \\
    cerr << #a << " = ";                                                       \\
    fore(e, a) { cerr << "(" << e.first << ", " << e.second << ") "; }         \\
    cerr << "\\n";                                                              \\
  }
#define logvv(a)                                                               \\
  {                                                                            \\
    cerr << #a << " = \\n";                                                     \\
    fore(r, a) {                                                               \\
      fore(e, r) { cerr << e << " "; }                                         \\
      cerr << "\\n";                                                            \\
    }                                                                          \\
  }
#define logvf(a, field)                                                        \\
  {                                                                            \\
    cerr << #a "." #field << " = \\n";                                          \\
    fore(e, a) { cerr << e.field << " "; }                                     \\
    cerr << "\\n";                                                              \\
  }
#define logvff(a, f1, f2)                                                      \\
  {                                                                            \\
    cerr << #a ".{" #f1 << ", " #f2 << "} = \\n";                               \\
    fore(e, a) { cerr << "(" << e.f1 << ", " << e.f2 << ") "; }                \\
    cerr << "\\n";                                                              \\
  }
#define logs(a) cerr << #a << " = " << (a) << "\\n";
#define logss(a, b)                                                            \\
  cerr << #a << " = " << (a) << ", " << #b << " = " << (b) << "\\n";
#define logp(a)                                                                \\
  cerr << #a << " = "                                                          \\
       << "(" << a.first << ", " << a.second << ")"                            \\
       << "\\n";
#define cond(pred, stmt)                                                       \\
  if (pred) {                                                                  \\
    stmt                                                                       \\
  }
#else
#define logv(a)
#define logvp(a)
#define logvv(a)
#define logvf(a, field)
#define logvff(a, f1, f2)
#define logs(a)
#define logss(a, b)
#define logp(a)
#define cond(pred, stmt)
#endif

using iip = pair<int, int>;
using llp = pair<i64, i64>;
using ivec = vector<int>;
using llvec = vector<i64>;
using svec = vector<string>;
template <typename T> using vec = vector<T>;

template <typename T, typename Dim> auto make_vec(T value, Dim dim) {
  return vector<T>(dim, value);
}
template <typename T, typename Dim1, typename... Dim>
auto make_vec(T value, Dim1 dim1, Dim... dims) {
  return make_vec(make_vec(value, dims...), dim1);
}

template <typename T> bool uax(T &v, const T &newv) {
  if (v < newv) {
    v = newv;
    return true;
  } else
    return false;
}
template <typename T> bool uin(T &v, const T &newv) {
  if (v > newv) {
    v = newv;
    return true;
  } else
    return false;
}

template <typename T> istream &operator>>(istream &is, vector<T> &c) {
  for (auto &e : c)
    is >> e;
  return is;
}
template <typename T, size_t N>
istream &operator>>(istream &is, array<T, N> &c) {
  for (auto &e : c)
    is >> e;
  return is;
}

template <typename... T> istream &read(T &...args) {
  return (cin >> ... >> args);
}

static mt19937 rande(123123);
template <typename T> T rand_int(T from, T to) {
  uniform_int_distribution<T> distr(from, to);
  return distr(rande);
}

constexpr int INF = 1e9;
constexpr int MXB = 20;
using bin = bitset<MXB>;

struct st {
  int id;
  iip dc;
  bool operator<(const st &b) const { return dc < b.dc; }
};

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  rande.seed(chrono::steady_clock::now().time_since_epoch().count());

  int h, w, k;
  while (read(h, w, k)) {
    int x1, y1, x2, y2;
    read(x1, y1, x2, y2);
    x1--;
    y1--;
    x2--;
    y2--;

    svec mp(h);
    read(mp);

    // ivec dist(h * w, INF);
    auto dist = make_vec(iip{INF, INF}, h * w * 4);
    auto ind = [=](int i, int j, int dr) { return dr * h * w + i * w + j; };
    auto coords = [=](int id) {
      int dr = id / (h * w);
      id %= h * w;
      int i = id / w;
      int j = id % w;
      return tuple<int, int, int>{i, j, dr};
    };

    forn(i, 4) { dist[ind(x1, y1, i)] = {0, k}; }
    deque<st> q;
    q.push_back({ind(x1, y1, 0), {0, k}});
    // q.push({ind(x1, y1, 0), 0, k});

    while (!q.empty()) {
      // int dds = q.top().d;
      auto [id, dc] = q.front();
      q.pop_front();
      if (dist[id] != dc) {
        continue;
      }
      // vec<tuple<int, int, int>> vs;
      // while (!q.empty() && q.top().d == dds) {
      //     auto [id, ds, c] = q.top(); q.pop();
      //     if (dist[id] != ds) {
      //         continue;
      //     }
      //     vs.emplace_back(id, ds, c);
      // }
      // for (auto& [id, ds, c] : vs) {

      auto [x, y, dr] = coords(id);
      iip dirs[] = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}};
      forn(i, 4) {
        auto [di, dj] = dirs[i];
        int ni = x + di;
        int nj = y + dj;
        if (ni >= 0 && ni < h && nj >= 0 && nj < w && mp[ni][nj] != '@') {
          int nid = ind(ni, nj, i);
          iip nd;
          if (i == dr && dc.second < k) {
            nd = {dc.first, dc.second + 1};
          } else {
            nd = {dc.first + 1, 1};
          }
          // logss(ni, nj);
          // logs(nd);
          if (nd < dist[nid]) {
            if (i == dr && dc.second < k) {
              q.push_front({nid, nd});
            } else {
              q.push_back({nid, nd});
            }
            dist[nid] = nd;
          }
        }
      }
    }

    // auto dd = make_vec(INF, h, w);
    // forn(i, h) forn(j, w) forn(p, 4) {
    //     uin(dd[i][j], dist[i][j][p]);
    // }

    // int ans = dd[x2][y2];
    int ans = INF;
    forn(i, 4) { uin(ans, dist[ind(x2, y2, i)].first); }
    // logvv(dd);
    if (ans == INF) {
      cout << -1 << endl;
    } else {
      cout << ans << endl;
    }
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

using i64 = long long;
#define forn(a, e) for (i64 a = 0; a < (i64)(e); a++)
#define forr(a, s, e) for (i64 a = s; a < (i64)(e); a++)
#define fore(e, a) for (auto &e : a)

#ifdef LOCAL
#define logv(a)                                                                \\
  {                                                                            \\
    cerr << #a << " = ";                                                       \\
    fore(e, a) { cerr << e << " "; }                                           \\
    cerr << "\\n";                                                              \\
  }
#define logvp(a)                                                               \\
  {                                                                            \\
    cerr << #a << " = ";                                                       \\
    fore(e, a) { cerr << "(" << e.first << ", " << e.second << ") "; }         \\
    cerr << "\\n";                                                              \\
  }
#define logvv(a)                                                               \\
  {                                                                            \\
    cerr << #a << " = \\n";                                                     \\
    fore(r, a) {                                                               \\
      fore(e, r) { cerr << e << " "; }                                         \\
      cerr << "\\n";                                                            \\
    }                                                                          \\
  }
#define logvf(a, field)                                                        \\
  {                                                                            \\
    cerr << #a "." #field << " = \\n";                                          \\
    fore(e, a) { cerr << e.field << " "; }                                     \\
    cerr << "\\n";                                                              \\
  }
#define logvff(a, f1, f2)                                                      \\
  {                                                                            \\
    cerr << #a ".{" #f1 << ", " #f2 << "} = \\n";                               \\
    fore(e, a) { cerr << "(" << e.f1 << ", " << e.f2 << ") "; }                \\
    cerr << "\\n";                                                              \\
  }
#define logs(a) cerr << #a << " = " << (a) << "\\n";
#define logss(a, b)                                                            \\
  cerr << #a << " = " << (a) << ", " << #b << " = " << (b) << "\\n";
#define logp(a)                                                                \\
  cerr << #a << " = "                                                          \\
       << "(" << a.first << ", " << a.second << ")"                            \\
       << "\\n";
#define cond(pred, stmt)                                                       \\
  if (pred) {                                                                  \\
    stmt                                                                       \\
  }
#else
#define logv(a)
#define logvp(a)
#define logvv(a)
#define logvf(a, field)
#define logvff(a, f1, f2)
#define logs(a)
#define logss(a, b)
#define logp(a)
#define cond(pred, stmt)
#endif

using iip = pair<int, int>;
using llp = pair<i64, i64>;
using ivec = vector<int>;
using llvec = vector<i64>;
using svec = vector<string>;
template <typename T> using vec = vector<T>;

template <typename T, typename Dim> auto make_vec(T value, Dim dim) {
  return vector<T>(dim, value);
}
template <typename T, typename Dim1, typename... Dim>
auto make_vec(T value, Dim1 dim1, Dim... dims) {
  return make_vec(make_vec(value, dims...), dim1);
}

template <typename T> bool uax(T &v, const T &newv) {
  if (v < newv) {
    v = newv;
    return true;
  } else
    return false;
}
template <typename T> bool uin(T &v, const T &newv) {
  if (v > newv) {
    v = newv;
    return true;
  } else
    return false;
}

template <typename T> istream &operator>>(istream &is, vector<T> &c) {
  for (auto &e : c)
    is >> e;
  return is;
}
template <typename T, size_t N>
istream &operator>>(istream &is, array<T, N> &c) {
  for (auto &e : c)
    is >> e;
  return is;
}

template <typename... T> istream &read(T &...args) {
  return (cin >> ... >> args);
}

static mt19937 rande(123123);
template <typename T> T rand_int(T from, T to) {
  uniform_int_distribution<T> distr(from, to);
  return distr(rande);
}

constexpr int INF = 1e9;
constexpr int MXB = 20;
using bin = bitset<MXB>;

struct st {
  int id;
  int d;
  int c;
  bool operator<(const st &b) const { return iip{d, c} < iip{b.d, b.c}; }
};

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  rande.seed(chrono::steady_clock::now().time_since_epoch().count());

  int h, w, k;
  while (read(h, w, k)) {
    int x1, y1, x2, y2;
    read(x1, y1, x2, y2);
    x1--;
    y1--;
    x2--;
    y2--;

    svec mp(h);
    read(mp);

    // ivec dist(h * w, INF);
    auto dist = make_vec(INF, h * w * 4);
    auto ind = [=](int i, int j, int dr) { return dr * h * w + i * w + j; };
    auto coords = [=](int id) {
      int dr = id / (h * w);
      id %= h * w;
      int i = id / w;
      int j = id % w;
      return tuple<int, int, int>{i, j, dr};
    };

    forn(i, 4) { dist[ind(x1, y1, i)] = 0; }
    priority_queue<st> q;
    q.push({ind(x1, y1, 0), 0, k});

    while (!q.empty()) {
      int dds = q.top().d;
      vec<tuple<int, int, int>> vs;
      while (!q.empty() && q.top().d == dds) {
        auto [id, ds, c] = q.top();
        q.pop();
        if (dist[id] != ds) {
          continue;
        }
        vs.emplace_back(id, ds, c);
      }
      for (auto &[id, ds, c] : vs) {

        auto [x, y, dr] = coords(id);
        iip dirs[] = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}};
        forn(i, 4) {
          auto [di, dj] = dirs[i];
          int ni = x + di;
          int nj = y + dj;
          if (ni >= 0 && ni < h && nj >= 0 && nj < w && mp[ni][nj] != '@') {
            int nid = ind(ni, nj, i);
            int nd = ds + (i != dr || c == k);
            // logss(ni, nj);
            // logs(nd);
            if (nd < dist[nid]) {
              if (i == dr && c < k) {
                q.push({nid, nd, c + 1});
              } else {
                q.push({nid, nd, 1});
              }
              dist[nid] = nd;
            }
          }
        }
      }
    }

    // auto dd = make_vec(INF, h, w);
    // forn(i, h) forn(j, w) forn(p, 4) {
    //     uin(dd[i][j], dist[i][j][p]);
    // }

    // int ans = dd[x2][y2];
    int ans = INF;
    forn(i, 4) { uin(ans, dist[ind(x2, y2, i)]); }
    // logvv(dd);
    if (ans == INF) {
      cout << -1 << endl;
    } else {
      cout << ans << endl;
    }
  }
}