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

#include "bits/stdc++.h"

using namespace std;

constexpr long long int MOD = 1000000007;
// constexpr int MOD = 1000000007;
// constexpr int MOD = 998244353;
// constexpr long long int MOD = 998244353;
constexpr double EPS = 1e-12;

// int N, M, K, T, H, W, L, R;
long long int N, M, K, T, H, W, L, R;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  cin >> H >> W;
  int sy, sx, gy, gx;
  cin >> sy >> sx >> gy >> gx;
  sy--, sx--, gy--, gx--;
  vector<string> s(H);
  for (auto &i : s)
    cin >> i;
  vector<vector<int>> dis(H, vector<int>(W, MOD));
  deque<pair<int, int>> Q;
  Q.push_back({sy, sx});
  dis[sy][sx] = 0;
  int dir[] = {1, 0, -1, 0, 1};
  while (!Q.empty()) {
    int cy, cx;
    tie(cy, cx) = Q.front();
    Q.pop_front();
    for (int i = 0; i < 4; i++) {
      int ny = cy + dir[i];
      int nx = cx + dir[i + 1];
      if (ny < 0 || nx < 0 || ny >= H || nx >= W)
        continue;
      if (s[ny][nx] == '#')
        continue;
      if (dis[ny][nx] > dis[cy][cx]) {
        dis[ny][nx] = dis[cy][cx];
        Q.push_front({ny, nx});
      }
    }
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        int ny = cy + i;
        int nx = cx + j;
        if (ny < 0 || nx < 0 || ny >= H || nx >= W)
          continue;
        if (s[ny][nx] == '#')
          continue;
        if (dis[ny][nx] > dis[cy][cx] + 1) {
          dis[ny][nx] = dis[cy][cx] + 1;
          Q.push_back({ny, nx});
        }
      }
    }
  }
  if (dis[gy][gx] == MOD)
    dis[gy][gx] = -1;
  cout << dis[gy][gx] << endl;
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
using namespace std;

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef vector<ll> vll;
typedef vector<vector<int>> vvi;
#define all(c) (c).begin(), (c).end()
#define sz(x) (int)(x).size()
#define ar array

#define pb push_back
#define vt vector
#define F first
#define S second

template <class T> bool umin(T &a, const T &b) { return b < a ? a = b, 1 : 0; }
template <class T> bool umax(T &a, const T &b) { return b > a ? a = b, 1 : 0; }

// no change
const ll MOD = 1e9 + 7;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int h, w;
  cin >> h >> w;
  int sx, sy, ex, ey;
  cin >> sx >> sy >> ex >> ey;
  sx--, sy--, ex--, ey--;

  vt<string> G(h);
  for (int i = 0; i < h; i++)
    cin >> G[i];
  vvi dis(h, vi(w, MOD));
  dis.reserve(1000 * 1000);
  dis[sx][sy] = 0;
  deque<ii> q;
  q.pb({sx, sy});
  vi dir = {1, 0, -1, 0, 1};
  while (!q.empty()) {
    int a, b;
    tie(a, b) = q.front();
    q.pop_front();
    for (int i = 0; i < 4; i++) {
      int ny = a + dir[i];
      int nc = b + dir[i + 1];

      if (ny < 0 || ny >= h || nc < 0 || nc >= w)
        continue;
      if (G[ny][nc] == '#')
        continue;
      if (dis[ny][nc] > dis[a][b]) {
        dis[ny][nc] = dis[a][b];
        q.pb({ny, nc});
      }
    }
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        int ny = a + i;
        int nc = b + j;

        if (ny < 0 || ny >= h || nc < 0 || nc >= w)
          continue;
        if (G[ny][nc] == '#')
          continue;
        if (dis[ny][nc] > dis[a][b] + 1) {
          dis[ny][nc] = dis[a][b] + 1;
          q.pb({ny, nc});
        }
      }
    }
  }

  if (dis[ex][ey] == MOD)
    dis[ex][ey] = -1;
  cout << dis[ex][ey] << endl;
}

/*///////////////////////////////////////////////////////////
Important Notes :
* check long long
* check base cases
* do not loose Hope
* if case fails on 2nd test case maybe some cases are missing check them
* do not sit idle
* try to do it using pen/paper.
*/