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

// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
using vint = vector<int>;
using vvint = vector<vint>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vchar = vector<char>;
using vvchar = vector<vchar>;
using ld = long double;
using vld = vector<ld>;
using vvld = vector<vld>;
using vp = vector<P>;
using vpp = vector<pair<P, P>>;
using vvp = vector<vp>;

#define rep(i, n) for (int i = 0; i < n; ++i)
#pragma region Debug
istream &operator>>(istream &is, P &a) { return is >> a.first >> a.second; }
ostream &operator<<(ostream &os, const P &a) {
  return os << "(" << a.first << "," << a.second << ")";
}

template <typename T> void view(const std::vector<T> &v) {
  for (const auto &e : v) {
    std::cout << e << " ";
  }
  std::cout << std::endl;
}
template <typename T> void view(const std::vector<std::vector<T>> &vv) {
  for (const auto &v : vv) {
    view(v);
  }
}
#pragma endregion

#define fst first
#define snd second

const int INF = 1e9;
struct edge {
  int c;
  P p;
  edge(int c, P p) : c(c), p(p) {}
  bool operator>(const edge &r) const { return c > r.c; }
};
int main() {

  int h, w, k, x1, y1, x2, y2;
  cin >> h >> w >> k;
  cin >> y1 >> x1 >> y2 >> x2;
  x1--;
  y1--;
  x2--;
  y2--;
  vvchar g(h, vchar(w));
  rep(y, h) rep(x, w) cin >> g[y][x];
  vvint v(h, vint(w, INF));

  priority_queue<edge, vector<edge>, greater<edge>> q;
  P start = P(y1, x1), goal = P(y2, x2);
  q.push(edge(0, start));
  v[y1][x1] = 0;

  vint dy = {0, 1, 0, -1}, dx = {1, 0, -1, 0};
  while (q.size()) {
    edge e = q.top();
    q.pop();
    P p = e.p;

    if (p == goal) {
      break;
    }

    int now = e.c;
    rep(dir, 4) for (int i = 1; i <= k; i++) {
      int ny = p.fst + dy[dir] * i;
      int nx = p.snd + dx[dir] * i;
      if (ny < 0 || nx < 0 || ny > h - 1 || nx > w - 1 || g[ny][nx] == '@' ||
          v[ny][nx] <= now) {
        break;
      }

      if (v[ny][nx] > now + 1) {
        v[ny][nx] = now + 1;
        q.push(edge(now + 1, P(ny, nx)));
      }
    }
  }

  ld ans = ceil(v[y2][x2]);
  if (ans == INF) {
    ans = -1;
  }
  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
using vint = vector<int>;
using vvint = vector<vint>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vchar = vector<char>;
using vvchar = vector<vchar>;
using vp = vector<P>;
using vpp = vector<pair<P, P>>;
using vvp = vector<vp>;

#define rep(i, n) for (int i = 0; i < n; ++i)
#pragma region Debug
istream &operator>>(istream &is, P &a) { return is >> a.first >> a.second; }
ostream &operator<<(ostream &os, const P &a) {
  return os << "(" << a.first << "," << a.second << ")";
}

template <typename T>

void view(const std::vector<T> &v) {
#ifndef ONLINE_JUDGE
  for (const auto &e : v) {
    std::cout << e << " ";
  }
  std::cout << std::endl;
#endif
}
template <typename T> void view(const std::vector<std::vector<T>> &vv) {
  for (const auto &v : vv) {
    view(v);
  }
}
#pragma endregion

const int INF = 1e8;

struct edge {
  int c;
  P p;
  edge(P p, int c) : p(p), c(c) {}
  bool operator>(const edge &r) const { return c > r.c; }
};

int main() {
  int h, w, k;
  cin >> h >> w >> k;
  int x1, y1, x2, y2;
  cin >> y1 >> x1 >> y2 >> x2;
  x1--;
  y1--;
  x2--;
  y2--;

  vvchar g(h, vchar(w));
  rep(y, h) rep(x, w) cin >> g[y][x];
  priority_queue<edge, vector<edge>, greater<edge>> q;
  q.emplace(edge(P(y1, x1), 0));
  vector<int> dir = {1, 0, -1, 0, 1};
  vvint d(h, vint(w, INF));
  d[y1][x1] = 0;

  while (q.size()) {
    edge e = q.top();
    P p = e.p;
    q.pop();
    int y = p.first, x = p.second;
    if (p == P(y2, x2))
      break;
    int now = d[y][x];
    rep(i, 4) {
      int dx = dir[i], dy = dir[i + 1];
      for (int i = 1; i <= k; ++i) {

        int nx = x + dx * i, ny = y + dy * i;
        if (ny < 0 || ny >= h || nx < 0 || nx >= w || g[ny][nx] == '@')
          break;

        if (d[ny][nx] > now + 1) {
          d[ny][nx] = now + 1;
          q.push(edge(P(ny, nx), now + 1));
        }
      }
    }
  }

  if (d[y2][x2] == INF) {
    cout << -1 << endl;
  } else {
    cout << d[y2][x2] << endl;
  }

  return 0;
}