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

#include <bits/stdc++.h>
using namespace std;
using Int = long long;
int PREP = (cin.tie(nullptr), ios::sync_with_stdio(false),
            cout << fixed << setprecision(9), 0);
// int SEGV = getenv("D") || (exit(system("D= SEGFAULT_SIGNALS=all catchsegv
// ./prog.exe") >> 8), 0);
template <class T> using MinHeap = priority_queue<T, vector<T>, greater<T>>;
const int DX[4] = {0, 0, +1, -1};
const int DY[4] = {+1, -1, 0, 0};
const int TYPE[4] = {1, 2, 4, 8};
int main() {
  int H, W, K;
  cin >> H >> W >> K;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--, y1--, x2--, y2--;
  vector<string> C(H);
  for (auto &c : C)
    cin >> c;
  vector<vector<int>> done(H, vector<int>(W));
  vector<vector<int>> cost(H, vector<int>(W, 1e9));
  MinHeap<tuple<int, int, int>> Q;
  Q.emplace(0, x1, y1);
  cost[x1][y1] = 0;
  while (!Q.empty()) {
    auto [c, x, y] = Q.top();
    Q.pop();
    if (cost[x][y] != c)
      continue;
    if (x == x2 && y == y2) {
      cout << c << '\\n';
      return 0;
    }
    for (int i = 0; i < 4; i++) {
      if ((done[x][y] & TYPE[i]) != 0)
        continue;
      int nx = x, ny = y, rest = K;
      while (rest) {
        nx += DX[i], ny += DY[i], rest--;
        if (!(0 <= nx && nx < H && 0 <= ny && ny < W))
          break;
        if (C[nx][ny] != '.')
          break;
        if (cost[nx][ny] < c)
          break;
        if (cost[nx][ny] == c && (done[nx][ny] & TYPE[i]) == 0) {
          done[nx][ny] |= TYPE[i];
          rest = K;
        }
        if (cost[nx][ny] > c + 1) {
          cost[nx][ny] = c + 1;
          Q.emplace(c + 1, nx, ny);
        }
      }
      done[x][y] |= TYPE[i];
    }
  }
  cout << -1 << '\\n';
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using Int = long long;
int PREP = (cin.tie(nullptr), ios::sync_with_stdio(false),
            cout << fixed << setprecision(9), 0);
// int SEGV = getenv("D") || (exit(system("D= SEGFAULT_SIGNALS=all catchsegv
// ./prog.exe") >> 8), 0);
template <class T> using MinHeap = priority_queue<T, vector<T>, greater<T>>;
const int DX[4] = {0, 0, +1, -1};
const int DY[4] = {+1, -1, 0, 0};
const int TYPE[4] = {1, 2, 4, 8};
int main() {
  int H, W, K;
  cin >> H >> W >> K;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--, y1--, x2--, y2--;
  vector<string> C(H);
  for (auto &c : C)
    cin >> c;
  vector<vector<int>> done(H, vector<int>(W));
  vector<vector<int>> cost(H, vector<int>(W, 1e9));
  MinHeap<tuple<int, int, int>> Q;
  Q.emplace(0, x1, y1);
  cost[x1][y1] = 0;
  while (!Q.empty()) {
    auto [c, x, y] = Q.top();
    Q.pop();
    if (cost[x][y] != c)
      continue;
    if (x == x2 && y == y2) {
      cout << c << '\\n';
      return 0;
    }
    for (int i = 0; i < 4; i++) {
      if ((done[x][y] & TYPE[i]) != 0)
        continue;
      int nx = x, ny = y, rest = K;
      while (rest) {
        nx += DX[i], ny += DY[i], rest--;
        if (!(0 <= nx && nx < H && 0 <= ny && ny < W))
          break;
        if (C[nx][ny] != '.')
          break;
        if (cost[nx][ny] == c && (done[nx][ny] & TYPE[i]) == 0) {
          done[nx][ny] |= TYPE[i];
          rest = K;
        }
        if (cost[nx][ny] > c + 1) {
          cost[nx][ny] = c + 1;
          Q.emplace(c + 1, nx, ny);
        }
      }
      done[x][y] |= TYPE[i];
    }
  }
  cout << -1 << '\\n';
  return 0;
}