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 X first
#define Y second
#define all(x) x.begin(), x.end()
#define ordered_set                                                            \\
  tree<int, null_type, less<int>, rb_tree_tag,                                 \\
       tree_order_statistics_node_update>

#define sim template <class c
#define ris return *this
#define dor > debug &operator<<
#define eni(x)                                                                 \\
  sim > typename enable_if<sizeof dud<c>(0) x 1, debug &>::type operator<<(    \\
            c i) {
sim > struct rge {
  c b, e;
};
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c *x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
  ~debug() { cerr << endl; }
  eni(!=) cerr << boolalpha << i;
  ris;
} eni(==) ris << range(begin(i), end(i));
}
sim, class b dor(pair<b, c> d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
  sim dor(const c &) { ris; }
#endif
}
;
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

const int mod = 998244353;
const int MX = (int)2e5;

void add_self(int &x, int y) {
  x += y;
  if (x >= mod)
    x -= mod;
}
void sub_self(int &x, int y) {
  x -= y;
  if (x < 0)
    x += mod;
}
void mul(int &x, int y) { x = x * 1LL * y % mod; }

int main() {

  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(0);

  int n, m, k;
  cin >> n >> m >> k;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--, y1--, x2--, y2--;

  vector<vector<char>> g(n, vector<char>(m));
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      cin >> g[i][j];
    }
  }

  // up, down, left, right
  const int dx[] = {-1, 1, 0, 0};
  const int dy[] = {0, 0, -1, 1};

  vector<vector<bool>> vis(n, vector<bool>(m));

  vector<vector<int>> best(n, vector<int>(m, 1e9));
  best[x1][y1] = 0;

  queue<array<int, 2>> q;
  q.push({x1, y1});

  while (!q.empty()) {
    auto cur = q.front();
    q.pop();
    int x = cur[0];
    int y = cur[1];

    if (vis[x][y])
      continue;

    vis[x][y] = true;

    for (int dir = 0; dir < 4; ++dir) {
      int cntr = k;
      while (cntr--) {
        int nx = x + (k - cntr) * dx[dir];
        int ny = y + (k - cntr) * dy[dir];
        if (nx >= 0 && nx < n && ny >= 0 && ny < m && g[nx][ny] == '.') {
          if (best[x][y] + 1 <= best[nx][ny] && !vis[nx][ny]) {
            best[nx][ny] = best[x][y] + 1;
            q.push({nx, ny});
          } else
            break;
        } else
          break;
      }
    }
  }

  int res = best[x2][y2];
  if (res == (int)1e9)
    res = -1;
  cout << res << endl;

  return 0;
}


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 X first
#define Y second
#define all(x) x.begin(), x.end()
#define ordered_set                                                            \\
  tree<int, null_type, less<int>, rb_tree_tag,                                 \\
       tree_order_statistics_node_update>

#define sim template <class c
#define ris return *this
#define dor > debug &operator<<
#define eni(x)                                                                 \\
  sim > typename enable_if<sizeof dud<c>(0) x 1, debug &>::type operator<<(    \\
            c i) {
sim > struct rge {
  c b, e;
};
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c *x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
  ~debug() { cerr << endl; }
  eni(!=) cerr << boolalpha << i;
  ris;
} eni(==) ris << range(begin(i), end(i));
}
sim, class b dor(pair<b, c> d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
  sim dor(const c &) { ris; }
#endif
}
;
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

const int mod = 998244353;
const int MX = (int)2e5;

void add_self(int &x, int y) {
  x += y;
  if (x >= mod)
    x -= mod;
}
void sub_self(int &x, int y) {
  x -= y;
  if (x < 0)
    x += mod;
}
void mul(int &x, int y) { x = x * 1LL * y % mod; }

int main() {

  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(0);

  int n, m, k;
  cin >> n >> m >> k;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--, y1--, x2--, y2--;

  vector<vector<char>> g(n, vector<char>(m));
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      cin >> g[i][j];
    }
  }

  // up, down, left, right
  const int dx[] = {-1, 1, 0, 0};
  const int dy[] = {0, 0, -1, 1};

  vector<vector<bool>> vis(n, vector<bool>(m));

  vector<vector<int>> best(n, vector<int>(m, 1e9));
  best[x1][y1] = 0;

  queue<array<int, 2>> q;
  q.push({x1, y1});

  while (!q.empty()) {
    auto cur = q.front();
    q.pop();
    int x = cur[0];
    int y = cur[1];
    vis[x][y] = true;

    for (int dir = 0; dir < 4; ++dir) {
      int cntr = k;
      while (cntr--) {
        int nx = x + (k - cntr) * dx[dir];
        int ny = y + (k - cntr) * dy[dir];
        if (nx >= 0 && nx < n && ny >= 0 && ny < m && g[nx][ny] == '.') {
          if (best[x][y] + 1 <= best[nx][ny] && !vis[nx][ny]) {
            best[nx][ny] = best[x][y] + 1;
            q.push({nx, ny});
          } else
            break;
        } else
          break;
      }
    }
  }

  int res = best[x2][y2];
  if (res == (int)1e9)
    res = -1;
  cout << res << endl;

  return 0;
}