Problem p02644 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
using namespace std;

typedef long long ll;
typedef uint64_t ull;
typedef pair<int, int> P;
constexpr double EPS = 1e-9;
constexpr int INF = 1001001001;
constexpr int mod = 1000000007;
// constexpr int mod = 998244353;

template <class T> inline bool chmax(T &x, T y) {
  if (x < y) {
    x = y;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &x, T y) {
  if (x > y) {
    x = y;
    return true;
  }
  return false;
}

constexpr int dx[4] = {1, 0, -1, 0};
constexpr int dy[4] = {0, 1, 0, -1};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int h, w, k, sx, sy, tx, ty;
  cin >> h >> w >> k >> sx >> sy >> tx >> ty;
  --sx, --sy, --tx, --ty;
  // cerr << sx << ' ' << sy << ' ' << tx << ' ' << ty << endl;
  vector<string> s(h);
  for (int i = 0; i < h; ++i)
    cin >> s[i];

  vector<vector<int>> dist(h, vector<int>(w, INF));
  dist[sx][sy] = 0;
  using T = tuple<int, int, int>;
  priority_queue<T, vector<T>, greater<T>> que;
  que.push(T(0, sx, sy));
  while (!que.empty()) {
    T tup = que.top();
    que.pop();
    int d, x, y;
    tie(d, x, y) = tup;
    if (d > dist[x][y])
      continue;
    for (int i = 0; i < 4; ++i) {
      int nx = x, ny = y;
      for (int j = 0; j < k; ++j) {
        nx += dx[i], ny += dy[i];
        if (nx < 0 || nx >= h || ny < 0 || ny >= w)
          break;
        if (s[nx][ny] == '@')
          break;
        if (chmin(dist[nx][ny], dist[x][y] + 1)) {
          que.push(T(dist[nx][ny], nx, ny));
        }
      }
    }
  }
  // for(int i = 0; i < h; ++i){
  //     for(int j = 0; j < w; ++j){
  //         if(dist[i][j] == INF)   cerr << -1 << ' ';
  //         else    cerr << dist[i][j] << ' ';
  //     }
  //     cerr  << endl;
  // }
  int ans = dist[tx][ty];
  if (ans == INF)
    ans = -1;
  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
using namespace std;

typedef long long ll;
typedef uint64_t ull;
typedef pair<int, int> P;
constexpr double EPS = 1e-9;
constexpr int INF = 1001001001;
constexpr int mod = 1000000007;
// constexpr int mod = 998244353;

template <class T> inline bool chmax(T &x, T y) {
  if (x < y) {
    x = y;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &x, T y) {
  if (x > y) {
    x = y;
    return true;
  }
  return false;
}

constexpr int dx[4] = {1, 0, -1, 0};
constexpr int dy[4] = {0, 1, 0, -1};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int h, w, k, sx, sy, tx, ty;
  cin >> h >> w >> k >> sx >> sy >> tx >> ty;
  --sx, --sy, --tx, --ty;
  // cerr << sx << ' ' << sy << ' ' << tx << ' ' << ty << endl;
  vector<string> s(h);
  for (int i = 0; i < h; ++i)
    cin >> s[i];

  vector<vector<int>> dist(h, vector<int>(w, INF));
  dist[sx][sy] = 0;
  using T = tuple<int, int, int>;
  priority_queue<T, vector<T>, greater<T>> que;
  que.push(T(0, sx, sy));
  while (!que.empty()) {
    T tup = que.top();
    que.pop();
    int d, x, y;
    tie(d, x, y) = tup;
    if (d > dist[x][y])
      continue;
    for (int i = 0; i < 4; ++i) {
      int nx = x, ny = y;
      for (int j = 0; j < k; ++j) {
        nx += dx[i], ny += dy[i];
        if (nx < 0 || nx >= h || ny < 0 || ny >= w)
          break;
        if (s[nx][ny] == '@')
          break;
        if (dist[nx][ny] < dist[x][y] + 1)
          break;
        if (chmin(dist[nx][ny], dist[x][y] + 1)) {
          que.push(T(dist[nx][ny], nx, ny));
        }
      }
    }
  }
  // for(int i = 0; i < h; ++i){
  //     for(int j = 0; j < w; ++j){
  //         if(dist[i][j] == INF)   cerr << -1 << ' ';
  //         else    cerr << dist[i][j] << ' ';
  //     }
  //     cerr  << endl;
  // }
  int ans = dist[tx][ty];
  if (ans == INF)
    ans = -1;
  cout << ans << endl;
}