Problem p02644 (C++)

Buggy submission (status: Time Limit Exceeded):
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define mp make_pair
#define owo                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);
#define MOD (ll)(1e6 + 3)
#define INF (ll)(1e18)
#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
#define time__(d)                                                              \
  for (long blockTime = 0;                                                     \
       (blockTime == 0 ? (blockTime = clock()) != 0 : false);                  \
       debug("%s time : %.4fs\n", d,                                           \
             (double)(clock() - blockTime) / CLOCKS_PER_SEC))
typedef long long int ll;
typedef long double ld;
typedef pair<ll, ll> PII;
typedef pair<int, int> pii;
typedef vector<vector<int>> vii;
typedef vector<vector<ll>> VII;
ll dx[4] = {0, 1, 0, -1};
ll dy[4] = {1, 0, -1, 0};
int main() {
  ll h, w, k;
  cin >> h >> w >> k;
  ll x1, x2, y1, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  x2--;
  y1--;
  y2--;
  vector<string> grid(h);
  VII d(h + 1, vector<ll>(w + 1, INF));
  for (int i = 0; i < h; i++)
    cin >> grid[i];
  auto ok = [&](ll x, ll y) {
    return (x >= 0 && y >= 0 && x < h && y < w && grid[x][y] == '.');
  };
  queue<pii> q;
  d[x1][y1] = 0;
  q.push({x1, y1});
  while (!q.empty()) {
    ll x, y;
    x = q.front().fi;
    y = q.front().se;
    q.pop();
    if (x == x2 && y == y2) {
      cout << d[x2][y2];
      return 0;
    }
    for (int dir = 0; dir < 4; dir++) {
      ll xn = x;
      ll yn = y;
      for (int i = 0; i < k; i++) {
        xn += dx[dir];
        yn += dy[dir];
        if (!ok(xn, yn))
          break;
        if (d[xn][yn] > d[x][y] + 1) {
          d[xn][yn] = d[x][y] + 1;
          q.push({xn, yn});
        }
      }
    }
    // cout<<x<<" "<<y<<endl;
  }
  cout << -1;
}


Fixed submission:
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define mp make_pair
#define owo                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);
#define MOD (ll)(1e6 + 3)
#define INF (ll)(1e18)
#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
#define time__(d)                                                              \
  for (long blockTime = 0;                                                     \
       (blockTime == 0 ? (blockTime = clock()) != 0 : false);                  \
       debug("%s time : %.4fs\n", d,                                           \
             (double)(clock() - blockTime) / CLOCKS_PER_SEC))
typedef long long int ll;
typedef long double ld;
typedef pair<ll, ll> PII;
typedef pair<int, int> pii;
typedef vector<vector<int>> vii;
typedef vector<vector<ll>> VII;
ll dx[4] = {0, 1, 0, -1};
ll dy[4] = {1, 0, -1, 0};
int main() {
  ll h, w, k;
  cin >> h >> w >> k;
  ll x1, x2, y1, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  x2--;
  y1--;
  y2--;
  vector<string> grid(h);
  VII d(h + 1, vector<ll>(w + 1, INF));
  for (int i = 0; i < h; i++)
    cin >> grid[i];
  auto ok = [&](ll x, ll y) {
    return (x >= 0 && y >= 0 && x < h && y < w && grid[x][y] == '.');
  };
  queue<pii> q;
  d[x1][y1] = 0;
  q.push({x1, y1});
  while (!q.empty()) {
    ll x, y;
    x = q.front().fi;
    y = q.front().se;
    q.pop();
    if (x == x2 && y == y2) {
      cout << d[x2][y2];
      return 0;
    }
    for (int dir = 0; dir < 4; dir++) {
      ll xn = x;
      ll yn = y;
      for (int i = 0; i < k; i++) {
        xn += dx[dir];
        yn += dy[dir];
        if (!ok(xn, yn))
          break;
        if (d[xn][yn] <= d[x][y])
          break;
        if (d[xn][yn] > d[x][y] + 1) {
          d[xn][yn] = d[x][y] + 1;
          q.push({xn, yn});
        }
      }
    }
    // cout<<x<<" "<<y<<endl;
  }
  cout << -1;
}