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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
typedef long long ll;
#define ALL(v) v.begin(), v.end()
#define vmax(v) *max_element(ALL(v))
#define vmin(v) *min_element(ALL(v))
#define chmax(x, a) x = max(x, a)
#define chmin(x, a) x = min(x, a)
#define INF 99999999

int main() {
  int H, W;
  cin >> H >> W;
  pair<int, int> C, D;
  cin >> C.first >> C.second >> D.first >> D.second;
  C.first++;
  C.second++;
  D.first++;
  D.second++;
  vector<vector<char>> S(H + 4, (vector<char>(W + 4, '#')));
  char tmp;
  /*rep(i, 2) {
      rep(j, W+4) {
          S[i][j] = '#';
          S[H+3-i][j] = '#';
      }
  }
  rep(j, 2) {
      rep(i, H+4) {
          S[i][j] = '#';
          S[i][W+3-j] = '#';
      }
  }*/
  // rep(i, H)rep(j, W) cin >> S[i+2][j+2];
  scanf("%c", &tmp);
  rep(i, H) {
    rep(j, W) { scanf("%c", &S[i + 2][j + 2]); }
    scanf("%c", &tmp);
  }
  vector<vector<int>> check(H + 4, vector<int>(W + 4, INF));
  check[C.first][C.second] = 0;
  deque<pair<int, int>> q;
  q.push_back(C);
  while (!q.empty()) {
    int x = q.front().first, y = q.front().second;
    q.pop_front();
    // cout << x << " " << y << endl;
    if (x == D.first && y == D.second)
      continue;
    if (check[x][y] >= check[D.first][D.second])
      continue;
    for (int i = x - 2; i <= x + 2; i++) {
      for (int j = y - 2; j <= y + 2; j++) {
        if (S[i][j] == '#')
          continue;
        if (abs(i - x) + abs(j - y) == 0)
          continue;
        if (abs(i - x) + abs(j - y) == 1) {
          if (check[i][j] > check[x][y]) {
            check[i][j] = check[x][y];
            q.push_front(make_pair(i, j));
          }
          continue;
        }
        if (check[i][j] > check[x][y] + 1) {
          check[i][j] = check[x][y] + 1;
          q.push_back(make_pair(i, j));
        }
      }
    }
  }
  if (check[D.first][D.second] < INF)
    cout << check[D.first][D.second] << endl;
  else
    cout << -1 << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
typedef long long ll;
#define ALL(v) v.begin(), v.end()
#define vmax(v) *max_element(ALL(v))
#define vmin(v) *min_element(ALL(v))
#define chmax(x, a) x = max(x, a)
#define chmin(x, a) x = min(x, a)
#define INF 99999999

int main() {
  int H, W;
  cin >> H >> W;
  pair<int, int> C, D;
  cin >> C.first >> C.second >> D.first >> D.second;
  C.first++;
  C.second++;
  D.first++;
  D.second++;
  vector<vector<char>> S(H + 4, (vector<char>(W + 4, '#')));
  char tmp;
  /*rep(i, 2) {
      rep(j, W+4) {
          S[i][j] = '#';
          S[H+3-i][j] = '#';
      }
  }
  rep(j, 2) {
      rep(i, H+4) {
          S[i][j] = '#';
          S[i][W+3-j] = '#';
      }
  }*/
  // rep(i, H)rep(j, W) cin >> S[i+2][j+2];
  scanf("%c", &tmp);
  rep(i, H) {
    rep(j, W) { scanf("%c", &S[i + 2][j + 2]); }
    scanf("%c", &tmp);
  }
  vector<vector<int>> check(H + 4, vector<int>(W + 4, INF));
  check[C.first][C.second] = 0;
  queue<pair<int, int>> q;
  q.push(C);
  while (!q.empty()) {
    int x = q.front().first, y = q.front().second;
    q.pop();
    // cout << x << " " << y << endl;
    if (x == D.first && y == D.second)
      continue;
    if (check[x][y] >= check[D.first][D.second])
      continue;
    for (int i = x - 2; i <= x + 2; i++) {
      for (int j = y - 2; j <= y + 2; j++) {
        if (S[i][j] == '#')
          continue;
        if (abs(i - x) + abs(j - y) == 0)
          continue;
        if (abs(i - x) + abs(j - y) == 1) {
          if (check[i][j] > check[x][y]) {
            check[i][j] = check[x][y];
            q.push(make_pair(i, j));
          }
          continue;
        }
        if (check[i][j] > check[x][y] + 1) {
          check[i][j] = check[x][y] + 1;
          q.push(make_pair(i, j));
        }
      }
    }
  }
  if (check[D.first][D.second] < INF)
    cout << check[D.first][D.second] << endl;
  else
    cout << -1 << endl;
}