Problem p02579 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <chrono>
// #pragma GCC optimize("Ofast")
using namespace std;
#define reps(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) reps(i, 0, n)
#define Rreps(i, n, e) for (int i = n - 1; i >= e; --i)
#define Rrep(i, n) Rreps(i, n, 0)
#define ALL(a) a.begin(), a.end()
#define fi first
#define se second
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;

ll N, M, H, W, Q, K, A, B;
string S;
typedef pair<ll, ll> P;
const ll INF = (1LL << 58);

template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  } else
    return false;
}
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  } else
    return false;
}

int main() {
  cin >> H >> W;
  vec s(2), t(2);
  cin >> s[0] >> s[1] >> t[0] >> t[1];
  rep(i, 2) {
    --s[i];
    --t[i];
  }
  vector<string> G(H);
  rep(i, H) cin >> G[i];
  mat dist(H, vec(W, INF));
  dist[s[0]][s[1]] = 0;
  deque<P> que;
  que.emplace_back(0, s[0] * W + s[1]);
  const vector<int> dh = {-1, 0, 1, 0}, dw = {0, -1, 0, 1};
  while (!que.empty()) {
    P p = que.front();
    que.pop_front();
    int h = p.se / W, w = p.se % W;
    // if(dist[h][w] < p.fi) continue;
    rep(i, 4) {
      int nh = h + dh[i], nw = w + dw[i];
      if (nh >= 0 && nh < H && nw >= 0 && nw < W && G[nh][nw] != '#') {
        if (chmin(dist[nh][nw], p.fi)) {
          que.emplace_front(p.fi, nh * W + nw);
        }
      }
    }
    for (int i = -2; i <= 2; ++i) {
      for (int j = -2; j <= 2; ++j) {
        int nh = h + i, nw = w + j;
        if (nh >= 0 && nh < H && nw >= 0 && nw < W && G[nh][nw] != '#') {
          if (chmin(dist[nh][nw], p.fi + 1)) {
            que.emplace_front(p.fi + 1, nh * W + nw);
          }
        }
      }
    }
  }
  cout << (dist[t[0]][t[1]] == INF ? -1 : dist[t[0]][t[1]]) << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#include <chrono>
// #pragma GCC optimize("Ofast")
using namespace std;
#define reps(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) reps(i, 0, n)
#define Rreps(i, n, e) for (int i = n - 1; i >= e; --i)
#define Rrep(i, n) Rreps(i, n, 0)
#define ALL(a) a.begin(), a.end()
#define fi first
#define se second
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;

ll N, M, H, W, Q, K, A, B;
string S;
typedef pair<ll, ll> P;
const ll INF = (1LL << 58);

template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  } else
    return false;
}
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  } else
    return false;
}

int main() {
  cin >> H >> W;
  vec s(2), t(2);
  cin >> s[0] >> s[1] >> t[0] >> t[1];
  rep(i, 2) {
    --s[i];
    --t[i];
  }
  vector<string> G(H);
  rep(i, H) cin >> G[i];
  mat dist(H, vec(W, INF));
  dist[s[0]][s[1]] = 0;
  deque<P> que;
  que.emplace_back(0, s[0] * W + s[1]);
  const vector<int> dh = {-1, 0, 1, 0}, dw = {0, -1, 0, 1};
  while (!que.empty()) {
    P p = que.front();
    que.pop_front();
    int h = p.se / W, w = p.se % W;
    // if(dist[h][w] < p.fi) continue;
    rep(i, 4) {
      int nh = h + dh[i], nw = w + dw[i];
      if (nh >= 0 && nh < H && nw >= 0 && nw < W && G[nh][nw] != '#') {
        if (chmin(dist[nh][nw], p.fi)) {
          que.emplace_front(p.fi, nh * W + nw);
        }
      }
    }
    for (int i = -2; i <= 2; ++i) {
      for (int j = -2; j <= 2; ++j) {
        int nh = h + i, nw = w + j;
        if (nh >= 0 && nh < H && nw >= 0 && nw < W && G[nh][nw] != '#') {
          if (chmin(dist[nh][nw], p.fi + 1)) {
            que.emplace_back(p.fi + 1, nh * W + nw);
          }
        }
      }
    }
  }
  cout << (dist[t[0]][t[1]] == INF ? -1 : dist[t[0]][t[1]]) << endl;
}