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

#include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG
#define rep(i, from, to) for (int i = from; i < (to); ++i)
#define mp(x, y) make_pair(x, y)
#define all(x) (x).begin(), (x).end()
#define pb push_back
using ll = long long;
using vin = vector<int>;
using vll = vector<ll>;
using vst = vector<string>;
using P = pair<int, int>;
const int inf = 1e9 + 7;
const ll INF = 1e18;
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chmax(T &a, T b) { a = max(a, b); }
template <class T> inline void Yes(T condition) {
  if (condition)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}
template <class T> inline void YES(T condition) {
  if (condition)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

int dist[1100][1100];

int main() {
  cout << fixed << setprecision(10);
  // bfsしつつchminを5*5にばら撒く
  int h, w;
  cin >> h >> w;
  int ch, cw, dh, dw;
  cin >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  rep(i, 0, 1100) rep(j, 0, 1100) dist[i][j] = inf;
  vst s(h);
  rep(i, 0, h) cin >> s[i];

  deque<P> que;
  que.push_front(mp(ch, cw));
  dist[ch][cw] = 0;
  while (!que.empty()) {
    int nowh = que.front().first;
    int noww = que.front().second;
    que.pop_front();
    int nh, nw;

    rep(i, 0, 4) {
      rep(j, 0, 4) { // A
        nh = nowh + dx[i];
        nw = noww + dy[i];
        if (nh < 0 || nw < 0 || nh >= h || nw >= w)
          continue;
        if (s[nh][nw] == '#')
          continue;
        if (dist[nh][nw] > dist[nowh][noww]) {
          dist[nh][nw] = dist[nowh][noww];
          que.push_front(mp(nh, nw));
        }
      }
    }

    rep(i, -2, 3) {
      rep(j, -2, 3) { // B
        nh = nowh + i;
        nw = noww + j;
        if (nh < 0 || nw < 0 || nh >= h || nw >= w)
          continue;
        if (s[nh][nw] == '#')
          continue;
        if ((i == 1 && j == 0) || (i == -1 && j == 0) || (i == 0 && j == -1) ||
            (i == 0 && j == 1))
          continue;
        if (dist[nh][nw] > dist[nowh][noww] + 1) {
          dist[nh][nw] = dist[nowh][noww] + 1;
          que.push_back(mp(nh, nw));
        } // if
      }   // repj
    }     // repi
  }       // while
  if (dist[dh][dw] == inf)
    cout << -1 << endl;
  else
    cout << dist[dh][dw] << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG
#define rep(i, from, to) for (int i = from; i < (to); ++i)
#define mp(x, y) make_pair(x, y)
#define all(x) (x).begin(), (x).end()
#define pb push_back
using ll = long long;
using vin = vector<int>;
using vll = vector<ll>;
using vst = vector<string>;
using P = pair<int, int>;
const int inf = 1e9 + 7;
const ll INF = 1e18;
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chmax(T &a, T b) { a = max(a, b); }
template <class T> inline void Yes(T condition) {
  if (condition)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}
template <class T> inline void YES(T condition) {
  if (condition)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

int dist[1100][1100];

int main() {
  cout << fixed << setprecision(10);
  // bfsしつつchminを5*5にばら撒く
  int h, w;
  cin >> h >> w;
  int ch, cw, dh, dw;
  cin >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  rep(i, 0, 1100) rep(j, 0, 1100) dist[i][j] = inf;
  vst s(h);
  rep(i, 0, h) cin >> s[i];

  queue<P> que;
  que.push(mp(ch, cw));
  dist[ch][cw] = 0;
  while (!que.empty()) {
    int nowh = que.front().first;
    int noww = que.front().second;
    que.pop();
    int nh, nw;

    rep(i, 0, 4) {
      rep(j, 0, 4) { // A
        nh = nowh + dx[i];
        nw = noww + dy[i];
        if (nh < 0 || nw < 0 || nh >= h || nw >= w)
          continue;
        if (s[nh][nw] == '#')
          continue;
        if (dist[nh][nw] > dist[nowh][noww]) {
          dist[nh][nw] = dist[nowh][noww];
          que.push(mp(nh, nw));
        }
      }
    }

    rep(i, -2, 3) {
      rep(j, -2, 3) { // B
        nh = nowh + i;
        nw = noww + j;
        if (nh < 0 || nw < 0 || nh >= h || nw >= w)
          continue;
        if (s[nh][nw] == '#')
          continue;
        if ((i == 1 && j == 0) || (i == -1 && j == 0) || (i == 0 && j == -1) ||
            (i == 0 && j == 1))
          continue;
        if (dist[nh][nw] > dist[nowh][noww] + 1) {
          dist[nh][nw] = dist[nowh][noww] + 1;
          que.push(mp(nh, nw));
        } // if
      }   // repj
    }     // repi
  }       // while
  if (dist[dh][dw] == inf)
    cout << -1 << endl;
  else
    cout << dist[dh][dw] << endl;
}