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

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

#define ll long long
#define int long long
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define vi vector<int>
#define pqb priority_queue<int>
#define pqs priority_queue<int, vi, greater<int>>
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define mod 1000000007
#define inf (int)(1e18)
#define ps(x, y) fixed << setprecision(y) << x
#define w(x)                                                                   \\
  int x;                                                                       \\
  cin >> x;                                                                    \\
  while (x--)
#define rep(i, begin, end)                                                     \\
  for (__typeof(end) i = (begin) - ((begin) > (end));                          \\
       i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define all(p) p.begin(), p.end()
#define ub(a, b) upper_bound(all(a), b)
#define lb(a, b) lower_bound(all(a), b)
#define sz(x) (int)x.size()
#define PI 3.14159265
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

void run() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
const int N = 1010;
vector<string> a;
int n, m;
bool check(int x, int y) {
  return (x >= 0 and y >= 0 and x < n and y < m and a[x][y] != '#');
}
void solution() {
  cin >> n >> m;
  a.resize(n);
  int cx, cy, dx, dy;
  cin >> cx >> cy >> dx >> dy;
  cx--, cy--, dx--, dy--;
  rep(i, 0, n) { cin >> a[i]; }
  if (a[cx][cy] == '#' or a[dx][dy] == '#') {
    cout << "-1" << endl;
    return;
  }
  vector<pair<int, int>> dir = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}};
  vector<vector<int>> dp(n, vector<int>(m, inf));
  dp[cx][cy] = 0;
  deque<pair<int, int>> q;
  q.push_front({cx, cy});
  int mi = 0;
  while (q.size()) {
    int x = q.front().ff;
    int y = q.front().ss;
    q.pop_front();
    mi = max(mi, dp[x][y]);
    for (auto ele : dir) {
      int x1 = x + ele.ff;
      int y1 = y + ele.ss;
      if (check(x1, y1) and (dp[x1][y1] > dp[x][y])) {
        dp[x1][y1] = dp[x][y];
        q.push_front({x1, y1});
      }
    }
    for (int i = x - 2; i <= x + 2; i++) {
      for (int j = y - 2; j <= y + 2; j++) {
        if (check(i, j) and (dp[i][j] > dp[x][y] + 1)) {
          dp[i][j] = dp[x][y] + 1;
          q.push_back({i, j});
        }
      }
    }
    if (dp[dx][dy] == mi) {
      break;
    }
  }
  if (dp[dx][dy] != inf) {
    cout << dp[dx][dy] << endl;
  } else {
    cout << "-1" << endl;
  }
  return;
}
int32_t main() {
  run();
  solution();
  return 0;
}

Error: TLE

Fixed submission:

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

#define ll long long
#define int long long
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define vi vector<int>
#define pqb priority_queue<int>
#define pqs priority_queue<int, vi, greater<int>>
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define mod 1000000007
#define inf (int)(1e18)
#define ps(x, y) fixed << setprecision(y) << x
#define w(x)                                                                   \\
  int x;                                                                       \\
  cin >> x;                                                                    \\
  while (x--)
#define rep(i, begin, end)                                                     \\
  for (__typeof(end) i = (begin) - ((begin) > (end));                          \\
       i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define all(p) p.begin(), p.end()
#define ub(a, b) upper_bound(all(a), b)
#define lb(a, b) lower_bound(all(a), b)
#define sz(x) (int)x.size()
#define PI 3.14159265
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

void run() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
const int N = 1010;
vector<string> a;
int n, m;
bool check(int x, int y) {
  return (x >= 0 and y >= 0 and x < n and y < m and a[x][y] != '#');
}
void solution() {
  cin >> n >> m;
  a.resize(n);
  int cx, cy, dx, dy;
  cin >> cx >> cy >> dx >> dy;
  cx--, cy--, dx--, dy--;
  rep(i, 0, n) { cin >> a[i]; }
  if (a[cx][cy] == '#' or a[dx][dy] == '#') {
    cout << "-1" << endl;
    return;
  }
  vector<pair<int, int>> dir = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}};
  vector<vector<int>> dp(n, vector<int>(m, inf));
  dp[cx][cy] = 0;
  queue<pair<int, int>> q;
  q.push({cx, cy});
  while (q.size()) {
    int x = q.front().ff;
    int y = q.front().ss;
    q.pop();
    for (auto ele : dir) {
      int x1 = x + ele.ff;
      int y1 = y + ele.ss;
      if (check(x1, y1) and (dp[x1][y1] > dp[x][y])) {
        dp[x1][y1] = dp[x][y];
        q.push({x1, y1});
      }
    }
    for (int i = x - 2; i <= x + 2; i++) {
      for (int j = y - 2; j <= y + 2; j++) {
        if (check(i, j) and (dp[i][j] > dp[x][y] + 1)) {
          dp[i][j] = dp[x][y] + 1;
          q.push({i, j});
        }
      }
    }
  }
  if (dp[dx][dy] != inf) {
    cout << dp[dx][dy] << endl;
  } else {
    cout << "-1" << endl;
  }
  return;
}
int32_t main() {
  run();
  solution();
  return 0;
}