Problem p02644 (C++) — buggy submission, verdict: Time Limit Exceeded

#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 long long ll;
#define orderd_set                                                             \\
  tree<pair<int, int>, null_type, greater<pair<int, int>>, rb_tree_tag,        \\
       tree_order_statistics_node_update>
// find_by_order(k)
// order_of_key(k)
#define FIO                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);
const int N = 2e3 + 69;
#define ALL(x) begin(x), end(x)
#define sz(x) (ll) x.size()
const int OO = 0x3f3f3f3f;
const int mod = 1000000007;

int n, m, k, xs, ys, xt, yt;
string grid[N];
vector<vector<ll>> dis;
int mvx[] = {1, -1, 0, 0};
int mvy[] = {0, 0, 1, -1};

bool valid(int x, int y) {
  return x < n && x >= 0 && y < m && y >= 0 && grid[x][y] == '.';
}

void bfs(int sx, int sy) {
  queue<pair<int, int>> q;
  q.push({sx, sy});
  dis[sx][sy] = 0;
  while (sz(q)) {
    int ux = q.front().first;
    int uy = q.front().second;
    q.pop();
    if (ux == xt && uy == yt)
      return;
    for (int i = 0; i < 4; i++) {
      for (int j = 1; j <= k; j++) {
        int vx = ux + mvx[i] * j;
        int vy = uy + mvy[i] * j;
        if (valid(vx, vy)) {
          if (dis[vx][vy] == OO) {
            dis[vx][vy] = dis[ux][uy] + 1;
            q.push({vx, vy});
          } else if (dis[vx][vy] >= dis[ux][uy] + 1) {
            dis[vx][vy] = dis[ux][uy] + 1;
          } else
            break;
        } else
          break;
      }
    }
  }
}

int main() {
  FIO cin >> n >> m >> k >> xs >> ys >> xt >> yt;
  dis = vector<vector<ll>>(n + 69, vector<ll>(m + 69, OO));
  for (int i = 0; i < n; i++) {
    cin >> grid[i];
  }
  xs--;
  xt--;
  yt--;
  ys--;
  bfs(xs, ys);
  //    for(int i = 0; i< n; i++)
  //    {
  //        for(int j = 0; j< m; j++)
  //        {
  //            cout << dis[i][j] << "          ";
  //        }
  //        cout << "\\n";
  //    }
  if (dis[xt][yt] == OO)
    cout << -1 << "\\n";
  else
    cout << dis[xt][yt] << "\\n";
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define FIO                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);
using namespace std;
typedef long long ll;

#define row second.first
#define col second.second
#define cost first.first
#define dir first.second

int const N = 2e5 + 5, M = 1e3 + 5, MOD = 1e9 + 7, OO = 0x3f3f3f3f;

int n, m, k, X1, Y1, X2, Y2;
vector<vector<int>> dis;
string grid[N];

int dr[] = {0, 0, 1, -1}; // r-l-d-u
int dc[] = {1, -1, 0, 0};

bool valid(int r, int c) {
  return r >= 0 && r < n && c >= 0 && c < m && grid[r][c] == '.';
}

void dijkstra(int sr, int sc) {
  int ur, uc, vr, vc, c, prv;
  priority_queue<pair<pair<int, int>, pair<int, int>>,
                 vector<pair<pair<int, int>, pair<int, int>>>,
                 greater<pair<pair<int, int>, pair<int, int>>>>
      q;
  dis[sr][sc] = 0;
  q.push({{0, -1}, {sr, sc}});
  while (!q.empty()) {
    ur = q.top().row;
    uc = q.top().col;
    c = q.top().cost;
    prv = q.top().dir;
    q.pop();
    if (ur == X2 - 1 && uc == Y2 - 1)
      return;
    if (dis[ur][uc] != c)
      continue;
    for (int i = 0; i < 4; i++) {
      if (i == prv)
        continue;
      for (int j = 1; j <= max(n, m); j++) {
        vr = ur + (j * dr[i]);
        vc = uc + (j * dc[i]);
        if (valid(vr, vc)) {
          if (dis[vr][vc] > dis[ur][uc] + ((j + k - 1) / k)) {
            dis[vr][vc] = dis[ur][uc] + ((j + k - 1) / k);
            q.push({{dis[vr][vc], i}, {vr, vc}});
          }
        } else
          break;
      }
    }
  }
}

int main() {
  FIO
          //    freopen("input.txt","rt",stdin);
          //    freopen("output.txt","wt",stdout);
          cin >>
      n >> m >> k >> X1 >> Y1 >> X2 >> Y2;
  for (int i = 0; i < n; i++)
    cin >> grid[i];
  dis = vector<vector<int>>(n + 1, vector<int>(m + 1, OO));
  dijkstra(X1 - 1, Y1 - 1);
  if (dis[X2 - 1][Y2 - 1] == OO)
    cout << "-1\\n";
  else
    cout << dis[X2 - 1][Y2 - 1] << "\\n";
}