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

#include <bits/stdc++.h>
#include <math.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = (n - 1); i >= 0; i--)
using namespace std;
typedef long long ll;
const int MOD = 1000000007;
const ll INF = 1LL << 60;
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;
}
using Graph = vector<vector<int>>;

const int INFINI = 100000000;

int main() {

  int H, W;
  cin >> H >> W;

  int ch, cw;
  cin >> ch >> cw;
  ch--;
  cw--;
  int dh, dw;
  cin >> dh >> dw;
  dh--;
  dw--;

  vector<string> c(H);
  rep(i, H) cin >> c[i];

  deque<pair<int, int>> que;
  que.emplace_back(ch, cw);

  vector<vector<int>> counter(H, vector<int>(W, INFINI));
  counter[ch][cw] = 0;

  int dx[4] = {0, 1, 0, -1};
  int dy[4] = {1, 0, -1, 0};

  while (!que.empty()) {
    auto vertex = que.front();
    que.pop_front();
    int y = vertex.first;
    int x = vertex.second;

    rep(i, 4) {
      int ny = y + dy[i];
      int nx = x + dx[i];
      if (ny < 0 || nx < 0 || ny >= H || nx >= W)
        continue;
      if (c[ny][nx] != '#') {
        if (counter[ny][nx] > counter[y][x]) {
          counter[ny][nx] = counter[y][x];
          que.emplace_front(ny, nx);
        }
      }
    }

    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        if (i == 0 && j == 0)
          continue;
        int max_move = max(abs(i), abs(j));
        int min_move = min(abs(i), abs(j));
        if (max_move == 1 && min_move == 0)
          continue;
        int ny = y + i;
        int nx = x + j;
        if (ny < 0 || nx < 0 || ny >= H || nx >= W)
          continue;
        if (c[ny][nx] != '#') {
          if (counter[ny][nx] > counter[y][x] + 1) {
            counter[ny][nx] = counter[y][x] + 1;
            que.emplace_back(ny, nx);
          }
        }
      }
    }
  }

  if (counter[dh][dw] == INFINI)
    cout << -1 << endl;
  else
    cout << counter[dh][dw] << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <math.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = (n - 1); i >= 0; i--)
using namespace std;
typedef long long ll;
const int MOD = 1000000007;
const ll INF = 1LL << 60;
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;
}
using Graph = vector<vector<int>>;

const int IN = 100000000;
int main() {
  int H, W;
  cin >> H >> W;
  int C1, C2;
  cin >> C1 >> C2;
  int D1, D2;
  cin >> D1 >> D2;
  C1--;
  C2--;
  D1--;
  D2--;
  vector<string> S(H);
  rep(i, H) { cin >> S[i]; }

  int dx[4] = {0, 1, 0, -1};
  int dy[4] = {1, 0, -1, 0};

  queue<pair<int, int>> que;
  vector<vector<int>> dist(H, vector<int>(W, IN));
  dist[C1][C2] = 0;
  que.push(make_pair(C1, C2));

  int ans = 0;
  while (!que.empty()) {
    auto v = que.front();
    que.pop();
    int y = v.first;
    int x = v.second;
    // cout << y << " " << x << endl;
    rep(i, 4) {
      int ny = y + dy[i];
      int nx = x + dx[i];
      int add = 0;
      if (ny < 0 || nx < 0 || ny >= H || nx >= W)
        continue;
      if (S[ny][nx] != '#') {
        if (dist[ny][nx] > dist[y][x] + add) {
          dist[ny][nx] = dist[y][x] + add;
          que.push(make_pair(ny, nx));
        }
      } else {

        for (int i = -2; i <= 2; i++) {
          for (int j = -2; j <= 2; j++) {
            ny = y + i;
            nx = x + j;
            int add = 1;
            if (i == 0 && j == 0)
              continue;
            // cout << ny << " " << nx << endl;
            if (ny < 0 || nx < 0 || ny >= H || nx >= W)
              continue;
            if (S[ny][nx] == '#')
              continue;
            if (abs(i) <= 1 && abs(j) <= 1 && !(abs(i) == 1 && abs(j) == 1))
              add = 0;
            if (dist[ny][nx] > dist[y][x] + add) {
              dist[ny][nx] = dist[y][x] + add;
              que.push(make_pair(ny, nx));
            }
            /*
            if (dist[ny][nx] ==-1){
                //cout << dist[ny][nx] << endl;
                dist[ny][nx] = dist[y][x]+1;
                //cout << y << " " << x << endl;
                //cout << ny << " " << nx << endl;
                que.push(make_pair(ny,nx));
            }
            */
          }
        }
      }
    }
  }
  if (dist[D1][D2] == IN)
    cout << -1 << endl;
  else
    cout << dist[D1][D2] << endl;
}