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

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

#define INF 102030405060708090

using P = pair<long long, tuple<int, int, int>>;

int main() {

  ios::sync_with_stdio(false);
  cin.tie(0);
  int h, w, k;
  cin >> h >> w >> k;
  int sh, sw, gh, gw;
  cin >> sh >> sw >> gh >> gw;
  sh--;
  sw--;
  gh--;
  gw--;
  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];
    }
  }
  vector<vector<vector<long long>>> mins(
      h, vector<vector<long long>>(w, vector<long long>(4, INF)));
  priority_queue<P, vector<P>, greater<P>> pq;
  vector<int> dh = {0, 1, 0, -1};
  vector<int> dw = {1, 0, -1, 0};
  for (int i = 0; i < 4; i++) {
    mins[sh][sw][i] = 0;
    pq.push(make_pair(0, make_tuple(sh, sw, i)));
  }
  while (!pq.empty()) {
    P nowp = pq.top();
    pq.pop();
    tuple<int, int, int> pos = nowp.second;
    long long dist = nowp.first;
    int nh = get<0>(pos);
    int nw = get<1>(pos);
    int dir = get<2>(pos);
    if (mins[nh][nw][dir] < dist) {
      continue;
    }
    for (int i = 0; i < 4; i++) {
      if (i == dir) {
        int toh = nh + dh[i];
        int tow = nw + dw[i];
        if (toh < 0 || toh >= h || tow < 0 || tow >= w) {
          continue;
        }
        if (c[toh][tow] == '@')
          continue;
        if (mins[toh][tow][dir] > mins[nh][nw][dir] + 1) {
          mins[toh][tow][dir] = mins[nh][nw][dir] + 1;
          pq.push(make_pair(mins[nh][nw][dir] + 1, make_tuple(toh, tow, dir)));
        }
      } else {
        long long kdis = mins[nh][nw][dir];
        kdis = (kdis + k - 1) / k * k;
        if (mins[nh][nw][i] > kdis) {
          mins[nh][nw][i] = kdis;
          pq.push(make_pair(kdis, make_tuple(nh, nw, i)));
        }
      }
    }
  }
  long long ans = INF;
  for (int i = 0; i < 4; i++) {
    ans = min(ans, mins[gh][gw][i]);
  }
  if (ans == INF)
    cout << -1 << endl;
  else
    cout << (ans + k - 1) / k << endl;
}

Error: TLE

Fixed submission:

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

// macro
#define rep(i, n) for (i = 0; i < n; i++)
#define ll long long
#define all(v) v.begin(), v.end()

// code starts
#define INF 1020304050
typedef pair<int, int> P;

int main() {
  int h, w, k;
  cin >> h >> w >> k;
  int sx, sy, gx, gy;
  cin >> sx >> sy >> gx >> gy;
  vector<vector<char>> c(h + 2, vector<char>(w + 2));
  int i, j;
  rep(i, h + 2) rep(j, w + 2) {
    if (i == 0 || i == h + 1 || j == 0 || j == w + 1) {
      c[i][j] = '@';
    } else {
      cin >> c[i][j];
    }
  }
  vector<vector<bool>> visited(h + 2, vector<bool>(w + 2, false));
  vector<vector<int>> dist(h + 2, vector<int>(w + 2, INF));
  dist[sx][sy] = 0;
  queue<P> q;
  queue<P> q2;
  q.push(make_pair(sx, sy));
  visited[sx][sy] = true;
  vector<int> dx = {1, 0, -1, 0};
  vector<int> dy = {0, 1, 0, -1};
  while (!q.empty()) {
    while (!q.empty()) {
      P nowp = q.front();
      q.pop();
      int nx = nowp.first;
      int ny = nowp.second;
      rep(i, 4) {
        for (j = 1; j <= k; j++) {
          int tox = nx + dx[i] * j;
          int toy = ny + dy[i] * j;
          if (c[tox][toy] == '@') {
            break;
          } else if (visited[tox][toy]) {
            continue;
          }
          visited[tox][toy] = true;
          dist[tox][toy] = dist[nx][ny] + 1;
          q2.push(make_pair(tox, toy));
        }
      }
    }
    while (!q2.empty()) {
      q.push(q2.front());
      q2.pop();
    }
  }
  if (dist[gx][gy] == INF)
    cout << -1 << "\\n";
  else
    cout << dist[gx][gy] << "\\n";
}