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

#include <bits/stdc++.h>

#define si(a) scanf("%d", &a)
#define sl(a) scanf("%lld", &a)
#define sd(a) scanf("%lf", &a)
#define sc(a) scahf("%c", &a);
#define ss(a) scanf("%s", a)
#define pi(a) printf("%d\\n", a)
#define pl(a) printf("%lld\\n", a)
#define pc(a) putchar(a)
#define ms(a) memset(a, 0, sizeof(a))
#define repi(i, a, b) for (register int i = a; i <= b; ++i)
#define repd(i, a, b) for (register int i = a; i >= b; --i)
#define reps(s) for (register int i = head[s]; i; i = Next[i])
#define ll long long
#define ull unsigned long long
#define vi vector<int>
#define pii pair<int, int>
#define mii unordered_map<int, int>
#define msi unordered_map<string, int>
#define lowbit(x) ((x) & (-(x)))
#define ce(i, r) i == r ? '\\n' : ' '
#define pb push_back
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define INF 0x3f3f3f3f
#define pr(x) cout << #x << ": " << x << endl
using namespace std;

inline int qr() {
  int f = 0, fu = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      fu = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    f = (f << 3) + (f << 1) + c - 48;
    c = getchar();
  }
  return f * fu;
}

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
const int N = 1e3 + 10, M = 1e6 + 10;
char a[N][N];
int d[N][N];
int n, m, sx, sy, tx, ty;

inline bool check(int x, int y) {
  return x >= 1 && x <= n && y >= 1 && y <= m && a[x][y] != '#';
}

inline int bfs() {
  priority_queue<pair<int, pii>> q;
  q.push({0, {sx, sy}}), d[sx][sy] = 0;
  while (!q.empty()) {
    pii t = q.top().se;
    q.pop();
    repi(i, 0, 3) {
      int nx = t.fi + dx[i], ny = t.se + dy[i];
      if (check(nx, ny) && d[nx][ny] > d[t.fi][t.se])
        d[nx][ny] = d[t.fi][t.se], q.push({-d[t.fi][t.se], {nx, ny}});
    }
    repi(i, max(1, t.fi - 2), min(n, t.fi + 2)) repi(
        j, max(1, max(1, t.se - 2)),
        min(m, t.se + 2)) if (a[i][j] != '#' && d[i][j] > d[t.fi][t.se] + 1)
        d[i][j] = d[t.fi][t.se] + 1,
        q.push({-d[i][j], {i, j}});
  }
  return d[tx][ty] == INF ? -1 : d[tx][ty];
}

int main() {
  n = qr(), m = qr();
  sx = qr(), sy = qr();
  tx = qr(), ty = qr();
  memset(d, 0x3f, sizeof(d));
  repi(i, 1, n) ss(a[i] + 1);
  pi(bfs());
  return 0;
}

Error: TLE

Fixed submission:

#pragma GCC optimize(2)
#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline")
#pragma GCC optimize("-fgcse")
#pragma GCC optimize("-fgcse-lm")
#pragma GCC optimize("-fipa-sra")
#pragma GCC optimize("-ftree-pre")
#pragma GCC optimize("-ftree-vrp")
#pragma GCC optimize("-fpeephole2")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-fsched-spec")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-falign-jumps")
#pragma GCC optimize("-falign-loops")
#pragma GCC optimize("-falign-labels")
#pragma GCC optimize("-fdevirtualize")
#pragma GCC optimize("-fcaller-saves")
#pragma GCC optimize("-fcrossjumping")
#pragma GCC optimize("-fthread-jumps")
#pragma GCC optimize("-funroll-loops")
#pragma GCC optimize("-fwhole-program")
#pragma GCC optimize("-freorder-blocks")
#pragma GCC optimize("-fschedule-insns")
#pragma GCC optimize("inline-functions")
#pragma GCC optimize("-ftree-tail-merge")
#pragma GCC optimize("-fschedule-insns2")
#pragma GCC optimize("-fstrict-aliasing")
#pragma GCC optimize("-fstrict-overflow")
#pragma GCC optimize("-falign-functions")
#pragma GCC optimize("-fcse-skip-blocks")
#pragma GCC optimize("-fcse-follow-jumps")
#pragma GCC optimize("-fsched-interblock")
#pragma GCC optimize("-fpartial-inlining")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("-freorder-functions")
#pragma GCC optimize("-findirect-inlining")
#pragma GCC optimize("-fhoist-adjacent-loads")
#pragma GCC optimize("-frerun-cse-after-loop")
#pragma GCC optimize("inline-small-functions")
#pragma GCC optimize("-finline-small-functions")
#pragma GCC optimize("-ftree-switch-conversion")
#pragma GCC optimize("-foptimize-sibling-calls")
#pragma GCC optimize("-fexpensive-optimizations")
#pragma GCC optimize("-funsafe-loop-optimizations")
#pragma GCC optimize("inline-functions-called-once")
#pragma GCC optimize("-fdelete-null-pointer-checks")
#pragma GCC optimize(3)
#include <bits/stdc++.h>

#define si(a) scanf("%d", &a)
#define sl(a) scanf("%lld", &a)
#define sd(a) scanf("%lf", &a)
#define sc(a) scahf("%c", &a);
#define ss(a) scanf("%s", a)
#define pi(a) printf("%d\\n", a)
#define pl(a) printf("%lld\\n", a)
#define pc(a) putchar(a)
#define ms(a) memset(a, 0, sizeof(a))
#define repi(i, a, b) for (register int i = a; i <= b; ++i)
#define repd(i, a, b) for (register int i = a; i >= b; --i)
#define reps(s) for (register int i = head[s]; i; i = Next[i])
#define ll long long
#define ull unsigned long long
#define vi vector<int>
#define pii pair<int, int>
#define mii unordered_map<int, int>
#define msi unordered_map<string, int>
#define lowbit(x) ((x) & (-(x)))
#define ce(i, r) i == r ? '\\n' : ' '
#define pb push_back
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define INF 0x3f3f3f3f
#define pr(x) cout << #x << ": " << x << endl
using namespace std;

inline int qr() {
  int f = 0, fu = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      fu = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    f = (f << 3) + (f << 1) + c - 48;
    c = getchar();
  }
  return f * fu;
}

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
const int N = 1e3 + 10, M = 1e6 + 10;
char a[N][N];
int d[N][N];
int n, m, sx, sy, tx, ty;

inline bool check(int x, int y) {
  return x >= 1 && x <= n && y >= 1 && y <= m && a[x][y] != '#';
}

inline int bfs() {
  queue<pii> q;
  q.push({sx, sy}), d[sx][sy] = 0;
  while (!q.empty()) {
    pii t = q.front();
    q.pop();
    repi(i, 0, 3) {
      int nx = t.fi + dx[i], ny = t.se + dy[i];
      if (check(nx, ny) && d[nx][ny] > d[t.fi][t.se])
        d[nx][ny] = d[t.fi][t.se], q.push({nx, ny});
    }
    repi(i, max(1, t.fi - 2), min(n, t.fi + 2)) repi(
        j, max(1, max(1, t.se - 2)),
        min(m, t.se + 2)) if (a[i][j] != '#' && d[i][j] > d[t.fi][t.se] + 1)
        d[i][j] = d[t.fi][t.se] + 1,
        q.push({i, j});
  }
  return d[tx][ty] == INF ? -1 : d[tx][ty];
}

int main() {
  n = qr(), m = qr();
  sx = qr(), sy = qr();
  tx = qr(), ty = qr();
  memset(d, 0x3f, sizeof(d));
  repi(i, 1, n) ss(a[i] + 1);
  pi(bfs());
  return 0;
}