Problem p02644 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; ++i)
#define REPR(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, a, b) for (int i = a; i < b; ++i)
#define FORR(i, a, b) for (int i = b - 1; i >= a; --i)
#define SORT(v) sort(v.begin(), v.end())
#define SORTR(v) sort(v.rbegin(), v.rend())
#define REV(v) reverse(v.begin(), v.end())
#define ITER(itr, v) for (auto itr = v.begin(); itr != v.end(); ++itr)
#define LB(v, x) (lower_bound(v.begin(), v.end(), x) - v.begin())
#define UB(v, x) (upper_bound(v.begin(), v.end(), x) - v.begin())
#define SZ(v) (int)v.size()
using namespace std;
using ll = long long;
using P = pair<int, int>;

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

  int H, W, K, x[2], y[2];
  cin >> H >> W >> K;
  REP(i, 2) {
    cin >> x[i] >> y[i];
    x[i]--;
    y[i]--;
  }
  vector<string> c(H);
  REP(i, H) { cin >> c[i]; }
  vector<vector<int>> cost(H, vector<int>(W, INT_MAX));
  cost[x[0]][y[0]] = 0;
  using T = tuple<int, int, int>;
  priority_queue<T, vector<T>, greater<T>> pq;
  pq.emplace(0, x[0], y[0]);
  int dx[4] = {1, 0, -1, 0};
  int dy[4] = {0, 1, 0, -1};
  while (!pq.empty()) {
    int d, u, v;
    tie(d, u, v) = pq.top();
    pq.pop();
    if (d > cost[u][v])
      continue;
    REP(i, 4) {
      FOR(j, 1, K + 1) {
        int s = u + j * dx[i];
        int t = v + j * dy[i];
        if (s >= 0 && s < H && t >= 0 && t < W) {
          if (c[s][t] == '@')
            break;
          if (d + 1 < cost[s][t]) {
            cost[s][t] = d + 1;
            pq.emplace(cost[s][t], s, t);
          }
        }
      }
    }
  }
  if (cost[x[1]][y[1]] == INT_MAX) {
    cout << -1 << endl;
  } else {
    cout << cost[x[1]][y[1]] << endl;
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; ++i)
#define REPR(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, a, b) for (int i = a; i < b; ++i)
#define FORR(i, a, b) for (int i = b - 1; i >= a; --i)
#define SORT(v) sort(v.begin(), v.end())
#define SORTR(v) sort(v.rbegin(), v.rend())
#define REV(v) reverse(v.begin(), v.end())
#define ITER(itr, v) for (auto itr = v.begin(); itr != v.end(); ++itr)
#define LB(v, x) (lower_bound(v.begin(), v.end(), x) - v.begin())
#define UB(v, x) (upper_bound(v.begin(), v.end(), x) - v.begin())
#define SZ(v) (int)v.size()
using namespace std;
using ll = long long;
using P = pair<int, int>;

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

  int H, W, K, x[2], y[2];
  cin >> H >> W >> K;
  REP(i, 2) {
    cin >> x[i] >> y[i];
    x[i]--;
    y[i]--;
  }
  vector<string> c(H);
  REP(i, H) { cin >> c[i]; }
  vector<vector<int>> cost(H, vector<int>(W, INT_MAX));
  cost[x[0]][y[0]] = 0;
  using T = tuple<int, int, int>;
  priority_queue<T, vector<T>, greater<T>> pq;
  pq.emplace(0, x[0], y[0]);
  int dx[4] = {1, 0, -1, 0};
  int dy[4] = {0, 1, 0, -1};
  while (!pq.empty()) {
    int d, u, v;
    tie(d, u, v) = pq.top();
    pq.pop();
    if (d > cost[u][v])
      continue;
    REP(i, 4) {
      FOR(j, 1, K + 1) {
        int s = u + j * dx[i];
        int t = v + j * dy[i];
        if (s >= 0 && s < H && t >= 0 && t < W) {
          if (c[s][t] == '@' || d + 1 > cost[s][t])
            break;
          if (d + 1 < cost[s][t]) {
            cost[s][t] = d + 1;
            pq.emplace(cost[s][t], s, t);
          }
        }
      }
    }
  }
  if (cost[x[1]][y[1]] == INT_MAX) {
    cout << -1 << endl;
  } else {
    cout << cost[x[1]][y[1]] << endl;
  }

  return 0;
}