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

#include "bits/stdc++.h"
using namespace std;
// #define int long long
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b - 1); i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n - 1); i >= 0; i--)
#define REP1(i, n) for (int i = 1; i <= (n); i++)
#define RREP1(i, n) for (int i = (n); i >= 1; i--)
#define ALL(a) (a).begin(), (a).end()
#define UNIQUE_SORT(l)                                                         \\
  sort(ALL(l));                                                                \\
  l.erase(unique(ALL(l)), l.end());
#define CONTAIN(a, b) find(ALL(a), (b)) != (a).end()
#define out(...) printf(__VA_ARGS__)
#define chmax(a, b) a = max(a, b)
#define chmin(a, b) a = min(a, b)
#if DEBUG
#define debug(...) // printf(__VA_ARGS__)
#else
#define debug(...) /* ... */
#endif

void solve();
signed main() {
#if DEBUG
  std::ifstream in("input.txt");
  std::cin.rdbuf(in.rdbuf());
#endif
  cin.tie(0);
  ios::sync_with_stdio(false);
  solve();
  return 0;
}

/*================================*/
#if DEBUG
#define SIZE 100
#else
#define SIZE 1234560
#endif

int H, W, K;
int X[2], Y[2];
string M[SIZE];

struct Item {
  int h, w;
  int score;
  bool operator<(const Item &that) const { return score > that.score; }
};
int dxy[] = {1, 0, -1, 0, 1};
int DH = 1, DW = 2;
void solve() {
  cin >> H >> W >> K;
  REP(i, 2) {
    cin >> X[i] >> Y[i];
    X[i]--;
    Y[i]--;
  }
  REP(h, H) cin >> M[h];
  vector<vector<int>> MAP(H, vector<int>(W, INT_MAX / 2));
  vector<vector<int>> DIST(H, vector<int>(W, 0));
  priority_queue<Item> que;
  que.push({X[0], Y[0], 0});
  MAP[X[0]][Y[0]] = 0;
  while (que.size()) {
    auto item = que.top();
    que.pop();
    REP(i, 4) {
      int dh = dxy[i], dw = dxy[i + 1];
      REP1(j, K) {
        int nh = item.h + dh * j, nw = item.w + dw * j;
        if (nh < 0 || nw < 0 || nh >= H || nw >= W)
          break;
        if (M[nh][nw] == '@')
          break;
        int v = MAP[item.h][item.w] + 1;
        int dist = (dh) ? dh * DH : dw * DW;
        if (MAP[nh][nw] > v) {
          debug("%lld,%lld->%lld,%lld\\n", item.h, item.w, nh, nw);
          MAP[nh][nw] = v;
          DIST[nh][nw] = dist;
          if (nh == X[1] && nw == Y[1]) {
            cout << v << endl;
            return;
          }
          que.push({nh, nw, v});
        } else if (v > MAP[nh][nw])
          break;
      }
    }
  }
  cout << -1 << endl;
}


Error: TLE

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
// #define int long long
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b - 1); i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n - 1); i >= 0; i--)
#define REP1(i, n) for (int i = 1; i <= (n); i++)
#define RREP1(i, n) for (int i = (n); i >= 1; i--)
#define ALL(a) (a).begin(), (a).end()
#define UNIQUE_SORT(l)                                                         \\
  sort(ALL(l));                                                                \\
  l.erase(unique(ALL(l)), l.end());
#define CONTAIN(a, b) find(ALL(a), (b)) != (a).end()
#define out(...) printf(__VA_ARGS__)
#define chmax(a, b) a = max(a, b)
#define chmin(a, b) a = min(a, b)
#if DEBUG
#define debug(...) // printf(__VA_ARGS__)
#else
#define debug(...) /* ... */
#endif

void solve();
signed main() {
#if DEBUG
  std::ifstream in("input.txt");
  std::cin.rdbuf(in.rdbuf());
#endif
  cin.tie(0);
  ios::sync_with_stdio(false);
  solve();
  return 0;
}

/*================================*/
#if DEBUG
#define SIZE 100
#else
#define SIZE 1234560
#endif

int H, W, K;
int X[2], Y[2];
string M[SIZE];

struct Item {
  int h, w;
  int score;
  bool operator<(const Item &that) const { return score > that.score; }
};
int dxy[] = {1, 0, -1, 0, 1};
int check(int k) {
  vector<vector<int>> MAP(H, vector<int>(W, INT_MAX / 2));
  priority_queue<Item> que;
  que.push({X[0], Y[0], 0});
  MAP[X[0]][Y[0]] = 0;
  while (que.size()) {
    auto item = que.top();
    que.pop();
    REP(i, 4) {
      int dh = dxy[i], dw = dxy[i + 1];
      REP1(j, k) {
        int nh = item.h + dh * j, nw = item.w + dw * j;
        if (nh < 0 || nw < 0 || nh >= H || nw >= W)
          break;
        if (M[nh][nw] == '@')
          break;
        int v = MAP[item.h][item.w] + 1;
        if (MAP[nh][nw] > v) {
          debug("%lld,%lld->%lld,%lld\\n", item.h, item.w, nh, nw);
          MAP[nh][nw] = v;
          if (nh == X[1] && nw == Y[1]) {
            return v;
          }
          que.push({nh, nw, v});
        }
      }
    }
  }
  return -1;
}
void solve() {
  cin >> H >> W >> K;
  RREP(i, 2) {
    cin >> X[i] >> Y[i];
    X[i]--;
    Y[i]--;
  }
  REP(h, H) cin >> M[h];
  if (check(1) == -1) {
    cout << -1 << endl;
    return;
  }
  cout << check(K) << endl;
}