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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define rep(i, n) for (int i = 0; i < (n); ++i)
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
const ll INF = 1LL << 60;
const int inf = (1 << 30) - 1;
const int mod = 1e9 + 7;
int dx[8] = {1, 0, -1, 0, -1, -1, 1, 1};
int dy[8] = {0, 1, 0, -1, -1, 1, -1, 1};
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll h, w, k;
  cin >> h >> w >> k;
  ll sx, sy, gx, gy;
  cin >> sx >> sy >> gx >> gy;
  sx--, sy--, gx--, gy--;
  char c[h][w];
  rep(i, h) {
    rep(j, w) { cin >> c[i][j]; }
  }
  ll s = sx * w + sy, g = gx * w + gy;
  vector<vector<ll>> d(h * w, vector<ll>(4, INF));
  priority_queue<pair<ll, pair<ll, ll>>, vector<pair<ll, pair<ll, ll>>>,
                 greater<pair<ll, pair<ll, ll>>>>
      pq;
  rep(i, 4) {
    d[s][i] = 0;
    pq.push({0, {s, i}});
  }
  while (!pq.empty()) {
    auto p = pq.top();
    pq.pop();
    ll u = p.first, v = p.second.first, f = p.second.second;
    if (u > d[v][f]) {
      continue;
    }
    rep(i, 4) {
      int x = v / w + dx[i], y = v % w + dy[i];
      ll nx = x * w + y;
      if (x < 0 || x >= h || y < 0 || y >= w || c[x][y] == '@') {
        continue;
        ;
      }
      if (i == f) {
        if (chmin(d[nx][i], d[v][f] + 1)) {
          pq.push({d[nx][i], {nx, i}});
        }
      } else {
        if (chmin(d[nx][i], (d[v][f] + k - 1) / k * k + 1)) {
          pq.push({d[nx][i], {nx, i}});
        }
      }
    }
  }
  ll ans = INF;
  rep(i, 4) { chmin(ans, d[g][i]); }
  cout << (ans == INF ? -1 : (ans + k - 1) / k) << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define rep(i, n) for (int i = 0; i < (n); ++i)
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
const ll INF = 1LL << 60;
const int inf = (1 << 30) - 1;
const int mod = 1e9 + 7;
int dx[8] = {1, 0, -1, 0, -1, -1, 1, 1};
int dy[8] = {0, 1, 0, -1, -1, 1, -1, 1};
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int h, w, k;
  cin >> h >> w >> k;
  int sx, sy, gx, gy;
  cin >> sx >> sy >> gx >> gy;
  sx--, sy--, gx--, gy--;
  char c[h][w];
  rep(i, h) {
    rep(j, w) { cin >> c[i][j]; }
  }
  vector<vector<int>> d(h, vector<int>(w, inf));
  d[sx][sy] = 0;
  stack<pair<int, int>> st;
  st.push({sx, sy});
  while (!st.empty()) {
    int u = st.top().first, v = st.top().second;
    st.pop();
    for (int i = 0; i < 4; i++) {
      for (int j = 1; j <= k; j++) {
        int x = u + dx[i] * j, y = v + dy[i] * j;
        if (x >= 0 && x < h && y >= 0 && y < w && c[x][y] == '.') {
          if (chmin(d[x][y], d[u][v] + 1)) {
            st.push({x, y});
          } else {
            break;
          }
        } else {
          break;
        }
      }
    }
  }
  cout << (d[gx][gy] == inf ? -1 : d[gx][gy]) << endl;
}