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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep_range(i, s, e) for (int i = (s); i < (int)(e); i++)
#define rep_rev(i, s, e) for (int i = (s)-1; i >= (int)(e); i--)
#define sort_vec(v) sort((v).begin(), (v).end())
typedef int64_t lint;
// lint mod = 998244353;
// lint mod = 1000000007;
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
  return a < b && (a = b, true);
}
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
  return a > b && (a = b, true);
}

int app() {
  vector<int> dx = {1, 0, -1, 0};
  vector<int> dy = {0, 1, 0, -1};

  int h, w, k;
  cin >> h >> w >> k;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  y1--;
  x2--;
  y2--;

  vector<vector<bool>> c(h, vector<bool>(w));
  rep(i, h) rep(j, w) {
    char cs;
    cin >> cs;
    c[i][j] = (cs == '.');
  }

  using state = pair<lint, pair<pair<int, int>, int>>;
  priority_queue<state, vector<state>, greater<state>> que;
  vector<vector<vector<lint>>> mincost(
      h, vector<vector<lint>>(w, vector<lint>(4, 9e18)));

  rep(dir, 4) que.emplace(0, make_pair(make_pair(x1, y1), dir));
  mincost[x1][y1] = vector<lint>(4, 0);
  while (!que.empty()) {
    state us = que.top();
    que.pop();
    lint ucost = us.first;
    auto u = us.second;
    int ux = u.first.first;
    int uy = u.first.second;
    int udir = u.second;
    if (ucost > mincost[ux][uy][udir])
      continue;

    rep(dir, 4) {
      if (dir == udir)
        continue;
      lint vcost = (ucost - 1) / k * k + k;
      if (chmin(mincost[ux][uy][dir], vcost)) {
        que.emplace(vcost, make_pair(make_pair(ux, uy), dir));
      }
    }

    int vx = ux + dx[udir];
    int vy = uy + dy[udir];
    if (vx < 0 || vx >= h || vy < 0 || vy >= w)
      continue;
    if (!c[vx][vy])
      continue;
    lint vcost = ucost + 1;
    if (vx == x2 && vy == y2) {
      return (vcost - 1) / k + 1;
    }
    if (chmin(mincost[vx][vy][udir], vcost)) {
      que.emplace(vcost, make_pair(make_pair(vx, vy), udir));
    }
  }

  return -1;
}

int main() {
  cout << app() << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep_range(i, s, e) for (int i = (s); i < (int)(e); i++)
#define rep_rev(i, s, e) for (int i = (s)-1; i >= (int)(e); i--)
#define sort_vec(v) sort((v).begin(), (v).end())
typedef int64_t lint;
// lint mod = 998244353;
// lint mod = 1000000007;
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
  return a < b && (a = b, true);
}
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
  return a > b && (a = b, true);
}

int app() {
  vector<int> dx = {1, 0, -1, 0};
  vector<int> dy = {0, 1, 0, -1};

  int h, w, k;
  cin >> h >> w >> k;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  y1--;
  x2--;
  y2--;

  vector<vector<bool>> c(h, vector<bool>(w));
  rep(i, h) rep(j, w) {
    char cs;
    cin >> cs;
    c[i][j] = (cs == '.');
  }

  using state = pair<int, int>;
  queue<state> q;
  vector<vector<int>> ans(h, vector<int>(w, h * w + 1));
  vector<vector<bool>> res(h, vector<bool>(w, false));
  q.emplace(x1, y1);
  ans[x1][y1] = 0;

  while (!q.empty()) {
    auto p = q.front();
    q.pop();
    int xu = p.first;
    int yu = p.second;
    int d = ans[xu][yu];
    if (res[xu][yu])
      continue;
    res[xu][yu] = true;
    rep(dir, 4) {
      int xn = xu, yn = yu;
      rep(t, k) {
        xn += dx[dir];
        yn += dy[dir];
        if (xn < 0 || xn >= h || yn < 0 || yn >= w)
          break;
        if (!c[xn][yn])
          break;
        if (xn == x2 && yn == y2) {
          return d + 1;
        }
        if (chmin(ans[xn][yn], d + 1)) {
          q.emplace(xn, yn);
        }
      }
    }
  }

  return -1;
}

int main() {
  cout << app() << endl;
  return 0;
}