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

#pragma region head
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vll = vector<ll>;
using pi = pair<int, int>;
using pll = pair<ll, ll>;
template <class T> using vv = vector<vector<T>>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repi(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define rrepi(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define bit(n) (1LL << (n))
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;
}
const int INF = 1002003004;
const ll LINF = 1002003004005006007ll;
struct preprocess {
  preprocess() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);
  }
} ____;
#pragma endregion head

#pragma region library
#pragma endregion library

int main() {
  int h, w;
  cin >> h >> w;
  int sx, sy;
  cin >> sx >> sy;
  int gx, gy;
  cin >> gx >> gy;
  sx--;
  sy--;
  gx--;
  gy--;
  vv<char> vec(h, vector<char>(w));
  rep(i, h) rep(j, w) cin >> vec[i][j];
  vi dx = {1, 0, -1, 0};
  vi dy = {0, 1, 0, -1};
  vv<int> dist(h, vector<int>(w, INF));
  deque<tuple<int, int, int>> q;
  q.emplace_front(sx, sy, 0);
  dist[sx][sy] = 0;
  vi wx, wy;
  rep(i, 5) rep(j, 5) {
    if (abs(i - 2) + abs(j - 2) < 2)
      continue;
    wx.emplace_back(i - 2);
    wy.emplace_back(j - 2);
  }

  // dist
  while (!q.empty()) {
    int x, y, val;
    tie(x, y, val) = q.front();
    q.pop_front();
    if (dist[x][y] < val)
      continue;
    int cnt = 0;
    rep(i, 4) {
      int nx = x + dx[i], ny = y + dy[i];
      if (nx < 0 || nx >= h)
        continue;
      if (ny < 0 || ny >= w)
        continue;
      if (vec[nx][ny] == '#')
        continue;
      cnt++;
      if (dist[nx][ny] > dist[x][y]) {
        dist[nx][ny] = dist[x][y];
        q.emplace_front(nx, ny, dist[nx][ny]);
      }
    }
    if (cnt == 4)
      continue;
    rep(i, 20) {
      int nx = x + wx[i], ny = y + wy[i];
      if (nx < 0 || nx >= h)
        continue;
      if (ny < 0 || ny >= w)
        continue;
      if (vec[nx][ny] == '#')
        continue;
      if (dist[nx][ny] > dist[x][y] + 1) {
        dist[nx][ny] = dist[x][y] + 1;
        q.emplace_back(nx, ny, dist[nx][ny]);
      }
    }
  }
  if (dist[gx][gy] != INF)
    cout << dist[gx][gy] << '\\n';
  else
    cout << -1 << '\\n';
}


Error: TLE

Fixed submission:

#pragma GCC optimize("Ofast")
#pragma region head
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vll = vector<ll>;
using pi = pair<int, int>;
using pll = pair<ll, ll>;
template <class T> using vv = vector<vector<T>>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repi(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define rrepi(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define bit(n) (1LL << (n))
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;
}
const int INF = 1002003004;
const ll LINF = 1002003004005006007ll;
struct preprocess {
  preprocess() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);
  }
} ____;
#pragma endregion head

#pragma region library
#pragma endregion library

int main() {
  int h, w;
  cin >> h >> w;
  int sx, sy;
  cin >> sx >> sy;
  int gx, gy;
  cin >> gx >> gy;
  sx--;
  sy--;
  gx--;
  gy--;
  vv<char> vec(h, vector<char>(w));
  rep(i, h) rep(j, w) cin >> vec[i][j];
  vi dx = {1, 0, -1, 0};
  vi dy = {0, 1, 0, -1};
  vv<int> dist(h, vector<int>(w, INF));
  queue<tuple<int, int, int>> q;
  q.emplace(sx, sy, 0);
  dist[sx][sy] = 0;
  vi wx, wy;
  rep(i, 5) rep(j, 5) {
    if (abs(i - 2) + abs(j - 2) < 2)
      continue;
    wx.emplace_back(i - 2);
    wy.emplace_back(j - 2);
  }

  // dist
  while (!q.empty()) {
    int x, y, val;
    tie(x, y, val) = q.front();
    q.pop();
    if (dist[x][y] < val)
      continue;
    int cnt = 0;
    rep(i, 4) {
      int nx = x + dx[i], ny = y + dy[i];
      if (nx < 0 || nx >= h)
        continue;
      if (ny < 0 || ny >= w)
        continue;
      if (vec[nx][ny] == '#')
        continue;
      cnt++;
      if (dist[nx][ny] > dist[x][y]) {
        dist[nx][ny] = dist[x][y];
        q.emplace(nx, ny, dist[nx][ny]);
      }
    }
    if (cnt == 4)
      continue;
    rep(i, 20) {
      int nx = x + wx[i], ny = y + wy[i];
      if (nx < 0 || nx >= h)
        continue;
      if (ny < 0 || ny >= w)
        continue;
      if (vec[nx][ny] == '#')
        continue;
      if (dist[nx][ny] > dist[x][y] + 1) {
        dist[nx][ny] = dist[x][y] + 1;
        q.emplace(nx, ny, dist[nx][ny]);
      }
    }
  }
  if (dist[gx][gy] != INF)
    cout << dist[gx][gy] << '\\n';
  else
    cout << -1 << '\\n';
}