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

#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize(2)
#pragma G++ optimize(2)

#include <bits/stdc++.h>

#define LL long long
#define int long long
#define fi first
#define se second
#define pii pair<int, int>
#define pll pair<LL, LL>
#define pdd pair<double, double>
#define fastio ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define length(p1, p2)                                                         \\
  ((p1.fi - p2.fi) * (p1.fi - p2.fi) + (p1.se - p2.se) * (p1.se - p2.se))
#define ith(state, i) (state & (1 << i))
#define PI acos(-1.0)
#define SZ(x) ((int)(x.size()))

using namespace std;

constexpr int N = 2e5 + 9;
constexpr int M = 1e6 + 7;
constexpr int INF = 0x3f3f3f3f3f3f3f3f;
constexpr int INF2 = 0xcfcfcfcfcfcfcfcf;

int h, w, k;
int r1, c1, r2, c2;
const static vector<pii> dirs = {{0, 1}, {0, -1}, {-1, 0}, {1, 0}};

int upRound(int x) { return x / k * k + (!!(x % k)) * k; }

signed main() {
  fastio cin >> h >> w >> k;
  vector<vector<vector<int>>> dis(
      h + 1, vector<vector<int>>(w + 1, vector<int>(4, INF)));
  vector<vector<char>> a(h + 1, vector<char>(w + 1, 0));
  cin >> r1 >> c1 >> r2 >> c2;
  for (int i = 1; i <= h; i++)
    for (int j = 1; j <= w; j++)
      cin >> a[i][j];
  auto cmp = [&dis](const pair<pii, int> &p1, const pair<pii, int> &p2) {
    if (dis[p1.fi.fi][p1.fi.se][p1.se] != dis[p2.fi.fi][p2.fi.se][p2.se])
      return dis[p1.fi.fi][p1.fi.se][p1.se] < dis[p2.fi.fi][p2.fi.se][p2.se];
    return p1 < p2;
  };
  set<pair<pii, int>, decltype(cmp)> s(cmp);
  for (int d = 0; d < 4; d++)
    dis[r1][c1][d] = 0, s.emplace(pii{r1, c1}, d);
  while (!s.empty()) {
    auto p = *s.begin();
    s.erase(s.begin());
    pair<pii, int> p2;
    for (int d = 0; d < 4; d++) {
      p2.fi.fi = p.fi.fi + dirs[d].fi;
      p2.fi.se = p.fi.se + dirs[d].se;
      p2.se = d;
      if (p2.fi.fi < 1 || p2.fi.fi > h)
        continue;
      if (p2.fi.se < 1 || p2.fi.se > w)
        continue;
      if (a[p2.fi.fi][p2.fi.se] == '@')
        continue;
      int ndis = d == p.se ? dis[p.fi.fi][p.fi.se][p.se] + 1
                           : upRound(dis[p.fi.fi][p.fi.se][p.se]) + 1;
      if (ndis < dis[p2.fi.fi][p2.fi.se][p2.se]) {
        s.erase(p2);
        dis[p2.fi.fi][p2.fi.se][p2.se] = ndis;
        s.insert(p2);
      }
    }
  }
  int ans = INF;
  for (int d = 0; d < 4; d++)
    ans = min(ans, upRound(dis[r2][c2][d]) / k);
  cout << (ans > h * w ? -1 : ans) << endl;
  return 0;
}

Error: TLE

Fixed submission:

#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize(2)
#pragma G++ optimize(2)

#include <bits/stdc++.h>

#define LL long long
#define int long long
#define fi first
#define se second
#define pii pair<int, int>
#define pll pair<LL, LL>
#define pdd pair<double, double>
#define fastio ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define length(p1, p2)                                                         \\
  ((p1.fi - p2.fi) * (p1.fi - p2.fi) + (p1.se - p2.se) * (p1.se - p2.se))
#define ith(state, i) (state & (1 << i))
#define PI acos(-1.0)
#define SZ(x) ((int)(x.size()))

using namespace std;

constexpr int N = 2e5 + 9;
constexpr int M = 1e6 + 7;
constexpr int INF = 0x3f3f3f3f3f3f3f3f;
constexpr int INF2 = 0xcfcfcfcfcfcfcfcf;

int h, w, k;
int r1, c1, r2, c2;
const static vector<pii> dirs = {{0, 1}, {0, -1}, {-1, 0}, {1, 0}};

signed main() {
  fastio cin >> h >> w >> k;
  vector<vector<int>> dis(h + 1, vector<int>(w + 1, INF));
  vector<vector<char>> a(h + 1, vector<char>(w + 1, 0));
  cin >> r1 >> c1 >> r2 >> c2;
  for (int i = 1; i <= h; i++)
    for (int j = 1; j <= w; j++)
      cin >> a[i][j];
  dis[r1][c1] = 0;
  queue<pii> q;
  q.emplace(r1, c1);
  while (!q.empty()) {
    auto p = q.front();
    q.pop();
    pii p2;
    for (auto &d : dirs) {
      for (int l = 1; l <= k; l++) {
        p2.fi = p.fi + l * d.fi;
        p2.se = p.se + l * d.se;
        if (p2.fi < 1 || p2.fi > h)
          break;
        if (p2.se < 1 || p2.se > w)
          break;
        if (a[p2.fi][p2.se] == '@')
          break;
        if (dis[p2.fi][p2.se] > dis[p.fi][p.se] + 1) {
          dis[p2.fi][p2.se] = dis[p.fi][p.se] + 1;
          q.emplace(p2);
        }
      }
    }
  }
  cout << (dis[r2][c2] > h * w ? -1 : dis[r2][c2]) << endl;
  return 0;
}