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

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

// Inserted snippets: io, root
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
template <typename H> bool chmin(H &v1, const H v2) {
  if (v1 > v2) {
    v1 = v2;
    return true;
  }
  return false;
}
template <typename H> bool chmax(H &v1, const H v2) {
  if (v1 < v2) {
    v1 = v2;
    return true;
  }
  return false;
}
template <typename H> void read(H &head) { cin >> head; }
template <typename H, typename... T> void read(H &head, T &...tail) {
  cin >> head;
  read(tail...);
}
template <typename H> void write(H head) { cout << head << '\\n'; }
template <typename H, typename... T> void write(H head, T... tail) {
  cout << head << " ";
  write(tail...);
}
template <typename... T> void die(T... tok) {
  write(tok...);
  exit(0);
}
// End snippets

struct hist {
  int x, y, dir;
  pii dist;
  bool operator<(const hist &h) const { return dist > h.dist; }
};

int main() {
  cin.tie(0)->sync_with_stdio(0);
  cin.exceptions(cin.failbit);

  int h, w, k;
  read(h, w, k);
  vector<string> mp(h);

  int x1, y1, x2, y2;
  read(x1, y1, x2, y2);
  --x1;
  --y1;
  --x2;
  --y2;

  rep(i, 0, h) read(mp[i]);

  const int inf = 0x3f3f3f3f;
  vector<vector<vector<pii>>> mindist(
      h, vector<vector<pii>>(w, vector<pii>(4, {inf, 0})));
  rep(i, 0, 4) mindist[x1][y1][i] = {1, 0};

  priority_queue<hist> pq;
  rep(i, 0, 4) pq.push({x1, y1, i, {1, 0}});

  const int dx[] = {-1, 1, 0, 0}, dy[] = {0, 0, -1, 1};

  while (!pq.empty()) {
    hist hh = pq.top();
    pq.pop();
    if (hh.dist > mindist[hh.x][hh.y][hh.dir])
      continue;
    rep(i, 0, 4) {
      int xx = hh.x + dx[i], yy = hh.y + dy[i];
      if (!(0 <= xx && xx < h && 0 <= yy && yy < w && mp[xx][yy] == '.'))
        continue;

      pii nd;
      if (hh.dist.second < k && i == hh.dir)
        nd = {hh.dist.first, hh.dist.second + 1};
      else
        nd = {hh.dist.first + 1, 1};

      if (chmin(mindist[xx][yy][i], nd)) {
        pq.push({xx, yy, i, mindist[xx][yy][i]});
      }
    }
  }

  pii ans = {inf, 0};
  rep(i, 0, 4) chmin(ans, mindist[x2][y2][i]);
  die(ans.first == inf ? -1 : ans.first);
}


Error: TLE

Fixed submission:

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

// Inserted snippets: io, root
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
template <typename H> bool chmin(H &v1, const H v2) {
  if (v1 > v2) {
    v1 = v2;
    return true;
  }
  return false;
}
template <typename H> bool chmax(H &v1, const H v2) {
  if (v1 < v2) {
    v1 = v2;
    return true;
  }
  return false;
}
template <typename H> void read(H &head) { cin >> head; }
template <typename H, typename... T> void read(H &head, T &...tail) {
  cin >> head;
  read(tail...);
}
template <typename H> void write(H head) { cout << head << '\\n'; }
template <typename H, typename... T> void write(H head, T... tail) {
  cout << head << " ";
  write(tail...);
}
template <typename... T> void die(T... tok) {
  write(tok...);
  exit(0);
}
// End snippets

int main() {
  cin.tie(0)->sync_with_stdio(0);
  cin.exceptions(cin.failbit);

  int h, w, k;
  read(h, w, k);
  vector<string> mp(h);

  int x1, y1, x2, y2;
  read(x1, y1, x2, y2);
  --x1;
  --y1;
  --x2;
  --y2;

  rep(i, 0, h) read(mp[i]);

  const int inf = 0x3f3f3f3f;
  vector<vector<int>> mindist(h, vi(w, inf));
  mindist[x1][y1] = 0;

  queue<pii> pos;
  pos.push({x1, y1});

  const int dx[] = {-1, 1, 0, 0}, dy[] = {0, 0, -1, 1};

  while (!pos.empty()) {
    int x, y;
    tie(x, y) = pos.front();
    pos.pop();

    rep(i, 0, 4) {
      int xx = x, yy = y;
      xx += dx[i], yy += dy[i];
      // if (x == 0 && y == 2) write("Try", x, y, xx, yy);
      while (0 <= xx && xx < h && 0 <= yy && yy < w && mp[xx][yy] == '.' &&
             mindist[xx][yy] >= mindist[x][y] + 1 &&
             abs(xx - x) + abs(yy - y) <= k) {
        mindist[xx][yy] = mindist[x][y] + 1;
        // write(x, y, xx, yy, mindist[xx][yy]);
        pos.push({xx, yy});
        xx += dx[i], yy += dy[i];
      }
    }
  }

  if (mindist[x2][y2] == inf)
    die(-1);
  else
    die(mindist[x2][y2]);
}