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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

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 ll llINF = 1LL << 60;
const int iINF = 1e9;

//---main---------------------------------------------
int main() {
  // main
  int H, W, Ch, Cw, Dh, Dw;
  cin >> H >> W >> Ch >> Cw >> Dh >> Dw;
  Ch--;
  Cw--;
  Dh--;
  Dw--;
  vector<string> S(H);
  rep(i, H) cin >> S[i];

  vector<vector<int>> cmap(H, vector<int>(W, iINF));
  deque<P> q;
  cmap[Ch][Cw] = 0;
  q.emplace_back(Ch, Cw);

  int h0, w0, h1, w1, c;
  int hdir[] = {0, 1, 0, -1};
  int wdir[] = {1, 0, -1, 0};
  while (!q.empty()) {
    h0 = q.front().first;
    w0 = q.front().second;
    c = cmap[h0][w0];
    q.pop_front();

    // if(c != cmap[h0][w0]) continue;

    rep(diri, 4) {
      h1 = h0 + hdir[diri];
      w1 = w0 + wdir[diri];
      if (h1 < 0 || h1 >= H || w1 < 0 || w1 >= W)
        continue;
      if (S[h1][w1] == '#')
        continue;
      if (cmap[h1][w1] <= c)
        continue;
      cmap[h1][w1] = c;
      q.emplace_front(h1, w1);
    }

    for (int dh = -2; dh <= 2; dh++) {
      for (int dw = -2; dw <= 2; dw++) {
        h1 = h0 + dh;
        w1 = w0 + dw;
        if (h1 < 0 || h1 >= H || w1 < 0 || w1 >= W)
          continue;
        if (S[h1][w1] == '#')
          continue;
        if (cmap[h1][w1] <= c + 1)
          continue;
        cmap[h1][w1] = c + 1;
        q.emplace_back(h1, w1);
      }
    }
  }

  int ans = cmap[Dh][Dw];
  if (ans == iINF)
    ans = -1;
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

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 ll llINF = 1LL << 60;
const int iINF = 1e8;

int hdir[] = {0, 1, 0, -1};
int wdir[] = {1, 0, -1, 0};

//---main---------------------------------------------
int main() {
  // main
  int H, W, Ch, Cw, Dh, Dw;
  cin >> H >> W >> Ch >> Cw >> Dh >> Dw;
  Ch--;
  Cw--;
  Dh--;
  Dw--;
  vector<string> S(H);
  rep(i, H) cin >> S[i];

  vector<vector<int>> cmap(H, vector<int>(W, iINF));
  deque<P> q;
  cmap[Ch][Cw] = 0;
  q.emplace_back(Ch, Cw);

  int h0, w0, h1, w1, c;
  while (!q.empty()) {
    h0 = q.front().first;
    w0 = q.front().second;
    c = cmap[h0][w0];
    q.pop_front();

    // if(c != cmap[h0][w0]) continue;

    rep(diri, 4) {
      h1 = h0 + hdir[diri];
      w1 = w0 + wdir[diri];
      if (h1 < 0 || h1 >= H || w1 < 0 || w1 >= W)
        continue;
      if (S[h1][w1] == '#')
        continue;
      if (cmap[h1][w1] <= c)
        continue;
      cmap[h1][w1] = c;
      q.emplace_back(h1, w1);
    }

    for (int dh = -2; dh <= 2; dh++) {
      for (int dw = -2; dw <= 2; dw++) {
        h1 = h0 + dh;
        w1 = w0 + dw;
        if (h1 < 0 || h1 >= H || w1 < 0 || w1 >= W)
          continue;
        if (S[h1][w1] == '#')
          continue;
        if (cmap[h1][w1] <= c + 1)
          continue;
        cmap[h1][w1] = c + 1;
        q.emplace_back(h1, w1);
      }
    }
  }

  int ans = cmap[Dh][Dw];
  if (ans == iINF)
    ans = -1;
  cout << ans << endl;
  return 0;
}