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

#include <bits/stdc++.h>
#pragma GCC optimize(2)
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;
int n, m, cur, cx, cy, dx, dy;
char ch[maxm][maxm];
int dar[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
inline int id(ll ix, ll iy) { return (ix - 1) * m + iy; }
int head[maxn], nex[maxn * 30], to[maxn * 30], val[maxn * 30];
void add(ll u, ll v, ll w) {
  nex[++cur] = head[u];
  to[cur] = v;
  val[cur] = w;
  head[u] = cur;
  return;
}
bool in(ll dx, ll dy) { return dx > 0 && dx <= n && dy > 0 && dy <= m; }
int dis[maxm][maxm];
bool al[maxm][maxm];
deque<pair<int, int>> q;
void spfa(ll sx, ll sy) {
  memset(dis, 0x3f, sizeof(dis));
  q.push_back({sx, sy});
  dis[sx][sy] = 0;
  al[sx][sy] = 0;
  while (!q.empty()) {
    int fx = q.front().first, fy = q.front().second;
    q.pop_front();
    if (al[fx][fy])
      continue;
    al[fx][fy] = 1;
    for (int k = 0; k < 4; k++) {
      ll tx = fx + dar[k][0];
      ll ty = fy + dar[k][1];
      if (!in(tx, ty) || ch[tx][ty] == '#' || al[tx][ty])
        continue;
      dis[tx][ty] = min(dis[tx][ty], dis[fx][fy]);
      q.push_front({tx, ty});
    }
    for (int kx = fx - 2; kx <= fx + 2; kx++) {
      for (int ky = fy - 2; ky <= fy + 2; ky++) {
        if (kx == fx && ky == fy)
          continue;
        if (!in(kx, ky) || ch[kx][ky] == '#' || al[kx][ky])
          continue;
        dis[kx][ky] = min(dis[kx][ky], dis[fx][fy] + 1);
        q.push_back({kx, ky});
      }
    }
  }
  return;
}
int main() {
  cin >> n >> m;
  cin >> cx >> cy >> dx >> dy;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      char c = getchar();
      while (c != '.' && c != '#')
        c = getchar();
      ch[i][j] = c;
    }
  }
  // for(int i = 1;i <= n;i++){
  //     for(int j = 1;j <= m;j++){
  //         if(ch[i][j] == '#') continue;
  //         for(int k = 0;k < 4;k++){
  //             ll tx = i + dar[k][0];
  //             ll ty = j + dar[k][1];
  //             if(!in(tx, ty) || ch[tx][ty] == '#') continue;
  //             add(id(i, j), id(tx, ty), 0);
  //         }
  //         for(int kx = i - 2;kx <= i + 2;kx++){
  //             for(int ky = j - 2;ky <= j + 2;ky++){
  //                 if(kx == i && ky == j) continue;
  //                 if(!in(kx, ky) || ch[kx][ky] == '#') continue;
  //                 add(id(i, j), id(kx, ky), 1);
  //             }
  //         }
  //     }
  // }
  spfa(cx, cy);
  if (dis[dx][dy] >= 0x3f3f3f3f) {
    cout << -1 << endl;
  } else {
    cout << dis[dx][dy] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#pragma GCC optimize(2)
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;
int n, m, cur, cx, cy, dx, dy;
char ch[maxm][maxm];
int dar[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
inline int id(ll ix, ll iy) { return (ix - 1) * m + iy; }
int head[maxn], nex[maxn * 30], to[maxn * 30], val[maxn * 30];
void add(ll u, ll v, ll w) {
  nex[++cur] = head[u];
  to[cur] = v;
  val[cur] = w;
  head[u] = cur;
  return;
}
bool in(ll dx, ll dy) { return dx > 0 && dx <= n && dy > 0 && dy <= m; }
int dis[maxn];
bool al[maxn];
int q[maxn * 10], l = 1, r = 0;
void spfa(ll st) {
  memset(dis, 0x3f, sizeof(dis));
  q[++r] = st;
  dis[st] = 0;
  al[st] = 1;
  while (r - l + 1 > 0) {
    ll fr = q[l];
    ++l;
    al[fr] = 0;
    for (int i = head[fr]; i; i = nex[i]) {
      ll t = to[i];
      if (dis[fr] + val[i] < dis[t]) {
        dis[t] = dis[fr] + val[i];
        if (al[t])
          continue;
        q[++r] = t;
        al[t] = 1;
      }
    }
  }
  return;
}
int main() {
  cin >> n >> m;
  cin >> cx >> cy >> dx >> dy;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      char c = getchar();
      while (c != '.' && c != '#')
        c = getchar();
      ch[i][j] = c;
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (ch[i][j] == '#')
        continue;
      for (int k = 0; k < 4; k++) {
        ll tx = i + dar[k][0];
        ll ty = j + dar[k][1];
        if (!in(tx, ty) || ch[tx][ty] == '#')
          continue;
        add(id(i, j), id(tx, ty), 0);
      }
      for (int kx = i - 2; kx <= i + 2; kx++) {
        for (int ky = j - 2; ky <= j + 2; ky++) {
          if (kx == i && ky == j)
            continue;
          if (!in(kx, ky) || ch[kx][ky] == '#')
            continue;
          add(id(i, j), id(kx, ky), 1);
        }
      }
    }
  }
  spfa(id(cx, cy));
  if (dis[id(dx, dy)] >= 0x3f3f3f3f) {
    cout << -1 << endl;
  } else {
    cout << dis[id(dx, dy)] << endl;
  }
  return 0;
}