Problem p02644 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

#define REP(i, m, n) for (int i = (m); i < (int)(n); i++)
#define RREP(i, m, n) for (int i = (int)(n - 1); i >= m; i--)
#define rep(i, n) REP(i, 0, n)
#define rrep(i, n) RREP(i, 0, n)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define fi first
#define se second
#define debug(...)                                                             \
  {                                                                            \
    cerr << "[L" << __LINE__ << "] ";                                          \
    _debug(__VA_ARGS__);                                                       \
  }

template <typename T> string join(const vector<T> &v, string del = ", ") {
  stringstream s;
  for (auto x : v)
    s << del << x;
  return s.str().substr(del.size());
}
template <typename T> ostream &operator<<(ostream &o, const vector<T> &v) {
  if (v.size())
    o << "[" << join(v) << "]";
  return o;
}
template <typename T>
ostream &operator<<(ostream &o, const vector<vector<T>> &vv) {
  int l = vv.size();
  if (l) {
    o << endl;
    rep(i, l) o << (i == 0 ? "[ " : ",\n  ") << vv[i]
                << (i == l - 1 ? " ]" : "");
  }
  return o;
}
inline void _debug() { cerr << endl; }
template <class First, class... Rest>
void _debug(const First &first, const Rest &...rest) {
  cerr << first << " ";
  _debug(rest...);
}

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;

const double PI = (1 * acos(0.0));
const double EPS = 1e-9;
const int INF = 0x3f3f3f3f;
const ll INFL = 0x3f3f3f3f3f3f3f3fLL;
const ll mod = 1e9 + 7;

inline void finput(string filename) { freopen(filename.c_str(), "r", stdin); }

int di[] = {0, 1, 0, -1};
int dj[] = {1, 0, -1, 0};

int main() {
  ios_base::sync_with_stdio(0);
  // finput("./input");
  int h, w, k;
  cin >> h >> w >> k;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  vector<string> s(h);
  rep(i, h) cin >> s[i];

  vvi d(h, vi(w, INF));
  queue<pii> Q;
  Q.push(pii(x1 - 1, y1 - 1));
  d[x1 - 1][y1 - 1] = 0;
  while (!Q.empty()) {
    int i = Q.front().fi;
    int j = Q.front().se;
    Q.pop();
    rep(l, 4) {
      REP(m, 1, k + 1) {
        int ni = i + di[l] * m;
        int nj = j + dj[l] * m;
        if (ni < 0 || h <= ni)
          continue;
        if (nj < 0 || w <= nj)
          continue;
        if (s[ni][nj] == '@')
          break;
        if (d[ni][nj] <= d[i][j])
          break;
        d[ni][nj] = d[i][j] + 1;
        Q.push(pii(ni, nj));
      }
    }
  }
  if (d[x2 - 1][y2 - 1] == INF)
    cout << -1 << endl;
  else
    cout << d[x2 - 1][y2 - 1] << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define REP(i, m, n) for (int i = (m); i < (int)(n); i++)
#define RREP(i, m, n) for (int i = (int)(n - 1); i >= m; i--)
#define rep(i, n) REP(i, 0, n)
#define rrep(i, n) RREP(i, 0, n)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define fi first
#define se second
#define debug(...)                                                             \
  {                                                                            \
    cerr << "[L" << __LINE__ << "] ";                                          \
    _debug(__VA_ARGS__);                                                       \
  }

template <typename T> string join(const vector<T> &v, string del = ", ") {
  stringstream s;
  for (auto x : v)
    s << del << x;
  return s.str().substr(del.size());
}
template <typename T> ostream &operator<<(ostream &o, const vector<T> &v) {
  if (v.size())
    o << "[" << join(v) << "]";
  return o;
}
template <typename T>
ostream &operator<<(ostream &o, const vector<vector<T>> &vv) {
  int l = vv.size();
  if (l) {
    o << endl;
    rep(i, l) o << (i == 0 ? "[ " : ",\n  ") << vv[i]
                << (i == l - 1 ? " ]" : "");
  }
  return o;
}
inline void _debug() { cerr << endl; }
template <class First, class... Rest>
void _debug(const First &first, const Rest &...rest) {
  cerr << first << " ";
  _debug(rest...);
}

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;

const double PI = (1 * acos(0.0));
const double EPS = 1e-9;
const int INF = 0x3f3f3f3f;
const ll INFL = 0x3f3f3f3f3f3f3f3fLL;
const ll mod = 1e9 + 7;

inline void finput(string filename) { freopen(filename.c_str(), "r", stdin); }

int di[] = {0, 1, 0, -1};
int dj[] = {1, 0, -1, 0};

int main() {
  ios_base::sync_with_stdio(0);
  // finput("./input");
  int h, w, k;
  cin >> h >> w >> k;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  vector<string> s(h);
  rep(i, h) cin >> s[i];

  vvi d(h, vi(w, INF));
  queue<pii> Q;
  Q.push(pii(x1 - 1, y1 - 1));
  d[x1 - 1][y1 - 1] = 0;
  while (!Q.empty()) {
    int i = Q.front().fi;
    int j = Q.front().se;
    Q.pop();
    rep(l, 4) {
      REP(m, 1, k + 1) {
        int ni = i + di[l] * m;
        int nj = j + dj[l] * m;
        if (ni < 0 || h <= ni)
          continue;
        if (nj < 0 || w <= nj)
          continue;
        if (s[ni][nj] == '@')
          break;
        if (d[ni][nj] <= d[i][j])
          break;
        if (d[ni][nj] == INF) {
          d[ni][nj] = d[i][j] + 1;
          Q.push(pii(ni, nj));
        }
      }
    }
  }
  if (d[x2 - 1][y2 - 1] == INF)
    cout << -1 << endl;
  else
    cout << d[x2 - 1][y2 - 1] << endl;
  return 0;
}