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

#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

#include <deque>
#include <iostream>
#include <vector>

using namespace std;
using i32 = int_fast32_t;
using i64 = int_fast64_t;
#define rep(i, n) for (i32 i = 0; i < (i32)(n); i++)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
using P = pair<i64, i64>;
i64 h, w, ch, cw, dh, dw;
i64 dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
vector<string> s(3001);
vector<vector<i32>> cost(3001, vector<i32>(3001, 1e9));

void bfs() {
  deque<P> dq;
  dq.push_front(P(ch, cw));
  cost[ch][cw] = 0;
  while (dq.size()) {
    P now = dq.front();
    dq.pop_front();
    rep(i, 4) {
      i64 ny = now.first + dy[i];
      i64 nx = now.second + dx[i];
      if (ny >= 0 && ny < h && nx >= 0 && nx < w && s[ny][nx] == '.') {
        if (cost[ny][nx] > cost[now.first][now.second]) {
          cost[ny][nx] = cost[now.first][now.second];
          dq.push_front(P(ny, nx));
        }
      }
      for (i64 i = -2; i <= 2; i++) {
        for (i64 j = -2; j <= 2; j++) {
          if (i == j and i == 0)
            continue;
          i64 ny = now.first + i;
          i64 nx = now.second + j;
          if (ny >= 0 && ny < h && nx >= 0 && nx < w && s[ny][nx] == '.') {
            if (cost[ny][nx] > cost[now.first][now.second] + 1) {
              cost[ny][nx] = cost[now.first][now.second] + 1;
              dq.push_back(P(ny, nx));
            }
          }
        }
      }
    }
  }
  return;
}

int main() {
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  cin >> h >> w >> ch >> cw >> dh >> dw;
  rep(i, h) cin >> s[i];
  ch--;
  cw--;
  dh--;
  dw--;
  bfs();
  cout << (cost[dh][dw] == 1e9 ? -1 : cost[dh][dw]) << endl;
}

Error: TLE

Fixed submission:

#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <iostream>
#include <set>
#include <string>
#include <vector>

using namespace std;
using i32 = int_fast32_t;
using i64 = int_fast64_t;
#define rep(i, n) for (i32 i = 0; i < (i32)(n); i++)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
using P = pair<i64, i64>;
i64 h, w, ans = 1e9;
i64 ch, cw, dh, dw;
i64 dx[4] = {0, -1, 0, 1};
i64 dy[4] = {1, 0, -1, 0};
i64 flag = 0;
vector<vector<bool>> seen(3001, vector<bool>(3001, false));

void dfs(set<P> st, vector<string> &s, i64 nowy, i64 nowx, i64 cnt) {

  if (nowy == dh && nowx == dw) {
    ans = min(ans, cnt);
    return;
  }
  if (st.count(P(nowy, nowx)))
    return;
  st.insert(P(nowy, nowx));
  rep(i, 4) {
    i64 nx = nowx + dx[i];
    i64 ny = nowy + dy[i];
    if (nx >= 0 && ny >= 0 && nx < w && ny < h && s[ny][nx] != '#')
      dfs(st, s, ny, nx, cnt);
    if (nx >= 0 && ny >= 0 && nx < w && ny < h && s[ny][nx] == '#') {
      for (i64 j = -2; j <= 2; j++) {
        for (i64 k = -2; k <= 2; k++) {
          i64 sx = nowx + k;
          i64 sy = nowy + j;
          if (sx >= 0 && sx < w && sy >= 0 && sy < h && s[sy][sx] != '#') {
            dfs(st, s, sy, sx, cnt + 1);
          }
        }
      }
    }
  }
  return;
}
int main() {
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  cin >> h >> w;
  cin >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  vector<string> s(h);
  rep(i, h) cin >> s[i];
  set<P> st;
  dfs(st, s, ch, cw, 0);
  if (ans == 1e9)
    cout << -1 << endl;
  else if (ans == 0)
    cout << 0 << endl;
  else
    cout << ans << endl;
}