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

/** BISMILLAH, AR-RAHMAN, AR-RAHIM **
*** Giving up is the only sure way to fail ***

    Md. Mujahedul Azad (Turin)
    FB : FB/Md.turin01
    GH : GITHUB/turin1602101
    Hajee Mohammad Danesh Science & Technology University( H S T U ) */

#include <bits/stdc++.h>
using namespace std;

#define IL inline
#define ST template <int M>
#define TP template <class T>
#define UP template <class T, class U>
#define HP template <class H, class... T>

#define sq(x) ((x) * (x))
#define cb(x) ((x)*sq(x))
#define abs(x) ((x) < 0 ? -(x) : (x))
#define max(a, b) ((a) < (b) ? (b) : (a))
#define min(a, b) ((a) < (b) ? (a) : (b))

#define gp ' '
#define nl '\\n'
#define sc scanf
#define ff first
#define se second
#define pf printf
#define pc putchar
#define G(a) get<(a)>
#define gc getchar()
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define lb lower_bound
#define ub upper_bound
#define eb emplace_back
#define amax(a, b) a = max(a, b)
#define amin(a, b) a = min(a, b)
#define rint register int
#define FLUSH fflush(stdout)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define Clr(x) memset(x, 0, sizeof(x))
#define Set(x) memset(x, -1, sizeof(x))
#define fr(i, a, b) for (rint i = a; i < b; i++)
#define rf(i, a, b) for (rint i = b; i >= a; i--)
#define precision(x) cout << fixed << setprecision(x)
#define fileIO(name)                                                           \\
  freopen(name ".in", "r", stdin);                                             \\
  freopen(name ".out", "w", stdout)

TP IL bool read(T &);
IL bool read(char &);
IL bool read(char *);
HP IL bool read(H &, T &...);
TP IL void write(T);
IL void write(char);
IL void write(char *);
IL void write(const char *);
HP IL void write(H, T...);
TP IL T gcd(T, T);
TP IL T lcm(T, T);
TP IL T power(T, T);
TP IL T MulL(T, T, T);
TP IL T power(T, T, T);
TP IL T powerL(T, T, T);
TP IL void in(T *, size_t);
TP IL void in(vector<T> &, size_t);
TP IL void out(T *, size_t);
TP IL void out(vector<T> &, size_t);
const int dx[] = {1, -1, 0, 0, -1, 1, -1, 1},
          dy[] = {0, 0, -1, 1, 1, 1, -1, -1},
          kx[] = {-2, -1, 1, 2, 2, 1, -1, -2},
          ky[] = {1, 2, 2, 1, -1, -2, -2, -1};
const int MODF = 998244353, MOD7 = 1000000007, MOD9 = 1000000009,
          INF = 0x3f3f3f3f;
const long double EPS = 1e-9, PI = acos(-1.0);
using tpl = tuple<int, int, int>;
using ll = long long;
using ld = long double;
using vl = vector<ll>;
using vi = vector<int>;
using ii = pair<int, int>;
using iii = pair<int, ii>;
IL int rand(int, int);
IL ll randLL(ll, ll);
IL void inRand(int *, int, int, int);
void inRandLL(ll *, int, ll, ll);
IL void inRand(vi &, int, int, int);
IL void inRandLL(vl &, int, ll, ll);
ST struct Calculator {
  IL ll Inv(ll);
  IL ll Add(ll, ll);
  IL ll Sub(ll, ll);
  IL ll Mul(ll, ll);
  IL ll Div(ll, ll);
};

Calculator<MOD7> C;
const int mx = 1e6 + 10;

int n, m, k;
int sx, sy, ex, ey;

vector<vi> dis;
vector<string> s;

IL void MAIN(int &cs) {
  //    write("Case ", cs, ": ");
  read(n, m, k, sx, sy, ex, ey);
  sx--;
  sy--;
  ex--;
  ey--;

  s.resize(n);
  dis.assign(n, vi(m, INF));

  fr(i, 0, n) cin >> s[i];
  queue<ii> q;
  q.push(ii(sx, sy));
  dis[sx][sy] = 0;
  while (q.size()) {
    ii p = q.front();
    q.pop();
    int x = p.ff, y = p.se;
    fr(i, 0, 4) fr(j, 1, k + 1) {
      int xx = x + j * dx[i], yy = y + j * dy[i];
      if (xx < 0 || xx >= n || yy < 0 || yy >= m)
        break;
      if (s[xx][yy] == '@' || dis[xx][yy] <= dis[x][y])
        break;
      if (dis[xx][yy] > dis[x][y] + 1) {
        dis[xx][yy] = dis[x][y] + 1;
        q.push(ii(xx, yy));
      }
    }
  }

  if (dis[ex][ey] == INF)
    puts("-1");
  else
    write(dis[ex][ey], nl);
}

int main() {
  int tc = 1; /// read( tc );
  fr(cs, 1, tc + 1) MAIN(cs);
  return 0;
}

char os[25];
TP IL void in(T *a, size_t n) { fr(i, 0, n) read(a[i]); }
TP IL void in(vector<T> &a, size_t n) { fr(i, 0, n) read(a[i]); }
TP IL void out(T *a, size_t n) {
  fr(i, 0, n) write(a[i], (i + 1 == n) ? nl : gp);
}
TP IL void out(vector<T> &a, size_t n) {
  fr(i, 0, n) write(a[i], (i + 1 == n) ? nl : gp);
}
ST IL ll Calculator<M>::Add(ll x, ll y) { return (x + y) % M; }
ST IL ll Calculator<M>::Inv(ll x) { return power<ll>(x, M - 2, M); }
ST IL ll Calculator<M>::Sub(ll x, ll y) { return (((x - y) % M) + M) % M; }
ST IL ll Calculator<M>::Mul(ll x, ll y) { return ((x % M) * (y % M)); }
ST IL ll Calculator<M>::Div(ll x, ll y) { return Mul(x, Inv(y)); }
TP IL void write(T x) {
  if (x == 0) {
    pc('0');
    return;
  }
  int i = 0;
  bool neg = (x < 0);
  if (neg)
    pc('-'), x = -x;
  while (x > 0) {
    os[i++] = (x % 10) + '0';
    x /= 10;
  }
  i--;
  while (i >= 0)
    pc(os[i--]);
}
IL void write(char *s) { pf("%s", s); }
IL void write(const char *s) { pf("%s", s); }
IL void write(char c) { pc(c); }
HP IL void write(H h, T... t) {
  write(h);
  write(t...);
}
TP IL bool read(T &val) {
  val = 0;
  bool neg = 0;
  char ch = gc;
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      neg = 1;
    else if (ch == EOF)
      return 0;
    ch = gc;
  }
  while (ch >= '0' && ch <= '9') {
    val = (val * 10) + (ch - '0');
    ch = gc;
  }
  if (neg)
    val = -val;
  return 1;
}
HP IL bool read(H &h, T &...t) { return read(h) && read(t...); }
IL bool read(char &c) {
  c = gc;
  return c != EOF;
}
IL bool read(char *s) { return scanf("%s", s); }
TP IL T lcm(T a, T b) { return (a / gcd(a, b)) * b; }
TP IL T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
TP IL T power(T v, T p) {
  T r = 1;
  while (p > 0) {
    if (p & 0x1)
      r = (r * v);
    v = (v * v);
    p >>= 1;
  }
  return r;
}
TP IL T power(T v, T p, T m) {
  v %= m;
  T r = 1;
  while (p > 0) {
    if (p & 0x1)
      r = (r * 1LL * v) % m;
    v = (v * 1LL * v) % m;
    p >>= 1;
  }
  return r;
}
TP IL T powerL(T v, T p, T m) {
  v %= m;
  T r = 1;
  while (p > 0) {
    if (p & 0x1)
      r = MulL(r, v, m);
    v = MulL(v, v, m);
    p >>= 1;
  }
  return r;
}
TP IL T MulL(T a, T b, T m) {
  T r = 0;
  a %= m;
  b %= m;
  while (b) {
    if (b & 0x1)
      r = (r + a) % m;
    b >>= 1;
    if (a < m - a)
      a <<= 1;
    else
      a -= (m - a);
  }
  return r;
}


Error: TLE

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
using vi = vector<int>;
using ii = pair<int, int>;

#define gp " "
#define nl "\\n"
#define ff first
#define se second

const int dx[] = {1, -1, 0, 0, -1, 1, -1, 1};
const int dy[] = {0, 0, -1, 1, 1, 1, -1, -1};

int n, m, K;
int sx, sy, ex, ey;

vector<string> s;
vector<vi> vis[4], dis[4];

struct node {
  int x, y, k, d;
  node() {}
  node(int _x, int _y, int _k, int _d) : x(_x), y(_y), k(_k), d(_d) {}
};

inline int bfs() {
  queue<node> q;
  for (int i = 0; i < 4; i++) {
    int x = sx + dx[i], y = sy + dy[i];
    vis[i][sx][sy] = 1;
    if (x >= 0 && x < n && y >= 0 && y < m && s[x][y] != '@')
      vis[i][x][y] = dis[i][x][y] = 1, q.push(node(x, y, (1) % K, i));
  }

  while (q.size()) {
    node p = q.front();
    q.pop();
    int x = p.x, y = p.y, k = p.k, d = p.d;

    for (int i = 0; i < 4; i++) {
      int xx = x + dx[i], yy = y + dy[i];

      if (xx < 0 || xx >= n || yy < 0 || yy >= m)
        continue;
      if (s[xx][yy] == '@')
        continue;

      if (i == d) {
        int di = dis[d][x][y] + (k == 0);
        if (!vis[i][xx][yy]) {
          vis[i][xx][yy] = 1;
          dis[i][xx][yy] = di;
          q.push(node(xx, yy, (k + 1) % K, i));
        } else if (dis[i][xx][yy] >= di) {
          dis[i][xx][yy] = di;
          q.push(node(xx, yy, (k + 1) % K, i));
        }
      } else {
        int di = dis[d][x][y] + 1;
        if (!vis[i][xx][yy]) {
          vis[i][xx][yy] = 1;
          dis[i][xx][yy] = di;
          q.push(node(xx, yy, (1) % K, i));
        } else if (dis[i][xx][yy] >= di) {
          dis[i][xx][yy] = di;
          q.push(node(xx, yy, (1) % K, i));
        }
      }
    }
  }

  int ans = INT_MAX;
  for (int i = 0; i < 4; i++)
    if (vis[i][ex][ey])
      ans = min(ans, dis[i][ex][ey]);
  if (ans == INT_MAX)
    ans = -1;
  return ans;
}

int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(0);

  cin >> n >> m >> K;
  cin >> sx >> sy >> ex >> ey;

  sx--;
  sy--;
  ex--;
  ey--;
  for (int i = 0; i < 4; i++) {
    vis[i].assign(n, vi(m, 0));
    dis[i].assign(n, vi(m, 0));
  }

  s.resize(n);
  for (auto &ss : s)
    cin >> ss;
  cout << bfs() << nl;

  return 0;
}