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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define repl(i, l, r) for (ll i = (l); i < (r); i++)
#define per(i, n) for (ll i = n - 1; i >= 0; i--)
#define perl(i, r, l) for (ll i = r - 1; i >= l; i--)
#define fi first
#define se second
#define pb push_back
#define ins insert
#define pqueue(x) priority_queue<x, vector<x>, greater<x>>
#define all(x) (x).begin(), (x).end()
#define CST(x) cout << fixed << setprecision(x)
#define rev(x) reverse(x);
using ll = long long;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using pl = pair<ll, ll>;
using vpl = vector<pl>;
using vvpl = vector<vpl>;
const ll MOD = 1000000007;
const ll MOD9 = 998244353;
const int inf = 1e9 + 10;
const ll INF = 4e18;
const ll dy[8] = {1, 0, -1, 0, 1, 1, -1, -1};
const ll dx[8] = {0, -1, 0, 1, 1, -1, 1, -1};
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  ll h, w;
  cin >> h >> w;
  ll ch, cw, dh, dw;
  cin >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  vector<string> g(h);
  rep(i, h) cin >> g[i];
  vvl seen(h, vl(w, inf));
  deque<pl> que;
  que.push_front({ch, cw});
  seen[ch][cw] = 0;
  while (!que.empty()) {
    pl p = que.front();
    que.pop_front();
    rep(i, 4) {
      ll ny = p.fi + dy[i], nx = p.se + dx[i];
      if (ny < 0 || nx < 0 || ny >= h || nx >= w)
        continue;
      if (g[ny][nx] == '#')
        continue;
      if (chmin(seen[ny][nx], seen[p.fi][p.se])) {
        que.push_front({ny, nx});
      }
    }
    rep(i, 5) {
      rep(j, 5) {
        ll ny = p.fi - 2 + i, nx = p.se - 2 + j;
        if (ny < 0 || nx < 0 || ny >= h || nx >= w)
          continue;
        if (g[ny][nx] == '#')
          continue;
        if (chmin(seen[ny][nx], seen[p.fi][p.se] + 1)) {
          que.push_back({ny, nx});
        }
      }
    }
  }
  /*rep(i,h){
      rep(j,w){
          cout << seen[i][j]<<" ";
      }
      cout << endl;
  }*/
  if (seen[dh][dw] == inf)
    cout << -1 << endl;
  else
    cout << seen[dh][dw] << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define repl(i, l, r) for (ll i = (l); i < (r); i++)
#define per(i, n) for (ll i = n - 1; i >= 0; i--)
#define perl(i, r, l) for (ll i = r - 1; i >= l; i--)
#define fi first
#define se second
#define pb push_back
#define ins insert
#define pqueue(x) priority_queue<x, vector<x>, greater<x>>
#define all(x) (x).begin(), (x).end()
#define CST(x) cout << fixed << setprecision(x)
#define rev(x) reverse(x);
using ll = long long;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using pl = pair<ll, ll>;
using vpl = vector<pl>;
using vvpl = vector<vpl>;
const ll MOD = 1000000007;
const ll MOD9 = 998244353;
const int inf = 1e9 + 10;
const ll INF = 4e18;
const ll dy[8] = {1, 0, -1, 0, 1, 1, -1, -1};
const ll dx[8] = {0, -1, 0, 1, 1, -1, 1, -1};
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  ll h, w;
  cin >> h >> w;
  ll ch, cw, dh, dw;
  cin >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  vector<string> g(h);
  rep(i, h) cin >> g[i];
  vvl seen(h, vl(w, inf));
  queue<pl> que;
  que.push({ch, cw});
  seen[ch][cw] = 0;
  while (!que.empty()) {
    pl p = que.front();
    que.pop();
    rep(i, 4) {
      ll ny = p.fi + dy[i], nx = p.se + dx[i];
      if (ny < 0 || nx < 0 || ny >= h || nx >= w)
        continue;
      if (g[ny][nx] == '#')
        continue;
      if (chmin(seen[ny][nx], seen[p.fi][p.se])) {
        que.push({ny, nx});
      }
    }
    rep(i, 5) {
      rep(j, 5) {
        ll ny = p.fi - 2 + i, nx = p.se - 2 + j;
        if (ny < 0 || nx < 0 || ny >= h || nx >= w)
          continue;
        if (g[ny][nx] == '#')
          continue;
        if (chmin(seen[ny][nx], seen[p.fi][p.se] + 1)) {
          que.push({ny, nx});
        }
      }
    }
  }
  /*rep(i,h){
      rep(j,w){
          cout << seen[i][j]<<" ";
      }
      cout << endl;
  }*/
  if (seen[dh][dw] == inf)
    cout << -1 << endl;
  else
    cout << seen[dh][dw] << endl;
}