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

#pragma GCC optimize(2)
#pragma G++ optimize(2)
#include <bits/stdc++.h>
#define si(a) scanf("%d", &a)
#define sl(a) scanf("%lld", &a)
#define sd(a) scanf("%lf", &a)
#define ss(a) scanf("%s", 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 (int i = head[s], v = e[i].to; i; i = e[i].nxt, v = e[i].to)
#define ce(i, r) i == r ? '\\n' : ' '
#define pb push_back
#define all(x) x.begin(), x.end()
#define fi first
#define se second
// #define pr(x) cout<<#x<<": "<<x<<endl
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int MAX_N = 1e3 + 5;
inline int qi() {
  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;
}
inline ll ql() {
  ll f = 0;
  int 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 ans[MAX_N][MAX_N];
char g[MAX_N][MAX_N];
bool vis[MAX_N][MAX_N];
int mov[4][2] = {0, 1, 0, -1, 1, 0, -1, 0};
vector<P> rec;
int h, w;
void solve(int x, int y) {
  queue<P> q;
  q.push(P(x, y));
  while (!q.empty()) {
    P now = q.front();
    q.pop();
    repi(i, 0, 3) {
      int tx = now.fi + mov[i][0], ty = now.se + mov[i][1];
      if (tx < 1 || tx > h || ty < 1 || ty > w || g[tx][ty] == '#' ||
          ans[tx][ty] != -1)
        continue;
      ans[tx][ty] = ans[now.fi][now.se], q.push(P(tx, ty));
    }
  }
}
int main() {
  h = qi(), w = qi();
  memset(ans, -1, sizeof(ans));
  ms(vis);
  int ch = qi(), cw = qi(), dh = qi(), dw = qi();
  repi(i, 1, h) ss(g[i] + 1);
  queue<P> q;
  ans[ch][cw] = 0, solve(ch, cw), q.push(P(ch, cw));
  while (!q.empty()) {
    rec.clear();
    P now = q.front();
    q.pop();
    queue<P> q2;
    q2.push(now), rec.pb(now), vis[now.fi][now.se] = true;
    while (!q2.empty()) {
      P now2 = q2.front();
      q2.pop();
      repi(i, 0, 3) {
        int tx = now2.fi + mov[i][0], ty = now2.se + mov[i][1];
        if (tx < 1 || tx > h || ty < 1 || ty > w || g[tx][ty] == '#' ||
            vis[tx][ty])
          continue;
        q2.push(P(tx, ty)), rec.pb(P(tx, ty)), vis[tx][ty] = true;
      }
    }
    for (auto x : rec) {
      repi(i, -2, 2) repi(j, -2, 2) {
        int tx = x.fi + i, ty = x.se + j;
        if (tx < 1 || tx > h || ty < 1 || ty > w || g[tx][ty] == '#' ||
            ans[tx][ty] != -1)
          continue;
        ans[tx][ty] = ans[x.fi][x.se] + 1, solve(tx, ty), q.push(P(tx, ty));
      }
    }
  }
  printf("%d\\n", ans[dh][dw]);
  return 0;
}

Error: TLE

Fixed submission:

#pragma GCC optimize(2)
#pragma G++ optimize(2)
#include <bits/stdc++.h>
#define si(a) scanf("%d", &a)
#define sl(a) scanf("%lld", &a)
#define sd(a) scanf("%lf", &a)
#define ss(a) scanf("%s", 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 (int i = head[s], v = e[i].to; i; i = e[i].nxt, v = e[i].to)
#define ce(i, r) i == r ? '\\n' : ' '
#define pb push_back
#define all(x) x.begin(), x.end()
#define fi first
#define se second
#define pr(x) cout << #x << ": " << x << endl
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int MAX_N = 1e3 + 5;
inline int qi() {
  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;
}
inline ll ql() {
  ll f = 0;
  int 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 ans[MAX_N][MAX_N];
char g[MAX_N][MAX_N];
int mov[4][2] = {0, 1, 0, -1, 1, 0, -1, 0};
int main() {
  int h = qi(), w = qi();
  memset(ans, -1, sizeof(ans));
  int ch = qi(), cw = qi(), dh = qi(), dw = qi();
  repi(i, 1, h) ss(g[i] + 1);
  queue<P> q;
  ans[ch][cw] = 0;
  q.push(P(ch, cw));
  while (!q.empty()) {
    P now = q.front();
    q.pop();
    repi(i, 0, 3) {
      int tx = now.fi + mov[i][0], ty = now.se + mov[i][1];
      if (tx < 1 || tx > h || ty < 1 || ty > w || g[tx][ty] == '#' ||
          (ans[tx][ty] != -1 && ans[tx][ty] <= ans[now.fi][now.se]))
        continue;
      ans[tx][ty] = ans[now.fi][now.se], q.push(P(tx, ty));
    }
    repi(i, -2, 2) repi(j, -2, 2) {
      int tx = now.fi + i, ty = now.se + j;
      if (tx < 1 || tx > h || ty < 1 || ty > w || g[tx][ty] == '#' ||
          (ans[tx][ty] != -1 && ans[tx][ty] <= (ans[now.fi][now.se] + 1)))
        continue;
      ans[tx][ty] = ans[now.fi][now.se] + 1, q.push(P(tx, ty));
    }
  }
  if (ans[dh][dw] == -1)
    puts("-1");
  else
    printf("%d\\n", ans[dh][dw]);
  return 0;
}