Problem p02644 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(a, n) for (ll a = 0; a < (n); ++a)
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<vector<ll>> Graph;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const ll INF = 1e18;

ll h, w, k;
ll X1, Y1, X2, Y2;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> h >> w >> k;
  cin >> X1 >> Y1 >> X2 >> Y2;
  X1--;
  Y1--;
  X2--;
  Y2--;
  vector<vector<char>> field(h + 1, vector<char>(w + 1));
  vector<vector<ll>> dist(h + 1, vector<ll>(w + 1));
  rep(i, h) {
    rep(j, w) {
      cin >> field[i][j];
      dist[i][j] = -1;
    }
  }
  queue<pair<ll, ll>> que;
  dist[X1][Y1] = 0;
  int dx[4] = {1, 0, -1, 0};
  int dy[4] = {0, 1, 0, -1};
  que.push({X1, Y1});
  while (!que.empty()) {
    ll xnow = que.front().first;
    ll ynow = que.front().second;
    que.pop();
    rep(dir, 4) {
      rep(i, k) { // 進めるマスは1~k
        ll xnx = xnow + dx[dir] * (i + 1);
        ll ynx = ynow + dy[dir] * (i + 1);
        if (xnx < 0 || xnx >= h)
          break;
        if (ynx < 0 || ynx >= w)
          break;
        if (field[xnx][ynx] == '@') {
          break;
        }
        if (dist[xnx][ynx] != -1 && (dist[xnx][ynx] <= dist[xnow][ynow]))
          break;
        dist[xnx][ynx] = dist[xnow][ynow] + 1;
        que.push({xnx, ynx});
      }
    }
  }
  cout << dist[X2][Y2] << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(a, n) for (ll a = 0; a < (n); ++a)
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<vector<ll>> Graph;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const ll INF = 1e18;

ll h, w, k;
ll X1, Y1, X2, Y2;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> h >> w >> k;
  cin >> X1 >> Y1 >> X2 >> Y2;
  X1--;
  Y1--;
  X2--;
  Y2--;
  vector<vector<char>> field(h + 1, vector<char>(w + 1));
  vector<vector<ll>> dist(h + 1, vector<ll>(w + 1));
  rep(i, h) {
    rep(j, w) {
      cin >> field[i][j];
      dist[i][j] = -1;
    }
  }
  queue<pair<ll, ll>> que;
  dist[X1][Y1] = 0;
  int dx[4] = {1, 0, -1, 0};
  int dy[4] = {0, 1, 0, -1};
  que.push({X1, Y1});
  while (!que.empty()) {
    ll xnow = que.front().first;
    ll ynow = que.front().second;
    que.pop();
    rep(dir, 4) {
      rep(i, k) { // 進めるマスは1~k
        ll xnx = xnow + dx[dir] * (i + 1);
        ll ynx = ynow + dy[dir] * (i + 1);
        if (xnx < 0 || xnx >= h)
          break;
        if (ynx < 0 || ynx >= w)
          break;
        if (field[xnx][ynx] == '@') {
          break;
        }
        if (dist[xnx][ynx] != -1 && (dist[xnx][ynx] <= dist[xnow][ynow]))
          break;
        if (dist[xnx][ynx] != -1)
          continue;
        dist[xnx][ynx] = dist[xnow][ynow] + 1;
        que.push({xnx, ynx});
      }
    }
  }
  cout << dist[X2][Y2] << endl;
  return 0;
}