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

#include <algorithm>
#include <complex>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define REP(i, m, n) for (int i = int(m); i < int(n); i++)
#define RREP(i, m, n) for (int i = int(n) - 1; i >= int(m); --i)
#define EACH(i, c) for (auto &(i) : c)
#define all(c) begin(c), end(c)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort(begin(c), end(c))
#define pb emplace_back
#define MP make_pair
#define SZ(a) int((a).size())
// #define int long long

#ifdef LOCAL
#define DEBUG(s) cout << (s) << endl
#define dump(x) cerr << #x << " = " << (x) << endl
#define BR cout << endl;
#else
#define DEBUG(s)                                                               \\
  do {                                                                         \\
  } while (0)
#define dump(x)                                                                \\
  do {                                                                         \\
  } while (0)
#define BR
#endif
using namespace std;

using UI = unsigned int;
using UL = unsigned long;
using LL = long long;
using ULL = unsigned long long;
using VI = vector<int>;
using VVI = vector<VI>;
using VLL = vector<LL>;
using VVLL = vector<VLL>;
using VS = vector<string>;
using PII = pair<int, int>;
using VP = vector<PII>;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

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

void solve() {
  LL h, w, k;
  cin >> h >> w >> k;
  VLL x(2), y(2);
  REP(i, 0, 2) cin >> x[i] >> y[i];
  REP(i, 0, 2) {
    --x[i];
    --y[i];
  }
  VS a(h);
  REP(i, 0, h) cin >> a[i];
  priority_queue<pair<int, PII>, vector<pair<int, PII>>,
                 greater<pair<int, PII>>>
      q;
  q.emplace(0, MP(x[0], y[0]));
  VVI d(h, VI(w, 1 << 30));
  while (!q.empty()) {
    auto p = q.top();
    q.pop();
    int dis = p.first, px = p.second.first, py = p.second.second;
    if (d[px][py] < dis)
      continue;
    REP(i, 0, 4) {
      REP(j, 1, k + 1) {
        int nx = px + dx[i] * j;
        int ny = py + dy[i] * j;
        if (nx < 0 || nx >= h || ny < 0 || ny >= w)
          break;
        if (a[nx][ny] == '@')
          break;
        if (d[nx][ny] < dis + 1)
          break;
        if (d[nx][ny] <= dis + 1)
          continue;
        d[nx][ny] = dis + 1;
        q.emplace(dis + 1, MP(nx, ny));
      }
    }
  }
  if (d[x[1]][y[1]] == 1 << 30)
    cout << -1 << endl;
  else
    cout << d[x[1]][y[1]] << endl;
}

signed main() {
  solve();

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <complex>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define REP(i, m, n) for (int i = int(m); i < int(n); i++)
#define RREP(i, m, n) for (int i = int(n) - 1; i >= int(m); --i)
#define EACH(i, c) for (auto &(i) : c)
#define all(c) begin(c), end(c)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort(begin(c), end(c))
#define pb emplace_back
#define MP make_pair
#define SZ(a) int((a).size())
// #define int long long

#ifdef LOCAL
#define DEBUG(s) cout << (s) << endl
#define dump(x) cerr << #x << " = " << (x) << endl
#define BR cout << endl;
#else
#define DEBUG(s)                                                               \\
  do {                                                                         \\
  } while (0)
#define dump(x)                                                                \\
  do {                                                                         \\
  } while (0)
#define BR
#endif
using namespace std;

using UI = unsigned int;
using UL = unsigned long;
using LL = long long;
using ULL = unsigned long long;
using VI = vector<int>;
using VVI = vector<VI>;
using VLL = vector<LL>;
using VVLL = vector<VLL>;
using VS = vector<string>;
using PII = pair<int, int>;
using VP = vector<PII>;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

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

void solve() {
  LL h, w, k;
  cin >> h >> w >> k;
  VLL x(2), y(2);
  REP(i, 0, 2) cin >> x[i] >> y[i];
  REP(i, 0, 2) {
    --x[i];
    --y[i];
  }
  VS a(h);
  REP(i, 0, h) cin >> a[i];
  priority_queue<pair<int, PII>, vector<pair<int, PII>>,
                 greater<pair<int, PII>>>
      q;
  q.emplace(0, MP(x[0], y[0]));
  VVI d(h, VI(w, 1 << 30));
  while (!q.empty()) {
    auto p = q.top();
    q.pop();
    int dis = p.first, px = p.second.first, py = p.second.second;
    if (d[px][py] < dis)
      continue;
    REP(i, 0, 4) {
      REP(j, 1, k + 1) {
        int nx = px + dx[i] * j;
        int ny = py + dy[i] * j;
        if (nx < 0 || nx >= h || ny < 0 || ny >= w)
          break;
        if (a[nx][ny] == '@')
          break;
        if (d[nx][ny] <= dis + 1)
          continue;
        d[nx][ny] = dis + 1;
        q.emplace(dis + 1, MP(nx, ny));
      }
    }
  }
  if (d[x[1]][y[1]] == 1 << 30)
    cout << -1 << endl;
  else
    cout << d[x[1]][y[1]] << endl;
}

signed main() {
  solve();

  return 0;
}