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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
using pll = pair<ll, ll>;
using Graph = vector<vector<ll>>;
#define all(v) v.begin(), v.end()
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep2(i, l, r) for (ll i = (l); i <= (ll)(r); i++)
#define rep3(i, l, r) for (ll i = (l); i >= (ll)(r); i--)
#define dup(x, y) (((x) + (y)-1) / (y)) // x/yの除算の切り上げ
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const int inf = 1001001001;
const ll INF = 1LL << 60;
const ll mod = 1000000007; // 1000000007
const ld pi = acos(-1);
const int di[] = {-1, 0, 1, 0};
const int dj[] = {0, -1, 0, 1};

int main() {

  ios::sync_with_stdio(false);
  cin.tie(0);

  ll h, w;
  cin >> h >> w;
  ll si, sj, ei, ej;
  cin >> si >> sj >> ei >> ej;
  si--;
  sj--;
  ei--;
  ej--;
  vector<string> s(h);
  rep(i, h) cin >> s[i];
  vector<vector<ll>> dist(h, vector<ll>(w, INF));
  deque<pll> q;
  dist[si][sj] = 0;
  q.emplace_back(si, sj);
  while (!q.empty()) {
    ll i = q.front().first;
    ll j = q.front().second;
    ll d = dist[i][j];
    q.pop_front();
    rep(v, 4) {
      ll ni = (i + di[v]);
      ll nj = (j + dj[v]);
      if (ni < 0 || ni >= h || nj < 0 || nj >= w)
        continue;
      if (s[ni][nj] == '#')
        continue;
      if (dist[ni][nj] <= d)
        continue;
      dist[ni][nj] = d;
      q.emplace_front(ni, nj);
    }
    rep2(ei, -2, 2) {
      rep2(ej, -2, 2) {
        ll ni = (i + ei);
        ll nj = (j + ej);
        if (ni < 0 || ni >= h || nj < 0 || nj >= w)
          continue;
        if (s[ni][nj] == '#')
          continue;
        if (dist[ni][nj] <= (d + 1))
          continue;
        dist[ni][nj] = (d + 1);
        q.emplace_back(ni, nj);
      }
    }
  }

  ll ans = dist[ei][ej];
  if (ans == INF)
    ans = -1;
  cout << ans << '\\n';
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
using pll = pair<ll, ll>;
using Graph = vector<vector<ll>>;
#define all(v) v.begin(), v.end()
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep2(i, l, r) for (ll i = (l); i <= (ll)(r); i++)
#define rep3(i, l, r) for (ll i = (l); i >= (ll)(r); i--)
#define dup(x, y) (((x) + (y)-1) / (y)) // x/yの除算の切り上げ
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const int inf = 1001001001;
const ll INF = 1LL << 60;
const ll mod = 1000000007; // 1000000007
const ld pi = acos(-1);
const int di[] = {-1, 0, 1, 0};
const int dj[] = {0, -1, 0, 1};

int main() {

  ios::sync_with_stdio(false);
  cin.tie(0);

  ll h, w;
  cin >> h >> w;
  ll si, sj, ei, ej;
  cin >> si >> sj >> ei >> ej;
  si--;
  sj--;
  ei--;
  ej--;
  vector<string> s(h);
  rep(i, h) cin >> s[i];
  vector<vector<ll>> dist(h, vector<ll>(w, INF));
  deque<pll> q;
  dist[si][sj] = 0;
  q.emplace_back(si, sj);
  while (!q.empty()) {
    ll i = q.front().first;
    ll j = q.front().second;
    ll d = dist[i][j];
    q.pop_front();
    rep(v, 4) {
      ll ni = (i + di[v]);
      ll nj = (j + dj[v]);
      if (ni < 0 || ni >= h || nj < 0 || nj >= w)
        continue;
      if (s[ni][nj] == '#')
        continue;
      if (dist[ni][nj] <= d)
        continue;
      dist[ni][nj] = d;
      q.emplace_back(ni, nj);
    }
    rep2(ei, -2, 2) {
      rep2(ej, -2, 2) {
        ll ni = (i + ei);
        ll nj = (j + ej);
        if (ni < 0 || ni >= h || nj < 0 || nj >= w)
          continue;
        if (s[ni][nj] == '#')
          continue;
        if (dist[ni][nj] <= (d + 1))
          continue;
        dist[ni][nj] = (d + 1);
        q.emplace_back(ni, nj);
      }
    }
  }

  ll ans = dist[ei][ej];
  if (ans == INF)
    ans = -1;
  cout << ans << '\\n';
}