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

#include "algorithm"
#include "bitset"
#include "cassert"
#include "climits"
#include "cmath"
#include "cstdio"
#include "ctime"
#include "functional"
#include "iomanip"
#include "iostream"
#include "list"
#include "map"
#include "numeric"
#include "queue"
#include "random"
#include "set"
#include "stack"
#include "string"
#include "unordered_map"
#include "unordered_set"

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, n, m, L, R;
long long int N, M, K, T, n, m, L, R;

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

  cin >> n >> m;
  int sy, sx, ny, nx;
  cin >> sy >> sx >> ny >> nx;
  sy--, sx--, ny--, nx--;
  vector<string> s(n);
  for (auto &i : s)
    cin >> i;
  vector<vector<int>> dis(n, vector<int>(m, 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 >= n || nx >= m)continue;
      // if (s[ny][nx] == '#')continue;
      if (unsigned(nx) < m && unsigned(ny) < n && s[ny][nx] == '.') {
        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 >= n || nx >= m)continue;
        // if (s[ny][nx] == '#')continue;
        if (unsigned(nx) < m && unsigned(ny) < n && s[ny][nx] == '.') {
          if (dis[ny][nx] > dis[cy][cx] + 1) {
            dis[ny][nx] = dis[cy][cx] + 1;
            q.push_back({ny, nx});
          }
        }
      }
    }
  }
  if (dis[ny][nx] == MOD)
    dis[ny][nx] = -1;
  cout << dis[ny][nx] << endl;
}


Error: TLE

Fixed submission:

#include "algorithm"
#include "bitset"
#include "cassert"
#include "climits"
#include "cmath"
#include "cstdio"
#include "ctime"
#include "functional"
#include "iomanip"
#include "iostream"
#include "list"
#include "map"
#include "numeric"
#include "queue"
#include "random"
#include "set"
#include "stack"
#include "string"
#include "unordered_map"
#include "unordered_set"

#define all(x) (x).begin(), (x).end()
#define sz(x) int(x.size())
#define pb push_back

using namespace std;
using ll = long long;
using pii = pair<int, int>;
template <typename... T> void rd(T &...args) { ((cin >> args), ...); }
template <typename... T> void wr(T... args) {
  ((cout << args << " "), ...);
  cout << endl;
}

// const vector<pii> dir{{1,0},{-1,0},{0,1},{0,-1}};
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  ll n, m;
  cin >> n >> m;
  vector<string> a(n);
  int sx, sy, ex, ey;
  cin >> sx >> sy >> ex >> ey;
  sx--, sy--, ex--, ey--;
  for (auto &it : a)
    cin >> it;
  deque<pair<int, int>> q;
  const int N = 1e9;
  vector<vector<int>> dis(n, vector<int>(m, N));
  dis[sx][sy] = 0;
  q.emplace_back(sx, sy);
  int dir[] = {1, 0, -1, 0, 1};
  while (!q.empty()) {
    int x, y;
    tie(x, y) = q.front();
    q.pop_front();
    for (int i = 0; i < 4; i++) {
      int nx = x + dir[i], ny = y + dir[i + 1];
      if (unsigned(nx) < n && unsigned(ny) < m && a[nx][ny] == '.') {
        if (dis[x][y] < dis[nx][ny]) {
          dis[nx][ny] = dis[x][y];
          q.pb({nx, ny});
        }
      }
    }
    for (int dx = -2; dx <= 2; dx++) {
      for (int dy = -2; dy <= 2; dy++) {
        int nx = x + dx, ny = y + dy;
        if (unsigned(nx) < n && unsigned(ny) < m && a[nx][ny] == '.') {
          if (dis[x][y] + 1 < dis[nx][ny]) {
            dis[nx][ny] = dis[x][y] + 1;
            q.pb({nx, ny});
          }
        }
      }
    }
  }
  if (dis[ex][ey] == N)
    dis[ex][ey] = -1;
  cout << dis[ex][ey];
  return 0;
}