Problem p02644 (C++)

Buggy submission (status: Time Limit Exceeded):
//                                               KEEP GRINDING
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    new_data_set;

#define fastio                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);

#define int long long
#define ld long double
#define MOD 1000000007
#define mod 998244353
#define precise(i) fixed << setprecision(10) << i
#define f(i, a, b) for (int i = a; i < b; ++i)

#define endl '\n'
#define debug cout << "\n========================================\n" << endl;
#define err1(a) cout << #a << " " << a << endl;
#define err2(a, b) cout << #a << " " << a << " " << #b << " " << b << endl;
#define err3(a, b, c)                                                          \
  cout << #a << " " << a << " " << #b << " " << b << " " << #c << " " << c     \
       << endl;
#define err4(a, b, c, d)                                                       \
  cout << #a << " " << a << " " << #b << " " << b << " " << #c << " " << c     \
       << " " << #d << " " << d << endl;

#define all(a) a.begin(), a.end()
#define show(a)                                                                \
  for (auto xyz : a)                                                           \
    cout << xyz << " ";                                                        \
  cout << endl;
#define PQ priority_queue
#define mx(a) *max_element(a)
#define mn(a) *min_element(a)
#define LB                                                                     \
  lower_bound // THIS GIVES THE ITR TO THE ELEMENT IN RANGE [SI,EI) THAT IS >=
              // OUR VALUE
#define UB                                                                     \
  upper_bound // THIS GIVES THE ITR TO THE ELEMENT IN RANGE [SI,EI) THAT IS >
              // OUR VALUE

#define fr first
#define sc second

#define MAXN 500000
#define INF 9223372036854775807
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int h, w, k;
bool in(int &i, int &j) { return (i >= 1 && j >= 1 && i <= h && j <= w); }
signed main() {
  fastio cin >> h >> w >> k;
  int si, sj, ei, ej;
  cin >> si >> sj >> ei >> ej;
  char a[h + 1][w + 1];
  int d[h + 1][w + 1];
  f(i, 1, h + 1) f(j, 1, w + 1) {
    cin >> a[i][j];
    d[i][j] = INF;
  }
  d[si][sj] = 0;
  PQ<pair<int, pair<int, int>>> pq;

  f(i, 1, h + 1) f(j, 1, w + 1) if (a[i][j] != '@')
      pq.push({(-1) * d[i][j], {i, j}});

  while (pq.size()) {
    auto ft = pq.top();
    pq.pop();
    int i = ft.sc.fr, j = ft.sc.sc;
    if (d[i][j] == INF) {
      puts("-1");
      return 0;
    }
    if (i == ei && j == ej) {
      cout << d[ei][ej];
      return 0;
    }
    f(dir, 0, 4) {
      int ni = i + dx[dir], nj = j + dy[dir], cnt = 0;
      while (in(ni, nj) && cnt < k && a[ni][nj] == '.') {
        if (d[ni][nj] > 1 + d[i][j]) {
          d[ni][nj] = 1 + d[i][j];
          pq.push({(-1) * d[ni][nj], {ni, nj}});
        }
        ni += dx[dir], nj += dy[dir], cnt++;
      }
    }
  }
}

Fixed submission:
//                                               KEEP GRINDING
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    new_data_set;

#define fastio                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);

#define int long long
#define ld long double
#define MOD 1000000007
#define mod 998244353
#define precise(i) fixed << setprecision(10) << i
#define f(i, a, b) for (int i = a; i < b; ++i)

#define endl '\n'
#define debug cout << "\n========================================\n" << endl;
#define err1(a) cout << #a << " " << a << endl;
#define err2(a, b) cout << #a << " " << a << " " << #b << " " << b << endl;
#define err3(a, b, c)                                                          \
  cout << #a << " " << a << " " << #b << " " << b << " " << #c << " " << c     \
       << endl;
#define err4(a, b, c, d)                                                       \
  cout << #a << " " << a << " " << #b << " " << b << " " << #c << " " << c     \
       << " " << #d << " " << d << endl;

#define all(a) a.begin(), a.end()
#define show(a)                                                                \
  for (auto xyz : a)                                                           \
    cout << xyz << " ";                                                        \
  cout << endl;
#define PQ priority_queue
#define mx(a) *max_element(a)
#define mn(a) *min_element(a)
#define LB                                                                     \
  lower_bound // THIS GIVES THE ITR TO THE ELEMENT IN RANGE [SI,EI) THAT IS >=
              // OUR VALUE
#define UB                                                                     \
  upper_bound // THIS GIVES THE ITR TO THE ELEMENT IN RANGE [SI,EI) THAT IS >
              // OUR VALUE

#define fr first
#define sc second

#define MAXN 500000
#define INF 9223372036854775807
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int h, w, k;
bool in(int &i, int &j) { return (i >= 1 && j >= 1 && i <= h && j <= w); }
signed main() {
  fastio cin >> h >> w >> k;
  int si, sj, ei, ej;
  cin >> si >> sj >> ei >> ej;
  char a[h + 1][w + 1];
  int d[h + 1][w + 1];
  f(i, 1, h + 1) f(j, 1, w + 1) {
    cin >> a[i][j];
    d[i][j] = INF;
  }
  d[si][sj] = 0;
  PQ<pair<int, pair<int, int>>> pq;

  f(i, 1, h + 1) f(j, 1, w + 1) if (a[i][j] != '@')
      pq.push({(-1) * d[i][j], {i, j}});

  while (pq.size()) {
    auto ft = pq.top();
    pq.pop();
    int i = ft.sc.fr, j = ft.sc.sc;
    if (d[i][j] == INF) {
      puts("-1");
      return 0;
    }
    if (i == ei && j == ej) {
      cout << d[ei][ej];
      return 0;
    }
    f(dir, 0, 4) {
      int ni = i + dx[dir], nj = j + dy[dir], cnt = 0;
      while (in(ni, nj) && cnt < k && a[ni][nj] == '.') {
        if (d[ni][nj] > 1 + d[i][j]) {
          d[ni][nj] = 1 + d[i][j];
          pq.push({(-1) * d[ni][nj], {ni, nj}});
        } else if (d[ni][nj] < 1 + d[i][j]) {
          break;
        }
        ni += dx[dir], nj += dy[dir], cnt++;
      }
    }
  }
}