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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll maxn = 1e6 + 5;
const ll maxm = 1e3 + 5;
namespace IO {
const int SIZE = (1 << 20) + 1;
char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = obuf + SIZE - 1;
char _st[55];
int _qr = 0;
inline char gc() {
  return (iS == iT ? iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
          (iS == iT ? EOF : *iS++) : *iS++);
}
inline void qread() {}
template <class T1, class... T2> inline void qread(T1 &IEE, T2 &...ls) {
  register T1 __ = 0, ___ = 1;
  register char ch;
  while (!isdigit(ch = gc()))
    ___ = (ch == '-') ? -___ : ___;
  do {
    __ = (__ << 1) + (__ << 3) + (ch ^ 48);
  } while (isdigit(ch = gc()));
  __ *= ___;
  IEE = __;
  qread(ls...);
  return;
}
inline void flush() {
  fwrite(obuf, 1, oS - obuf, stdout);
  oS = obuf;
  return;
}
inline void putc_(char _x) {
  *oS++ = _x;
  if (oS == oT)
    flush();
}
inline void qwrite() {}
template <class T1, class... T2> inline void qwrite(T1 IEE, T2... ls) {
  if (!IEE)
    putc_('0');
  if (IEE < 0)
    putc_('-'), IEE = -IEE;
  while (IEE)
    _st[++_qr] = IEE % 10 + '0', IEE /= 10;
  while (_qr)
    putc_(_st[_qr--]);
  qwrite(ls...);
  return;
}
struct Flusher_ {
  ~Flusher_() { flush(); }
} io_flusher;
} // namespace IO
using namespace IO;
ll n, m, k, sx, sy, tx, ty;
char ch[maxn];
inline ll id(ll x, ll y) { return (x - 1) * m + y; }
inline ll north(ll x) { return x - m; }
inline ll south(ll x) { return x + m; }
inline ll west(ll x) { return x - 1; }
inline ll east(ll x) { return x + 1; }
inline ll row(ll x) { return (x + m - 1) / m; }
inline ll col(ll x) { return (x - 1) % m + 1; }
inline bool chknth(ll x) { return row(x) > 1; }
inline bool chksth(ll x) { return row(x) < n; }
inline bool chkwst(ll x) { return col(x) > 1; }
inline bool chkest(ll x) { return col(x) < m; }
ll nth[maxn], sth[maxn], wst[maxn], est[maxn];
void init() {
  for (int i = 1; i <= n * m; i++) {
    nth[i] = sth[i] = wst[i] = est[i] = i;
  }
  return;
}
ll findnth(ll x) { return nth[x] == x ? x : nth[x] = findnth(nth[x]); }
ll findsth(ll x) { return sth[x] == x ? x : sth[x] = findsth(sth[x]); }
ll findwst(ll x) { return wst[x] == x ? x : wst[x] = findwst(wst[x]); }
ll findest(ll x) { return est[x] == x ? x : est[x] = findest(est[x]); }
void mgenth(ll x, ll y) {
  nth[findnth(x)] = findnth(y);
  return;
}
void mgesth(ll x, ll y) {
  sth[findsth(x)] = findsth(y);
  return;
}
void mgewst(ll x, ll y) {
  wst[findwst(x)] = findwst(y);
  return;
}
void mgeest(ll x, ll y) {
  est[findest(x)] = findest(y);
  return;
}
queue<ll> q;
ll d[maxn];
int main() {
  memset(d, -1, sizeof(d));
  cin.tie(0);
  cin >> n >> m >> k >> sx >> sy >> tx >> ty;
  init();
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> ch[id(i, j)];
    }
  }
  d[id(sx, sy)] = 0;
  q.push(id(sx, sy));
  while (!q.empty()) {
    ll fr = q.front();
    q.pop();
    ll x = findnth(fr);
    while (row(fr) - row(x) <= k && ch[x] != '@') {
      if (d[x] == -1) {
        d[x] = d[fr] + 1;
        q.push(x);
      }
      if (chknth(x)) {
        mgenth(x, north(x));
        x = findnth(x);
      } else {
        break;
      }
    }
    x = findsth(fr);
    while (row(x) - row(fr) <= k && ch[x] != '@') {
      if (d[x] == -1) {
        d[x] = d[fr] + 1;
        q.push(x);
      }
      if (chksth(x)) {
        mgesth(x, south(x));
        x = findsth(x);
      } else {
        break;
      }
    }
    x = findwst(fr);
    while (col(fr) - col(x) <= k && ch[x] != '@') {
      if (d[x] == -1) {
        d[x] = d[fr] + 1;
        q.push(x);
      }
      if (chkwst(x)) {
        mgewst(x, west(x));
        x = findwst(x);
      } else {
        break;
      }
    }
    x = findest(fr);
    while (col(x) - col(fr) <= k && ch[x] != '@') {
      if (d[x] == -1) {
        d[x] = d[fr] + 1;
        q.push(x);
      }
      if (chkest(x)) {
        mgeest(x, east(x));
        x = findest(x);
      } else {
        break;
      }
    }
  }
  cout << d[id(tx, ty)] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll maxn = 1e6 + 5;
const ll maxm = 1e3 + 5;
namespace IO {
const int SIZE = (1 << 20) + 1;
char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = obuf + SIZE - 1;
char _st[55];
int _qr = 0;
inline char gc() {
  return (iS == iT ? iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
          (iS == iT ? EOF : *iS++) : *iS++);
}
inline void qread() {}
template <class T1, class... T2> inline void qread(T1 &IEE, T2 &...ls) {
  register T1 __ = 0, ___ = 1;
  register char ch;
  while (!isdigit(ch = gc()))
    ___ = (ch == '-') ? -___ : ___;
  do {
    __ = (__ << 1) + (__ << 3) + (ch ^ 48);
  } while (isdigit(ch = gc()));
  __ *= ___;
  IEE = __;
  qread(ls...);
  return;
}
inline void flush() {
  fwrite(obuf, 1, oS - obuf, stdout);
  oS = obuf;
  return;
}
inline void putc_(char _x) {
  *oS++ = _x;
  if (oS == oT)
    flush();
}
inline void qwrite() {}
template <class T1, class... T2> inline void qwrite(T1 IEE, T2... ls) {
  if (!IEE)
    putc_('0');
  if (IEE < 0)
    putc_('-'), IEE = -IEE;
  while (IEE)
    _st[++_qr] = IEE % 10 + '0', IEE /= 10;
  while (_qr)
    putc_(_st[_qr--]);
  qwrite(ls...);
  return;
}
struct Flusher_ {
  ~Flusher_() { flush(); }
} io_flusher;
} // namespace IO
using namespace IO;
ll n, m, k, sx, sy, tx, ty;
char ch[maxn];
inline ll id(ll x, ll y) { return (x - 1) * m + y; }
inline ll north(ll x) { return x - m; }
inline ll south(ll x) { return x + m; }
inline ll west(ll x) { return x - 1; }
inline ll east(ll x) { return x + 1; }
inline ll row(ll x) { return (x + m - 1) / m; }
inline ll col(ll x) { return (x - 1) % m + 1; }
inline bool chknth(ll x) { return row(x) > 1; }
inline bool chksth(ll x) { return row(x) < n; }
inline bool chkwst(ll x) { return col(x) > 1; }
inline bool chkest(ll x) { return col(x) < m; }
ll nth[maxn], sth[maxn], wst[maxn], est[maxn];
void init() {
  for (int i = 1; i <= n * m; i++) {
    nth[i] = sth[i] = wst[i] = est[i] = i;
  }
  return;
}
ll findnth(ll x) { return nth[x] == x ? x : nth[x] = findnth(nth[x]); }
ll findsth(ll x) { return sth[x] == x ? x : sth[x] = findnth(sth[x]); }
ll findwst(ll x) { return wst[x] == x ? x : wst[x] = findnth(wst[x]); }
ll findest(ll x) { return est[x] == x ? x : est[x] = findnth(est[x]); }
void mgenth(ll x, ll y) {
  nth[findnth(x)] = findnth(y);
  return;
}
void mgesth(ll x, ll y) {
  sth[findsth(x)] = findsth(y);
  return;
}
void mgewst(ll x, ll y) {
  wst[findwst(x)] = findwst(y);
  return;
}
void mgeest(ll x, ll y) {
  est[findest(x)] = findest(y);
  return;
}
queue<ll> q;
ll d[maxn];
int main() {
  memset(d, -1, sizeof(d));
  cin.tie(0);
  cin >> n >> m >> k >> sx >> sy >> tx >> ty;
  init();
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> ch[id(i, j)];
    }
  }
  d[id(sx, sy)] = 0;
  q.push(id(sx, sy));
  while (!q.empty()) {
    ll fr = q.front();
    q.pop();
    ll x = findnth(fr);
    while (row(fr) - row(x) <= k && ch[x] != '@') {
      if (d[x] == -1) {
        d[x] = d[fr] + 1;
        q.push(x);
      }
      if (chknth(x)) {
        mgenth(x, north(x));
        x = findnth(x);
      } else {
        break;
      }
    }
    x = findsth(fr);
    while (row(x) - row(fr) <= k && ch[x] != '@') {
      if (d[x] == -1) {
        d[x] = d[fr] + 1;
        q.push(x);
      }
      if (chksth(x)) {
        mgesth(x, south(x));
        x = findsth(x);
      } else {
        break;
      }
    }
    x = findwst(fr);
    while (col(fr) - col(x) <= k && ch[x] != '@') {
      if (d[x] == -1) {
        d[x] = d[fr] + 1;
        q.push(x);
      }
      if (chkwst(x)) {
        mgewst(x, west(x));
        x = findwst(x);
      } else {
        break;
      }
    }
    x = findest(fr);
    while (col(x) - col(fr) <= k && ch[x] != '@') {
      if (d[x] == -1) {
        d[x] = d[fr] + 1;
        q.push(x);
      }
      if (chkest(x)) {
        mgeest(x, east(x));
        x = findest(x);
      } else {
        break;
      }
    }
  }
  cout << d[id(tx, ty)] << endl;
  return 0;
}