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

#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
#include <algorithm>
#include <array>
#include <bitset>
#include <chrono>
#include <cmath>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <vector>
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define make_unique(x)                                                         \\
  sort(all((x)));                                                              \\
  (x).resize(unique(all((x))) - (x).begin())
typedef long long ll;
typedef long double ld;
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int main() {

  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  int h, w;

  cin >> h >> w;

  int sx, sy, fx, fy;

  cin >> sx >> sy >> fx >> fy;

  sx--;
  sy--;
  fx--;
  fy--;

  vector<string> m(h);

  for (int i = 0; i < h; i++)
    cin >> m[i];

  vector<vector<int>> cost(h, vector<int>(w, 1e9 + 42));

  cost[sx][sy] = 0;

  deque<pair<int, int>> dq;

  dq.push_front(mp(sx, sy));

  int movex[4] = {-1, 0, 0, 1}, movey[4] = {0, -1, 1, 0};

  int x, y;

  while (!dq.empty()) {
    tie(x, y) = dq.front();
    dq.pop_front();
    for (int mm = 0; mm < 4; mm++) {
      int cx = x + movex[mm], cy = y + movey[mm];
      if (cx >= 0 && cx < h && cy >= 0 && cy < w && m[cx][cy] == '.' &&
          cost[cx][cy] > cost[x][y]) {
        cost[cx][cy] = cost[x][y];
        dq.push_front(mp(cx, cy));
      }
    }
    for (int xx = x - 2; xx <= x + 2; xx++)
      for (int yy = y - 2; yy <= y + 2; yy++) {
        if (xx >= 0 && xx < h && yy >= 0 && yy < w && m[xx][yy] == '.' &&
            cost[xx][yy] > cost[x][y] + 1) {
          cost[xx][yy] = cost[x][y] + 1;
          dq.push_back(mp(xx, yy));
        }
      }
  }

  cout << (cost[fx][fy] >= 1e9 ? -1 : cost[fx][fy]);

  return 0;
}
/*

*/


Error: TLE

Fixed submission:

#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
#include <algorithm>
#include <array>
#include <bitset>
#include <chrono>
#include <cmath>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <vector>
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define make_unique(x)                                                         \\
  sort(all((x)));                                                              \\
  (x).resize(unique(all((x))) - (x).begin())
typedef long long ll;
typedef long double ld;
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int main() {

  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  int h, w;

  cin >> h >> w;

  int sx, sy, fx, fy;

  cin >> sx >> sy >> fx >> fy;

  sx--;
  sy--;
  fx--;
  fy--;

  vector<string> m(h);

  for (int i = 0; i < h; i++)
    cin >> m[i];

  vector<vector<int>> cost(h, vector<int>(w, 1e9 + 42));

  cost[sx][sy] = 0;

  deque<pair<int, int>> dq;

  dq.push_front(mp(sx, sy));

  int movex[4] = {-1, 0, 0, 1}, movey[4] = {0, -1, 1, 0};

  int x, y;

  while (!dq.empty()) {
    tie(x, y) = dq.front();
    dq.pop_front();
    for (int mm = 0; mm < 4; mm++) {
      int cx = x + movex[mm], cy = y + movey[mm];
      if (cx >= 0 && cx < h && cy >= 0 && cy < w && m[cx][cy] == '.' &&
          cost[cx][cy] > cost[x][y]) {
        cost[cx][cy] = cost[x][y];
        dq.push_front(mp(cx, cy));
      }
    }
    for (int xx = x - 2; xx <= x + 2; xx++)
      for (int yy = y - 2; yy <= y + 2; yy++) {
        if (xx >= 0 && xx < h && yy >= 0 && yy < w && m[xx][yy] == '.' &&
            cost[xx][yy] > cost[x][y] + 1) {
          cost[xx][yy] = cost[x][y] + 1;
          dq.push_front(mp(xx, yy));
        }
      }
  }

  cout << (cost[fx][fy] >= 1e9 ? -1 : cost[fx][fy]);

  return 0;
}
/*

*/