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

#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
template <class T> using V = vector<T>;
#define fi first
#define se second
#define all(v) (v).begin(), (v).end()
const ll inf = (1e18);
// const ll mod=998244353;
const ll mod = 1000000007;
ll GCD(ll a, ll b) { return b ? GCD(b, a % b) : a; }
ll LCM(ll c, ll d) { return c / GCD(c, d) * d; }
struct __INIT {
  __INIT() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(15);
  }
} __init;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
V<int> dy = {-2, -1, 0, 1, 2};
V<int> dd = {-1, 0, 1, 0}, dx = {0, -1, 0, 1};
int main() {
  int h, w;
  cin >> h >> w;
  V<string> s(h);
  int sx, sy;
  cin >> sx >> sy;
  sx--;
  sy--;
  int gx, gy;
  cin >> gx >> gy;
  gx--;
  gy--;
  for (int i = 0; i < h; i++)
    cin >> s[i];
  V<V<ll>> dp(h, V<ll>(w, inf));
  dp[sx][sy] = 0;
  deque<P> q;
  q.emplace_back(sx, sy);
  while (q.size()) {
    int x = q.front().fi, y = q.front().se;
    q.pop_front();
    int ok = true;
    for (int i = 0; i < 4; i++) {
      int nx = x + dd[i], ny = y + dx[i];
      if (nx < 0 || nx >= h || ny < 0 || ny >= w)
        continue;
      if (s[nx][ny] == '#')
        continue;
      if (dp[nx][ny] > dp[x][y]) {
        dp[nx][ny] = dp[x][y];
        q.emplace_front(nx, ny);
      }
    }
    if (ok) {
      for (int i = 0; i < 5; i++) {
        for (int j = 0; j < 5; j++) {
          int nx = x + dy[i], ny = y + dy[j];
          if (nx < 0 || nx >= h || ny < 0 || ny >= w)
            continue;
          if (s[nx][ny] == '#')
            continue;
          if (dp[nx][ny] > dp[x][y] + 1) {
            dp[nx][ny] = dp[x][y] + 1;
            q.emplace_back(nx, ny);
          }
        }
      }
    }
  }
  cout << (dp[gx][gy] >= inf ? -1 : dp[gx][gy]) << "\\n";
}


Error: TLE

Fixed submission:

#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
template <class T> using V = vector<T>;
#define fi first
#define se second
#define all(v) (v).begin(), (v).end()
const ll inf = (1e18);
// const ll mod=998244353;
const ll mod = 1000000007;
ll GCD(ll a, ll b) { return b ? GCD(b, a % b) : a; }
ll LCM(ll c, ll d) { return c / GCD(c, d) * d; }
struct __INIT {
  __INIT() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(15);
  }
} __init;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
V<int> dy = {-2, -1, 0, 1, 2};
V<int> dd = {-1, 0, 1, 0}, dx = {0, -1, 0, 1};
int main() {
  int h, w;
  cin >> h >> w;
  V<string> s(h);
  int sx, sy;
  cin >> sx >> sy;
  sx--;
  sy--;
  int gx, gy;
  cin >> gx >> gy;
  gx--;
  gy--;
  for (int i = 0; i < h; i++)
    cin >> s[i];
  V<V<ll>> dp(h, V<ll>(w, inf));
  dp[sx][sy] = 0;
  queue<P> q;
  q.emplace(sx, sy);
  while (q.size()) {
    int x = q.front().fi, y = q.front().se;
    q.pop();
    int ok = true;
    for (int i = 0; i < 4; i++) {
      int nx = x + dd[i], ny = y + dx[i];
      if (nx < 0 || nx >= h || ny < 0 || ny >= w)
        continue;
      if (s[nx][ny] == '#')
        continue;
      if (dp[nx][ny] > dp[x][y]) {
        dp[nx][ny] = dp[x][y];
        q.emplace(nx, ny);
      }
    }
    if (ok) {
      for (int i = 0; i < 5; i++) {
        for (int j = 0; j < 5; j++) {
          int nx = x + dy[i], ny = y + dy[j];
          if (nx < 0 || nx >= h || ny < 0 || ny >= w)
            continue;
          if (s[nx][ny] == '#')
            continue;
          if (dp[nx][ny] > dp[x][y] + 1) {
            dp[nx][ny] = dp[x][y] + 1;
            q.emplace(nx, ny);
          }
        }
      }
    }
  }
  cout << (dp[gx][gy] >= inf ? -1 : dp[gx][gy]) << "\\n";
}