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;

#define ll long long
#define ull unsigned long long
#define db long double
#define pb push_back
#define ppb pop_back
#define F first
#define S second
#define mp make_pair
#define all(x) (x).begin(), (x).end()

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef array<int, 2> pii;
typedef vector<int> vi;

typedef tree<pii, null_type, less<pii>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;

int n, m, k;

bool ok(int x, int y) { return x >= 1 && x <= n && y >= 1 && y <= m; }

int main() {
  ios_base::sync_with_stdio(false), cin.tie(NULL);
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
#endif

  cin >> n >> m >> k;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  vector<vector<char>> c(n + 2, vector<char>(m + 2));
  vector<vi> u(n + 2, vi(m + 2));
  vector<vi> d(n + 2, vi(m + 2));
  vector<vi> l(n + 2, vi(m + 2));
  vector<vi> r(n + 2, vi(m + 2));
  vector<vi> dist(n + 2, vi(m + 2));
  vector<vi> used(n + 2, vi(m + 2));
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> c[i][j];
      if (i == n) {
        u[i][j] = -1;
      } else {
        u[i][j] = i + 1;
      }
      if (i == 1) {
        d[i][j] = -1;
      } else {
        d[i][j] = i - 1;
      }
      if (j == m) {
        r[i][j] = -1;
      } else {
        r[i][j] = j + 1;
      }
      if (j == 1) {
        l[i][j] = -1;
      } else {
        l[i][j] = j - 1;
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (c[i][j] != '.') {
        int X = i, Y = j;
        if (u[X][Y] != -1)
          d[u[X][Y]][Y] = -1;
        if (d[X][Y] != -1)
          u[d[X][Y]][Y] = -1;
        if (r[X][Y] != -1)
          l[X][r[X][Y]] = -1;
        if (l[X][Y] != -1)
          r[X][l[X][Y]] = -1;
      }
    }
  }
  queue<pii> q;
  q.push({x1, y1});
  used[x1][y1] = 1;
  dist[x1][y1] = 0;
  if (u[x1][y1] != -1)
    d[u[x1][y1]][y1] = d[x1][y1];
  if (d[x1][y1] != -1)
    u[d[x1][y1]][y1] = u[x1][y1];
  if (r[x1][y1] != -1)
    l[x1][r[x1][y1]] = l[x1][y1];
  if (l[x1][y1] != -1)
    r[x1][l[x1][y1]] = r[x1][y1];
  pii tmp;
  while (!q.empty()) {
    pii v = q.front();
    q.pop();
    int x = v[0], y = v[1], ind = 0;
    int X = x, Y = y;
    while (ind < 4) {
      int X0 = X, Y0 = Y;
      if (ind == 0) {
        X = d[X][Y];
      }
      if (ind == 1) {
        X = u[X][Y];
      }
      if (ind == 2) {
        Y = l[X][Y];
      }
      if (ind == 3) {
        Y = r[X][Y];
      }
      if (!ok(X, Y) || abs(X - x) > k || abs(Y - y) > k) {
        X = x, Y = y;
        ind++;
        continue;
      }
      if (ind == 0) {
        d[X0][Y0] = d[X][Y];
      }
      if (ind == 1) {
        u[X0][Y0] = u[X][Y];
      }
      if (ind == 2) {
        l[X0][Y0] = l[X][Y];
      }
      if (ind == 3) {
        r[X0][Y0] = r[X][Y];
      }
      if (used[X][Y]) {
        continue;
      }
      dist[X][Y] = dist[x][y] + 1;
      used[X][Y] = 1;
      tmp[0] = X, tmp[1] = Y;
      q.push(tmp);
      if (u[X][Y] != -1)
        d[u[X][Y]][Y] = d[X][Y];
      if (d[X][Y] != -1)
        u[d[X][Y]][Y] = u[X][Y];
      if (r[X][Y] != -1)
        l[X][r[X][Y]] = l[X][Y];
      if (l[X][Y] != -1)
        r[X][l[X][Y]] = r[X][Y];
    }
  }
  if (!used[x2][y2]) {
    cout << -1;
  } else {
    cout << dist[x2][y2];
  }
}


Error: TLE

Fixed submission:

#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;

#define ll long long
#define ull unsigned long long
#define db long double
#define pb push_back
#define ppb pop_back
#define F first
#define S second
#define mp make_pair
#define all(x) (x).begin(), (x).end()

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef array<int, 2> pii;
typedef vector<int> vi;

typedef tree<pii, null_type, less<pii>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;

int n, m, k;

bool ok(int x, int y) { return x >= 1 && x <= n && y >= 1 && y <= m; }

int main() {
  ios_base::sync_with_stdio(false), cin.tie(NULL);
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
#endif

  cin >> n >> m >> k;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  vector<vector<char>> c(n + 2, vector<char>(m + 2));
  vector<vi> u(n + 2, vi(m + 2));
  vector<vi> d(n + 2, vi(m + 2));
  vector<vi> l(n + 2, vi(m + 2));
  vector<vi> r(n + 2, vi(m + 2));
  vector<vi> dist(n + 2, vi(m + 2));
  vector<vi> used(n + 2, vi(m + 2));
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> c[i][j];
      if (i == n) {
        u[i][j] = -1;
      } else {
        u[i][j] = i + 1;
      }
      if (i == 1) {
        d[i][j] = -1;
      } else {
        d[i][j] = i - 1;
      }
      if (j == m) {
        r[i][j] = -1;
      } else {
        r[i][j] = j + 1;
      }
      if (j == 1) {
        l[i][j] = -1;
      } else {
        l[i][j] = j - 1;
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (c[i][j] != '.') {
        int X = i, Y = j;
        if (u[X][Y] != -1)
          d[u[X][Y]][Y] = -1;
        if (d[X][Y] != -1)
          u[d[X][Y]][Y] = -1;
        if (r[X][Y] != -1)
          l[X][r[X][Y]] = -1;
        if (l[X][Y] != -1)
          r[X][l[X][Y]] = -1;
      }
    }
  }
  queue<pii> q;
  q.push({x1, y1});
  used[x1][y1] = 1;
  dist[x1][y1] = 0;
  if (u[x1][y1] != -1)
    d[u[x1][y1]][y1] = d[x1][y1];
  if (d[x1][y1] != -1)
    u[d[x1][y1]][y1] = u[x1][y1];
  if (r[x1][y1] != -1)
    l[x1][r[x1][y1]] = l[x1][y1];
  if (l[x1][y1] != -1)
    r[x1][l[x1][y1]] = r[x1][y1];
  pii tmp;
  while (!q.empty()) {
    pii v = q.front();
    q.pop();
    int x = v[0], y = v[1], ind = 0;
    int X = x, Y = y;
    while (ind < 4) {
      int X0 = X, Y0 = Y;
      if (ind == 0) {
        X = d[X][Y];
      }
      if (ind == 1) {
        X = u[X][Y];
      }
      if (ind == 2) {
        Y = l[X][Y];
      }
      if (ind == 3) {
        Y = r[X][Y];
      }
      if (!ok(X, Y) || abs(X - x) > k || abs(Y - y) > k) {
        X = x, Y = y;
        ind++;
        continue;
      }
      // if (ind == 0) {
      // d[X0][Y0] = -1;
      //}
      // if (ind == 1) {
      // u[X0][Y0] = -1;
      //}
      // if (ind == 2) {
      // l[X0][Y0] = -1;
      //}
      // if (ind == 3) {
      // r[X0][Y0] = -1;
      //}
      if (used[X][Y]) {
        continue;
      }
      dist[X][Y] = dist[x][y] + 1;
      used[X][Y] = 1;
      tmp[0] = X, tmp[1] = Y;
      q.push(tmp);
      if (u[X][Y] != -1)
        d[u[X][Y]][Y] = d[X][Y];
      if (d[X][Y] != -1)
        u[d[X][Y]][Y] = u[X][Y];
      if (r[X][Y] != -1)
        l[X][r[X][Y]] = l[X][Y];
      if (l[X][Y] != -1)
        r[X][l[X][Y]] = r[X][Y];
    }
  }
  if (!used[x2][y2]) {
    cout << -1;
  } else {
    cout << dist[x2][y2];
  }
}