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

/*
    Author : DemonStar
*/

#pragma GCC optimize("Ofast")
#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 ordered_set                                                            \\
  tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>
#define fast                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define all(x) x.begin(), x.end()
#define loop(i, n) for (ll i = 0; i < n; i++)
#define pl pair<ll, ll>
#define vl vector<ll>
#define endl '\\n'
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\\'' << x << '\\''; }
void __print(const char *x) { cerr << '\\"' << x << '\\"'; }
void __print(const string &x) { cerr << '\\"' << x << '\\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V> void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T> void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x)
    cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cerr << ", ";
  _print(v...);
}

#ifdef LOCAL
#define debug(x...)                                                            \\
  cerr << "[" << #x << "] = [";                                                \\
  _print(x)
#else
#define debug(x...)
#endif

struct custom_hash {
  static uint64_t splitmix64(uint64_t x) {
    x += 0x9e3779b97f4a7c15;
    x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
    x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
    return x ^ (x >> 31);
  }
  size_t operator()(uint64_t x) const {
    static const uint64_t FIXED_RANDOM =
        chrono::steady_clock::now().time_since_epoch().count();
    return splitmix64(x + FIXED_RANDOM);
  }
};

template <typename T, typename TT>
ostream &operator<<(ostream &os, const pair<T, TT> &t) {
  return os << t.first << " " << t.second;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &t) {
  for (auto &i : t)
    os << i << " ";
  return os;
}
template <typename T> istream &operator>>(istream &is, vector<T> &v) {
  for (T &t : v)
    is >> t;
  return is;
}
template <typename T1, typename T2>
istream &operator>>(istream &is, vector<pair<T1, T2>> &v) {
  for (pair<T1, T2> &t : v)
    is >> t.first >> t.second;
  return is;
}

const ll mod = 1e9 + 7;

int main() {

#ifdef LOCAL
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  fast;
  ll n, m, k;
  cin >> n >> m >> k;
  ll a, b, x, y;
  cin >> a >> b >> x >> y;
  a--, b--, x--, y--;
  vector<vector<char>> s(n, vector<char>(m));
  vector<set<ll>> clr(n), rlc(m);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> s[i][j];
      clr[i].insert(j);
      rlc[j].insert(i);
    }
  }
  vector<vector<int>> dis(n, vector<int>(m, -1));
  queue<pl> q;
  q.push({a, b});
  dis[a][b] = 0;
  clr[a].erase(b);
  rlc[b].erase(a);
  int i, j;
  while (!q.empty()) {
    tie(i, j) = q.front();
    q.pop();
    debug(i, j);
    auto it = clr[i].lower_bound(j);
    while (it != clr[i].end() and (*it) - j <= k and s[i][*it] != '@') {
      dis[i][*it] = dis[i][j] + 1;
      q.push({i, *it});
      rlc[*it].erase(i);
      it++;
      clr[i].erase(prev(it));
    }
    // debug(1, clr[i]);
    it = clr[i].lower_bound(j);
    while (it != clr[i].begin() and j - (*prev(it)) <= k and
           s[i][*prev(it)] != '@') {
      dis[i][*prev(it)] = dis[i][j] + 1;
      q.push({i, *prev(it)});
      rlc[*prev(it)].erase(i);
      clr[i].erase(prev(it));
    }
    // debug(2, clr[i]);
    it = rlc[j].lower_bound(i);
    while (it != rlc[j].end() and (*it) - i <= k and s[*it][j] != '@') {
      dis[*it][j] = dis[i][j] + 1;
      q.push({*it, j});
      clr[*it].erase(j);
      it++;
      rlc[j].erase(prev(it));
    }
    // debug(3, rlc[j]);
    it = rlc[j].lower_bound(i);
    while (it != rlc[j].begin() and i - (*prev(it)) <= k and
           s[*prev(it)][j] != '@') {
      dis[*prev(it)][j] = dis[i][j] + 1;
      q.push({*prev(it), j});
      clr[*prev(it)].erase(j);
      rlc[j].erase(prev(it));
    }
    // debug(4, rlc[j]);
  }

  cout << dis[x][y] << endl;

#ifdef LOCAL
  cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s";
#endif

  return 0;
}

Error: TLE

Fixed submission:

/*
    Author : DemonStar
*/

#pragma GCC optimize("Ofast")
#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 ordered_set                                                            \\
  tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>
#define fast                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define all(x) x.begin(), x.end()
#define loop(i, n) for (ll i = 0; i < n; i++)
#define pl pair<ll, ll>
#define vl vector<ll>
#define endl '\\n'
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\\'' << x << '\\''; }
void __print(const char *x) { cerr << '\\"' << x << '\\"'; }
void __print(const string &x) { cerr << '\\"' << x << '\\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V> void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T> void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x)
    cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cerr << ", ";
  _print(v...);
}

#ifdef LOCAL
#define debug(x...)                                                            \\
  cerr << "[" << #x << "] = [";                                                \\
  _print(x)
#else
#define debug(x...)
#endif

struct custom_hash {
  static uint64_t splitmix64(uint64_t x) {
    x += 0x9e3779b97f4a7c15;
    x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
    x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
    return x ^ (x >> 31);
  }
  size_t operator()(uint64_t x) const {
    static const uint64_t FIXED_RANDOM =
        chrono::steady_clock::now().time_since_epoch().count();
    return splitmix64(x + FIXED_RANDOM);
  }
};

template <typename T, typename TT>
ostream &operator<<(ostream &os, const pair<T, TT> &t) {
  return os << t.first << " " << t.second;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &t) {
  for (auto &i : t)
    os << i << " ";
  return os;
}
template <typename T> istream &operator>>(istream &is, vector<T> &v) {
  for (T &t : v)
    is >> t;
  return is;
}
template <typename T1, typename T2>
istream &operator>>(istream &is, vector<pair<T1, T2>> &v) {
  for (pair<T1, T2> &t : v)
    is >> t.first >> t.second;
  return is;
}

const ll mod = 1e9 + 7;

int main() {

#ifdef LOCAL
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  fast;
  ll h, w, k;
  cin >> h >> w >> k;
  int a, b, x, y;
  cin >> a >> b >> x >> y;
  a--, b--, x--, y--;
  if (a == x and b == y) {
    cout << 0 << endl;
    return 0;
  }
  vector<string> s(h);
  cin >> s;
  vector<vector<int>> vis(h, vector<int>(w, mod));
  queue<vector<int>> q;
  q.push({0, a, b});
  vis[a][b] = 0;
  int dis, i, j;
  vector<int> tmp;
  while (!q.empty()) {
    tmp = q.front();
    dis = tmp[0], i = tmp[1], j = tmp[2];
    q.pop();
    debug(tmp);
    if (i == x and j == y) {
      cout << dis << endl;
      goto skip;
    }
    int d = 1;
    while (d <= k and i + d < h and s[i + d][j] != '@' and
           vis[i + d][j] > dis) {
      q.push({dis + 1, i + d, j});
      vis[i + d][j] = dis + 1;
      debug(i + d, j);
      d++;
    }
    d = 1;
    while (d <= k and i - d >= 0 and s[i - d][j] != '@' and
           vis[i - d][j] > dis) {
      q.push({dis + 1, i - d, j});
      vis[i - d][j] = dis + 1;
      debug(i - d, j);
      d++;
    }
    d = 1;
    while (d <= k and j + d < w and s[i][j + d] != '@' and
           vis[i][j + d] > dis) {
      q.push({dis + 1, i, j + d});
      vis[i][j + d] = dis + 1;
      debug(i, j + d);
      d++;
    }
    d = 1;
    while (d <= k and j - d >= 0 and s[i][j - d] != '@' and
           vis[i][j - d] > dis) {
      q.push({dis + 1, i, j - d});
      vis[i][j - d] = dis + 1;
      debug(i, j - d);
      d++;
    }
  }
  cout << -1 << endl;
skip:;

#ifdef LOCAL
  cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s";
#endif

  return 0;
}