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

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define REPLL(i, n) for (ll i = 0; i < (ll)(n); i++)
using namespace std;
template <class T> inline bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
typedef long long ll;

int H, W;
int INF = 1e9;
vector<string> S;
vector<vector<ll>> d;
vector<vector<bool>> visited;
queue<pair<int, int>> nxt;
vector<int> dh = {1, 0, -1, 0};
vector<int> dw = {0, 1, 0, -1};

void BFS(int h, int w) {
  queue<pair<int, int>> q;
  q.push({h, w});
  visited[h][w] = true;
  nxt.push({h, w});
  while (!q.empty()) {
    auto cur = q.front();
    q.pop();
    int ch = cur.first, cw = cur.second;
    REP(i, 4) {
      if (ch + dh[i] < 0 || ch + dh[i] >= H || cw + dw[i] < 0 ||
          cw + dw[i] >= W)
        continue;
      if (S[ch + dh[i]][cw + dw[i]] == '#')
        continue;
      if (visited[ch + dh[i]][cw + dw[i]])
        continue;
      q.push({ch + dh[i], cw + dw[i]});
      d[ch + dh[i]][cw + dw[i]] = d[ch][cw];
      visited[ch + dh[i]][cw + dw[i]] = true;
      nxt.push({ch + dh[i], cw + dw[i]});
    }
  }
  return;
}

void magic(int h, int w) {
  REP(i, 5) {
    REP(j, 5) {
      if (h - 2 + i < 0 || h - 2 + i >= H || w - 2 + j < 0 || w - 2 + j >= W ||
          (i == 2 && j == 2))
        continue;
      if (S[h - 2 + i][w - 2 + j] == '#')
        continue;
      if (visited[h - 2 + i][w - 2 + j])
        continue;
      nxt.push({h - 2 + i, w - 2 + j});
      d[h - 2 + i][w - 2 + j] = d[h][w] + 1;
      visited[h - 2 + i][w - 2 + j] = true;
    }
  }
  return;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> H >> W;
  int Ch, Cw;
  cin >> Ch >> Cw;
  Ch--;
  Cw--;
  int Dh, Dw;
  cin >> Dh >> Dw;
  Dh--;
  Dw--;
  S.assign(H, "");
  d.assign(H, vector<ll>(W, INF));
  visited.assign(H, vector<bool>(W, false));
  REP(i, H) { cin >> S[i]; }
  d[Ch][Cw] = 0;
  BFS(Ch, Cw);
  if (d[Dh][Dw] == INF) {
    while (!nxt.empty() && d[Dh][Dw] == INF) {
      queue<pair<int, int>> tmp;
      swap(tmp, nxt);
      while (!tmp.empty()) {
        auto cur = tmp.front();
        tmp.pop();
        int cuh = cur.first, cuw = cur.second;
        magic(cuh, cuw);
      }
      swap(tmp, nxt);
      while (!tmp.empty()) {
        auto cur = tmp.front();
        tmp.pop();
        int cuh = cur.first, cuw = cur.second;
        BFS(cuh, cuw);
      }
    }
  }
  if (d[Dh][Dw] != INF) {
    cout << d[Dh][Dw] << endl;
  } else {
    cout << -1 << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define REPLL(i, n) for (ll i = 0; i < (ll)(n); i++)
using namespace std;
template <class T> inline bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
typedef long long ll;

int H, W;
ll INF = 1e18;
vector<string> S;
vector<vector<ll>> d1;
vector<vector<bool>> visited;
vector<int> dx1 = {2,  1,  0, -1, -2, 2,  1,  0, -1, -2, 2,  1,
                   -1, -2, 2, 1,  0,  -1, -2, 2, 1,  0,  -1, -2};
vector<int> dy1 = {2, 2, 2,  2,  2,  1,  1,  1,  1,  1,  0,  0,
                   0, 0, -1, -1, -1, -1, -1, -2, -2, -2, -2, -2};

void bfs1(int w, int h) {
  queue<pair<int, int>> q;
  q.push({w, h});
  d1[w][h] = 0;
  visited[w][h] = true;
  while (!q.empty()) {
    auto cur = q.front();
    q.pop();
    int cx = cur.first, cy = cur.second;
    REP(i, 24) {
      if (cx + dx1[i] < 0 || cx + dx1[i] >= W || cy + dy1[i] < 0 ||
          cy + dy1[i] >= H)
        continue;
      if (S[cy + dy1[i]][cx + dx1[i]] == '#')
        continue;
      if (d1[cx + dx1[i]][cy + dy1[i]] < d1[cx][cy] + 1)
        continue;
      if (visited[cx + dx1[i]][cy + dy1[i]])
        continue;
      q.push({cx + dx1[i], cy + dy1[i]});
      if (i == 7 || i == 11 || i == 12 || i == 16) {
        d1[cx + dx1[i]][cy + dy1[i]] = d1[cx][cy] + 1;
        if (d1[cx + dx1[i]][cy + dy1[i]] < 1e9)
          visited[cx + dx1[i]][cy + dy1[i]] = true;
      } else {
        d1[cx + dx1[i]][cy + dy1[i]] = d1[cx][cy] + (1e9);
      }
    }
  }
  return;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> H >> W;
  int Ch, Cw;
  cin >> Ch >> Cw;
  Ch--;
  Cw--;
  int Dh, Dw;
  cin >> Dh >> Dw;
  Dh--;
  Dw--;
  S.assign(H, "");
  d1.assign(W, vector<ll>(H, INF));
  visited.assign(W, vector<bool>(H, false));
  REP(i, H) { cin >> S[i]; }
  bfs1(Cw, Ch);
  if (d1[Dw][Dh] != INF) {
    ll ans = d1[Dw][Dh] / (1e9);
    cout << ans << endl;
  } else {
    cout << -1 << endl;
  }
}