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

#include <bits/stdc++.h>
using namespace std;
// using lint = long long;
using lint = int;
template <typename T>
vector<T> dijkstra(int s, vector<vector<pair<int, T>>> &G) {
  const T inf = numeric_limits<T>::max();
  using P = pair<T, int>;
  int n = G.size();
  vector<T> d(n, inf);
  priority_queue<P, vector<P>, greater<P>> q;
  d[s] = 0;
  q.emplace(d[s], s);
  while (!q.empty()) {
    P p = q.top();
    q.pop();
    int v = p.second;
    if (d[v] < p.first)
      continue;
    for (auto &e : G[v]) {
      int u = e.first;
      T c = e.second;
      if (d[u] > d[v] + c) {
        d[u] = d[v] + c;
        q.emplace(d[u], u);
      }
    }
  }
  return d;
}

using ulint = unsigned long long;
struct RandomNumberGenerator {
  mt19937_64 mt;
  RandomNumberGenerator()
      : mt(chrono::steady_clock::now().time_since_epoch().count()) {}
  ulint operator()(ulint a, ulint b) { // [a, b)
    uniform_int_distribution<ulint> dist(a, b - 1);
    return dist(mt);
  }
  ulint operator()(ulint b) { // [0, b)
    return (*this)(0, b);
  }
  ulint operator()() { return mt(); }
};

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  lint h, w, k;
  cin >> h >> w >> k;
  lint x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--, y1--, x2--, y2--;
  vector<vector<char>> c(h, vector<char>(w));
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      cin >> c[i][j];
    }
  }
  lint n = h * w;
  const int dx[4] = {-1, 1, 0, 0};
  const int dy[4] = {0, 0, -1, 1};
  auto inside = [&](int y, int x) {
    return 0 <= y && y < h && 0 <= x && x < w;
  };
  auto idx = [&](int r, int c) { return r * w + c; };
  const lint inf = numeric_limits<lint>::max();
  using P = pair<lint, int>;
  vector<lint> d(n, inf);
  priority_queue<P, vector<P>, greater<P>> q;
  d[idx(x1, y1)] = 0;
  q.emplace(d[idx(x1, y1)], idx(x1, y1));
  RandomNumberGenerator r;
  int rand = r(2);
  while (!q.empty()) {
    P p = q.top();
    q.pop();
    int v = p.second;
    if (d[v] < p.first)
      continue;
    int cx = v / w;
    int cy = v % w;
    for (int l = 0; l < 4; ++l) {
      int ny = cy;
      int nx = cx;
      for (int m = 0; m < k; ++m) {
        ny += dy[l];
        nx += dx[l];
        if (!inside(nx, ny))
          break;
        if (c[nx][ny] == '@')
          break;
        if (d[idx(nx, ny)] > d[v] + 1) {
          d[idx(nx, ny)] = d[v] + 1;
          if (nx == x2 && ny == y2) {
            cout << d[idx(nx, ny)] << "\\n";
            return 0;
          }
          q.emplace(d[idx(nx, ny)], idx(nx, ny));
        } else {
          if (rand % 2 == 0)
            break;
        }
      }
    }
  }
  if (d[idx(x2, y2)] == inf) {
    cout << -1 << "\\n";
  } else {
    cout << d[idx(x2, y2)] << "\\n";
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
// using lint = long long;
using lint = int;
template <typename T>
vector<T> dijkstra(int s, vector<vector<pair<int, T>>> &G) {
  const T inf = numeric_limits<T>::max();
  using P = pair<T, int>;
  int n = G.size();
  vector<T> d(n, inf);
  priority_queue<P, vector<P>, greater<P>> q;
  d[s] = 0;
  q.emplace(d[s], s);
  while (!q.empty()) {
    P p = q.top();
    q.pop();
    int v = p.second;
    if (d[v] < p.first)
      continue;
    for (auto &e : G[v]) {
      int u = e.first;
      T c = e.second;
      if (d[u] > d[v] + c) {
        d[u] = d[v] + c;
        q.emplace(d[u], u);
      }
    }
  }
  return d;
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  lint h, w, k;
  cin >> h >> w >> k;
  lint x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--, y1--, x2--, y2--;
  vector<vector<char>> c(h, vector<char>(w));
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      cin >> c[i][j];
    }
  }
  lint n = h * w;
  const int dx[4] = {-1, 1, 0, 0};
  const int dy[4] = {0, 0, -1, 1};
  auto inside = [&](int y, int x) {
    return 0 <= y && y < h && 0 <= x && x < w;
  };
  auto idx = [&](int r, int c) { return r * w + c; };
  const lint inf = numeric_limits<lint>::max();
  using P = pair<lint, int>;
  vector<lint> d(n, inf);
  priority_queue<P, vector<P>, greater<P>> q;
  d[idx(x1, y1)] = 0;
  q.emplace(d[idx(x1, y1)], idx(x1, y1));
  int r = rand();
  while (!q.empty()) {
    P p = q.top();
    q.pop();
    int v = p.second;
    if (d[v] < p.first)
      continue;
    int cx = v / w;
    int cy = v % w;
    for (int l = 0; l < 4; ++l) {
      int ny = cy;
      int nx = cx;
      for (int m = 0; m < k; ++m) {
        ny += dy[l];
        nx += dx[l];
        if (!inside(nx, ny))
          break;
        if (c[nx][ny] == '@')
          break;
        if (d[idx(nx, ny)] > d[v] + 1) {
          d[idx(nx, ny)] = d[v] + 1;
          if (nx == x2 && ny == y2) {
            cout << d[idx(nx, ny)] << "\\n";
            return 0;
          }
          q.emplace(d[idx(nx, ny)], idx(nx, ny));
        } else {
          if (r % 2 == 0)
            break;
        }
      }
    }
  }
  if (d[idx(x2, y2)] == inf) {
    cout << -1 << "\\n";
  } else {
    cout << d[idx(x2, y2)] << "\\n";
  }
  return 0;
}