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

// #pragma GCC optimize("Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
// #pragma GCC optimize("unroll-loops")

#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 DEBUG
#ifdef DEBUG
#define debug(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " = " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " = " << arg1 << " || ";
  __f(comma + 1, args...);
}
#else
#define debug(...)
#endif

template <class Ch, class Tr, class Container>
basic_ostream<Ch, Tr> &operator<<(basic_ostream<Ch, Tr> &os,
                                  Container const &x) {
  os << "{ ";
  for (auto &y : x)
    os << y << " ; ";
  return os << "}";
}

template <class X, class Y>
ostream &operator<<(ostream &os, pair<X, Y> const &p) {
  return os << "[ " << p.first << ", " << p.second << "]";
}

#define FAST_IO                                                                \\
  std::ios::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define ll long long int
#define pb push_back
#define mp make_pair
#define ld long double
#define sz(a) (ll)(a).size()
#define endl "\\n"

// priority_queue<ll, vector<ll>, less<ll>>pq;     // Max Heap
// priority_queue<ll, vector<ll>, greater<ll>>pq;  // Min Heap

typedef tree<ll, null_type, less<ll>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
// K-th smallest
// cout << k << "kth (1 based indexing) smallest: " << *A.find_by_order(k - 1)
// << endl; NO OF ELEMENTS < X cout << "No of elements less than " << X << " are
// " << A.order_of_key(X) << endl;

const int MAXN = 1e6 + 5;

ll n, m, k, vis[MAXN], xx, yy, x2, y2, dist[MAXN], done[MAXN];
vector<string> a;

void bfs(ll source) {
  vis[source] = 1;
  dist[source] = 0;
  queue<ll> q;
  q.push(source);
  while (!q.empty()) {
    ll p = q.front();
    q.pop();
    if (done[p] == 1)
      continue;
    done[p] = 1;
    ll c = p % m;
    ll r = p / m;
    for (int i = r + 1; i <= r + k; i++) {
      if (i >= n || a[i][c] == '@')
        break;
      ll val = m * i + c;
      if (dist[val] < dist[p] + 1)
        break;
      q.push(val);
      vis[val] = 1;
      dist[val] = dist[p] + 1;
    }
    for (int i = r - 1; i >= r - k; i--) {
      if (i < 0 || a[i][c] == '@')
        break;
      ll val = m * i + c;
      if (dist[val] < dist[p] + 1)
        break;
      q.push(val);
      vis[val] = 1;
      dist[val] = dist[p] + 1;
    }
    for (int i = c + 1; i <= c + k; i++) {
      if (i >= m || a[r][i] == '@')
        break;
      ll val = m * r + i;
      if (dist[val] < dist[p] + 1)
        break;
      q.push(val);
      vis[val] = 1;
      dist[val] = dist[p] + 1;
    }
    for (int i = c - 1; i >= c - k; i--) {
      if (i < 0 || a[r][i] == '@')
        break;
      ll val = m * r + i;
      if (dist[val] < dist[p] + 1)
        break;
      q.push(val);
      vis[val] = 1;
      dist[val] = dist[p] + 1;
    }
  }
}

int main() {
  FAST_IO;

  cin >> n >> m >> k;
  cin >> xx >> yy >> x2 >> y2;
  xx -= 1;
  yy -= 1;
  x2 -= 1;
  y2 -= 1;
  a.resize(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < MAXN; i++)
    dist[i] = 1e18;
  bfs(m * xx + yy);
  if (dist[m * x2 + y2] == 1e18)
    cout << -1 << endl;
  else
    cout << dist[m * x2 + y2] << endl;
  return 0;
}

Error: TLE

Fixed submission:

// #pragma GCC optimize("Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
// #pragma GCC optimize("unroll-loops")

#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 DEBUG
#ifdef DEBUG
#define debug(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " = " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " = " << arg1 << " || ";
  __f(comma + 1, args...);
}
#else
#define debug(...)
#endif

template <class Ch, class Tr, class Container>
basic_ostream<Ch, Tr> &operator<<(basic_ostream<Ch, Tr> &os,
                                  Container const &x) {
  os << "{ ";
  for (auto &y : x)
    os << y << " ; ";
  return os << "}";
}

template <class X, class Y>
ostream &operator<<(ostream &os, pair<X, Y> const &p) {
  return os << "[ " << p.first << ", " << p.second << "]";
}

#define FAST_IO                                                                \\
  std::ios::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define ll long long int
#define pb push_back
#define mp make_pair
#define ld long double
#define sz(a) (ll)(a).size()
#define endl "\\n"

// priority_queue<ll, vector<ll>, less<ll>>pq;     // Max Heap
// priority_queue<ll, vector<ll>, greater<ll>>pq;  // Min Heap

typedef tree<ll, null_type, less<ll>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
// K-th smallest
// cout << k << "kth (1 based indexing) smallest: " << *A.find_by_order(k - 1)
// << endl; NO OF ELEMENTS < X cout << "No of elements less than " << X << " are
// " << A.order_of_key(X) << endl;

const int MAXN = 1e6 + 5;

ll n, m, k, vis[MAXN], xx, yy, x2, y2, dist[MAXN];
vector<string> a;

void bfs(ll source) {
  vis[source] = 1;
  dist[source] = 0;
  queue<ll> q;
  q.push(source);
  while (!q.empty()) {
    ll p = q.front();
    q.pop();
    ll c = p % m;
    ll r = p / m;
    for (int i = r + 1; i <= r + k; i++) {
      if (i >= n || a[i][c] == '@')
        break;
      ll val = m * i + c;
      if (dist[val] < dist[p] + 1)
        break;
      q.push(val);
      vis[val] = 1;
      dist[val] = dist[p] + 1;
    }
    for (int i = r - 1; i >= r - k; i--) {
      if (i < 0 || a[i][c] == '@')
        break;
      ll val = m * i + c;
      if (dist[val] < dist[p] + 1)
        break;
      q.push(val);
      vis[val] = 1;
      dist[val] = dist[p] + 1;
    }
    for (int i = c + 1; i <= c + k; i++) {
      if (i >= m || a[r][i] == '@')
        break;
      ll val = m * r + i;
      if (dist[val] < dist[p] + 1)
        break;
      q.push(val);
      vis[val] = 1;
      dist[val] = dist[p] + 1;
    }
    for (int i = c - 1; i >= c - k; i--) {
      if (i < 0 || a[r][i] == '@')
        break;
      ll val = m * r + i;
      if (dist[val] < dist[p] + 1)
        break;
      q.push(val);
      vis[val] = 1;
      dist[val] = dist[p] + 1;
    }
  }
}

int main() {
  FAST_IO;

  cin >> n >> m >> k;
  cin >> xx >> yy >> x2 >> y2;
  xx -= 1;
  yy -= 1;
  x2 -= 1;
  y2 -= 1;
  a.resize(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < MAXN; i++)
    dist[i] = 1e18;
  bfs(m * xx + yy);
  if (dist[m * x2 + y2] == 1e18)
    cout << -1 << endl;
  else
    cout << dist[m * x2 + y2] << endl;
  return 0;
}