Problem p02644 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define REP1(i, a, b) for (int i = a; i <= (int)(b); i++)
#define ALL(x) begin(x), end(x)
#define PB push_back
using namespace std;
typedef int64_t LL;
typedef vector<int> VI;
typedef pair<int, int> PII;

template <class T> inline bool chmax(T &a, const T &b) {
  return b > a ? a = b, true : false;
}
template <class T> inline bool chmin(T &a, const T &b) {
  return b < a ? a = b, true : false;
}
template <class T> using MaxHeap = priority_queue<T>;
template <class T> using MinHeap = priority_queue<T, vector<T>, greater<T>>;
template <class T, class F = less<T>> void sort_uniq(vector<T> &v, F f = F()) {
  sort(begin(v), end(v), f);
  v.resize(unique(begin(v), end(v)) - begin(v));
}

const int INF = 1e9;
int dx[4] = {1, 0, -1, 0}; // D,R,U,L;
int dy[4] = {0, 1, 0, -1};
int h, w, k;

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> h >> w >> k;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  --x1;
  --y1;
  --x2;
  --y2;
  vector<string> G(h);
  REP(i, h) cin >> G[i];
  vector<VI> d(h, VI(w, INF));
  d[x1][y1] = 0;
  queue<PII> q;
  q.emplace(x1, y1);
  while (q.size()) {
    int x = q.front().first;
    int y = q.front().second;
    q.pop();
    REP(i, 4) {
      int nx = x;
      int ny = y;
      REP(_, k) {
        nx = nx + dx[i];
        ny = ny + dy[i];
        if (nx < 0 || nx >= h || ny < 0 || ny >= w || G[nx][ny] == '@')
          break;
        if (d[nx][ny] > d[x][y] + 1) {
          d[nx][ny] = d[x][y] + 1;
          q.emplace(nx, ny);
        }
      }
    }
  }
  cout << (d[x2][y2] < INF ? d[x2][y2] : -1) << '\n';
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define REP1(i, a, b) for (int i = a; i <= (int)(b); i++)
#define ALL(x) begin(x), end(x)
#define PB push_back
using namespace std;
typedef int64_t LL;
typedef vector<int> VI;
typedef pair<int, int> PII;

template <class T> inline bool chmax(T &a, const T &b) {
  return b > a ? a = b, true : false;
}
template <class T> inline bool chmin(T &a, const T &b) {
  return b < a ? a = b, true : false;
}
template <class T> using MaxHeap = priority_queue<T>;
template <class T> using MinHeap = priority_queue<T, vector<T>, greater<T>>;
template <class T, class F = less<T>> void sort_uniq(vector<T> &v, F f = F()) {
  sort(begin(v), end(v), f);
  v.resize(unique(begin(v), end(v)) - begin(v));
}

const int INF = 1e9;
int dx[4] = {1, 0, -1, 0}; // D,R,U,L;
int dy[4] = {0, 1, 0, -1};
int h, w, k;

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> h >> w >> k;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  --x1;
  --y1;
  --x2;
  --y2;
  vector<string> G(h);
  REP(i, h) cin >> G[i];
  vector<VI> d(h, VI(w, INF));
  d[x1][y1] = 0;
  queue<PII> q;
  q.emplace(x1, y1);
  while (q.size()) {
    int x = q.front().first;
    int y = q.front().second;
    q.pop();
    REP(i, 4) {
      int nx = x;
      int ny = y;
      REP(_, k) {
        nx = nx + dx[i];
        ny = ny + dy[i];
        if (nx < 0 || nx >= h || ny < 0 || ny >= w || G[nx][ny] == '@')
          break;
        if (d[nx][ny] <= d[x][y])
          break;
        if (d[nx][ny] > d[x][y] + 1) {
          d[nx][ny] = d[x][y] + 1;
          q.emplace(nx, ny);
        }
      }
    }
  }
  cout << (d[x2][y2] < INF ? d[x2][y2] : -1) << '\n';
  return 0;
}