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

#include <bits/stdc++.h>
using namespace std;

template <class T> using V = vector<T>;
template <class T> using VV = V<V<T>>;
using pii = pair<int, int>;

#define rep(i, n) rep2(i, 0, n)
#define rep2(i, m, n) for (int i = m; i < n; i++)
#define ef emplace_front
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second

const int INF = 1 << 30;

int main() {
  int H, W, C_h, C_w, D_h, D_w;
  cin >> H >> W >> C_h >> C_w >> D_h >> D_w;
  C_h--;
  C_w--;
  D_h--;
  D_w--;
  VV<char> S(H, V<char>(W));
  rep(i, H) { rep(j, W) cin >> S.at(i).at(j); }

  VV<int> dis(H, V<int>(W, INF));
  deque<pii> update;
  V<int> h_list = {-1, 0, 1, 0}, w_list = {0, -1, 0, 1};

  dis.at(C_h).at(C_w) = 0;
  update.eb(mp(C_h, C_w));

  while (!update.empty()) {
    pii p = update.front();
    update.pop_front();
    int pf = p.fi, ps = p.se;
    int d = dis.at(pf).at(ps);
    int h, w;
    rep(i, 4) {
      h = pf + h_list.at(i);
      w = ps + w_list.at(i);
      if (0 <= h && h < H && 0 <= w && w < W && S.at(h).at(w) == '.' &&
          dis.at(h).at(w) > d) {
        dis.at(h).at(w) = d;
        update.ef(mp(h, w));
      }
    }

    d++;
    rep2(i, -2, 3) {
      rep2(j, -2, 3) {
        h = pf + i;
        w = ps + j;
        if (0 <= h && h < H && 0 <= w && w < W && S.at(h).at(w) == '.' &&
            dis.at(h).at(w) > d) {
          dis.at(h).at(w) = d;
          update.eb(mp(h, w));
        }
      }
    }
  }
  if (dis.at(D_h).at(D_w) == INF)
    cout << -1 << endl;
  else
    cout << dis.at(D_h).at(D_w) << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

template <class T> using V = vector<T>;
template <class T> using VV = V<V<T>>;
using pii = pair<int, int>;

#define rep(i, n) rep2(i, 0, n)
#define rep2(i, m, n) for (int i = m; i < n; i++)
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second

const int INF = 1 << 30;

int main() {
  int H, W, C_h, C_w, D_h, D_w;
  cin >> H >> W >> C_h >> C_w >> D_h >> D_w;
  C_h--;
  C_w--;
  D_h--;
  D_w--;
  VV<char> S(H, V<char>(W));
  rep(i, H) { rep(j, W) cin >> S.at(i).at(j); }

  VV<int> dis(H, V<int>(W, INF));
  deque<pii> update;
  V<int> h_list = {-1, 0, 1, 0}, w_list = {0, -1, 0, 1};

  dis.at(C_h).at(C_w) = 0;
  update.eb(mp(C_h, C_w));

  while (!update.empty()) {
    pii p = update.front();
    update.pop_front();
    int pf = p.fi, ps = p.se;
    int d = dis.at(pf).at(ps);
    int h, w;
    rep(i, 4) {
      h = pf + h_list.at(i);
      w = ps + w_list.at(i);
      if (0 <= h && h < H && 0 <= w && w < W && S.at(h).at(w) == '.' &&
          dis.at(h).at(w) > d) {
        dis.at(h).at(w) = d;
        update.eb(mp(h, w));
      }
    }

    d++;
    rep2(i, -2, 3) {
      rep2(j, -2, 3) {
        h = pf + i;
        w = ps + j;

        if (0 > h || h >= H || 0 > w || w >= W)
          continue;
        if (S.at(h).at(w) == '#')
          continue;
        if (dis.at(h).at(w) <= d)
          continue;

        dis.at(h).at(w) = d;
        update.eb(mp(h, w));
      }
    }
  }
  if (dis.at(D_h).at(D_w) == INF)
    cout << -1 << endl;
  else
    cout << dis.at(D_h).at(D_w) << endl;
}