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

#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
#define ll long long
#define rep(i, n) for (int i = 0; i < int(n); i++)
#define vi vector<int>
#define INF (1 << 30) - 1
using namespace std;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};
template <class T> inline bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  cin.tie(0), ios::sync_with_stdio(false);
  int h, w;
  cin >> h >> w;
  int ch, cw, dh, dw;
  cin >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  vector<string> s(h);
  rep(i, h) cin >> s[i];

  vector<vi> cnt(h, vi(w, INF));
  deque<pair<int, int>> q;
  q.push_front({ch, cw});
  cnt[ch][cw] = 0;
  while (!q.empty()) {
    pair<int, int> p = q.front();
    q.pop_front();
    int x = p.second, y = p.first;
    rep(i, 4) {
      if (x + dx[i] < 0 || w <= x + dx[i] || y + dy[i] < 0 || h <= y + dy[i])
        continue;
      if (s[y + dy[i]][x + dx[i]] == '.') {
        if (chmin(cnt[y + dy[i]][x + dx[i]], cnt[y][x]))
          q.push_front({y + dy[i], x + dx[i]});
      }
    }
    for (int ddx = -2; ddx <= 2; ddx++) {
      for (int ddy = -2; ddy <= 2; ddy++) {
        if (x + ddx < 0 || w <= x + ddx || y + ddy < 0 || h <= y + ddy)
          continue;
        if (s[y + ddy][x + ddx] == '.') {
          if (chmin(cnt[y + ddy][x + ddx], cnt[y][x] + 1))
            q.push_back({y + ddy, x + ddx});
        }
      }
    }
  }
  if (cnt[dh][dw] == INF)
    cout << -1;
  else
    cout << cnt[dh][dw];
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
#define ll long long
#define rep(i, n) for (int i = 0; i < int(n); i++)
#define vi vector<int>
#define INF (1 << 30) - 1
using namespace std;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};
template <class T> inline bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  cin.tie(0), ios::sync_with_stdio(false);
  int h, w;
  cin >> h >> w;
  int ch, cw, dh, dw;
  cin >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  vector<string> s(h);
  rep(i, h) cin >> s[i];

  vector<vi> cnt(h, vi(w, INF));
  queue<pair<int, int>> q;
  q.push({ch, cw});
  cnt[ch][cw] = 0;
  while (!q.empty()) {
    pair<int, int> p = q.front();
    q.pop();
    int x = p.second, y = p.first;
    rep(i, 4) {
      if (x + dx[i] < 0 || w <= x + dx[i] || y + dy[i] < 0 || h <= y + dy[i])
        continue;
      if (s[y + dy[i]][x + dx[i]] == '.') {
        if (chmin(cnt[y + dy[i]][x + dx[i]], cnt[y][x]))
          q.push({y + dy[i], x + dx[i]});
      }
    }
    for (int ddx = -2; ddx <= 2; ddx++) {
      for (int ddy = -2; ddy <= 2; ddy++) {
        if (x + ddx < 0 || w <= x + ddx || y + ddy < 0 || h <= y + ddy)
          continue;
        if (s[y + ddy][x + ddx] == '.') {
          if (chmin(cnt[y + ddy][x + ddx], cnt[y][x] + 1))
            q.push({y + ddy, x + ddx});
        }
      }
    }
  }
  if (cnt[dh][dw] == INF)
    cout << -1;
  else
    cout << cnt[dh][dw];
  return 0;
}