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

#include <bits/stdc++.h>
#define N 1001
using namespace std;
struct Node {
  int x, y;
};
deque<Node> q;
int sx, sy, tx, ty, n, m, dis[N][N], dx[4] = {1, -1, 0, 0},
                                     dy[4] = {0, 0, 1, -1};
char str[N][N], vis[N][N];
int main(void) {
  int i, dir, j;
  Node x, y;
  memset(dis, 0x3f, sizeof dis);
  scanf("%d%d%d%d%d%d", &n, &m, &sx, &sy, &tx, &ty);
  for (i = 1; i <= n; ++i)
    scanf("%s", str[i] + 1);
  q.push_back({sx, sy}), dis[sx][sy] = 0;
  while (!q.empty()) {
    x = q.front(), q.pop_front();
    if (vis[x.x][x.y])
      continue;
    vis[x.x][x.y] = 1;
    for (dir = 0; dir < 4; ++dir) {
      y = {x.x + dx[dir], x.y + dy[dir]};
      if (y.x && y.x <= n && y.y && y.y <= m && str[y.x][y.y] ^ '#' &&
          dis[y.x][y.y] > dis[x.x][x.y])
        dis[y.x][y.y] = dis[x.x][x.y], q.push_front(y);
    }
    for (i = -2; i <= 2; ++i) {
      for (j = -2; j <= 2; ++j) {
        y = {x.x + i, x.y + j};
        if (y.x >= 1 && y.x <= n && y.y >= 1 && y.y <= m &&
            str[y.x][y.y] ^ '#' && dis[y.x][y.y] > dis[x.x][x.y] + 1)
          dis[y.x][y.y] = dis[x.x][x.y] + 1, q.push_back(y);
      }
    }
  }
  printf("%d\\n", dis[tx][ty] ^ 0x3f3f3f3f ? dis[tx][ty] : -1);
}


Error: TLE

Fixed submission:

#pragma GCC optimization("0fast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
/// speed up
/***************************/

#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define per(i, a, b) for (int i = a; i >= b; i--)
#define rusha                                                                  \\
  scanf("%d", &n);                                                             \\
  rep(i, 1, n) scanf("%d", &a[i]);
#define rushb                                                                  \\
  scanf("%d", &t);                                                             \\
  while (t--)
#define pb push_back
#define eb emplace_back
#define pii pair<int, int>
#define vii vector<int>
#define fi first
#define se second
#define mem(a, b) memset(a, b, sizeof a)
#define all(x) (x.begin(), x.end())
#define judgef printf("%s\\n", f ? "Yes" : "No")
#define Judgef printf("%s\\n", f ? "YES" : "NO")
#define jud(f) printf("%s\\n", f ? "Yes" : "No")
#define Jud(f) printf("%s\\n", f ? "YES" : "NO")
#define sz(x) ((int)(x).size())
#define sf(x) (sizeof(x))
#define error(args...)                                                         \\
  {                                                                            \\
    string _s = #args;                                                         \\
    replace(_s.begin(), _s.end(), ',', ' ');                                   \\
    stringstream _ss(_s);                                                      \\
    istream_iterator<string> _it(_ss);                                         \\
    err(_it, args);                                                            \\
    cerr << endl;                                                              \\
  }
#define debug(args...)                                                         \\
  {                                                                            \\
    string _s = #args;                                                         \\
    replace(_s.begin(), _s.end(), ',', ' ');                                   \\
    stringstream _ss(_s);                                                      \\
    istream_iterator<string> _it(_ss);                                         \\
    dbg(_it, args);                                                            \\
  }
using namespace std;
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << " | ";
  err(++it, args...);
}
void dbg(istream_iterator<string> it) {}
template <typename T, typename... Args>
void dbg(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << endl;
  dbg(++it, args...);
}
/***********************/
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
// typedef gp_hash_table<int, ll, hash<ll> > ht;
/***********************/
typedef long long ll;
typedef double db;
typedef unsigned long long ull;
typedef unsigned int uint;
// mt19937_64 mrand64(random_device{}());
mt19937_64 mrand64(chrono::steady_clock::now().time_since_epoch().count());
ll rng(ll x) { return (ull)mrand64() % x; }
const double pai = acos(-1.0);
const double eps = 1e-8;
const int mod = 998244353;
// const int mod=1e9+7;
// const int mod=100000429;
const int inf = 1e9 + 9;
// const ll inf=0x7f7f7f1234abcd;
// const int N=2e5+10;
const int maxn = 1e3 + 10;
inline ll ksm(ll x, ll y) {
  ll res = 1;
  while (y) {
    if (y & 1)
      res = res * x % mod;
    x = x * x % mod;
    y >>= 1;
  }
  return res;
}
inline ll inv(ll a, ll p) {
  if (a == 1)
    return 1;
  return (p - p / a) * inv(p % a, p) % p;
}
inline ll lcm(ll a, ll b) { return a * b / __gcd(a, b); }

int t, n, m, k;
char a[maxn][maxn];
int d[maxn][maxn];
int f[25][2] = {2,  2,  2,  1,  2,  0, 2,  -1, 2,  -2, 1,  2, 1,  1,
                1,  -1, 1,  -2, 0,  2, 0,  -2, -1, 2,  -1, 1, -1, -1,
                -1, -2, -2, 2,  -2, 1, -2, 0,  -2, -1, -2, -2};
inline void dfs(int x, int y, int dep) {
  if (x < 0 || x > n || y < 0 || y > m || a[x][y] == 0 || a[x][y] == '#' ||
      dep >= d[x][y])
    return;
  d[x][y] = min(d[x][y], dep);
  dfs(x + 1, y, dep);
  dfs(x, y + 1, dep);
  dfs(x - 1, y, dep);
  dfs(x, y - 1, dep);
  rep(i, 0, 19) dfs(x + f[i][0], y + f[i][1], dep + 1);
  /// error(dep);
}

int main() {
  int x1, x2, y1, y2;
  scanf("%d%d%d%d%d%d", &n, &m, &x1, &y1, &x2, &y2);
  rep(i, 1, n) rep(j, 1, m) d[i][j] = inf;
  rep(i, 1, n) scanf("%s", a[i] + 1);
  dfs(x1, y1, 0);
  /// rep(i,1,n) {rep(j,1,m) printf("%d ",d[i][j]);puts("");}
  printf("%d\\n", d[x2][y2] == inf ? -1 : d[x2][y2]);
  return 0;
}

/*

*/

//                  .,:,,,                                        .::,,,::
//                .::::,,;;,                                  .,;;:,,....:i:
//                :i,.::::,;i:.      ....,,:::::::::,....   .;i:,.  ......;i.
//                :;..:::;::::i;,,:::;:,,,,,,,,,,..,.,,:::iri:. .,:irsr:,.;i.
//                ;;..,::::;;;;ri,,,.                    ..,,:;s1s1ssrr;,.;r,
//                :;. ,::;ii;:,     . ...................     .;iirri;;;,,;i,
//                ,i. .;ri:.   ... ............................  .,,:;:,,,;i:
//                :s,.;r:... ....................................... .::;::s;
//                ,1r::. .............,,,.,,:,,........................,;iir;
//                ,s;...........     ..::.,;:,,.          ...............,;1s
//               :i,..,.              .,:,,::,.          .......... .......;1,
//              ir,....:rrssr;:,       ,,.,::.     .r5S9989398G95hr;. ....,.:s,
//             ;r,..,s9855513XHAG3i   .,,,,,,,.  ,S931,.,,.;s;s&BHHA8s.,..,..:r:
//            :r;..rGGh,  :SAG;;G@BS:.,,,,,,,,,.r83: hHAcceptedHM3..,,,,.ir.
//           ,si,.1GS,   sBMAAX&MBMB5,,,,,,:,,.:&8 3@HAcceptedH#X,.,,,,,,rr
//           ;1:,,SH:   .A@&&B#&8H#BS,,,,,,,,,.,5XS, 3@MAccepted#As..,,,,:,is,
//          .rr,,,;9&1   hBHHBB&8AMGr,,,,,,,,,,,:h&&9s;   r9Accepted9:  .
//          .,,,,;ri. :1:....:5&XSi;r8BMBHHA9r:,......,,,,:ii19GG88899XHHH&GSr.
//          ...,:rs. ;s.     .:sS8G8GG889hi.        ....,,:;:,.:irssrriii:,.
//          ...,,i1, ;1,         ..,....,,isssi;,        .,,. ....,.i1, ;h:
//          i9HHBMBBHAX9:         .                     ...,,,rs, ,1i..
//          :A#MBBBBMHB##s                             ....,,,;si. .r1,..
//          ,..;3BMBBBHBB#Bh.     ..                    ....,,,,,i1;
//           :h;..       .,..;,1XBMMMMBXs,.,, .. :: ,. ....,,,,,,ss.
//            ih: ..    .;;;, ;;:s58A3i,..    ,. ,.:,,. ...,,,,,:,s1, .s1,....
//            .,;sh,  ,iSAXs;.    ,.  ,,.i85            ...,,,,,,:i1;
//             .rh: ...     rXG9XBBM#M#MHAX3hss13&&HHXr         .....,,,,,,,ih;
//              .s5: .....    i598X&&A&AAAAAA&XG851r:       ........,,,,:,,sh;
//              . ihr, ...  .         ..                    ........,,,,,;11:
//                 ,s1i. ...  ..,,,..,,,.,,.,,.,..       ........,,.,,.;s5i.
//                  .:s1r,......................       ..............;shs,
//                  . .:shr:.  ....                 ..............,ishs.
//                      .,issr;,... ...........................,is1s;.
//                         .,is1si;:,....................,:;ir1sr;,
//                            ..:isssssrrii;::::::;;iirsssssr;:..
//                                 .,::iiirsssssssssrri;;:.