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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef long ll;

typedef long double ld;
typedef pair<ll, ll> pp;
typedef tree<pp, null_type, less<pp>, rb_tree_tag,
             tree_order_statistics_node_update>
    super_set;

#define x1 dfds
#define y1 dsfdsfe
#define pb push_back
#define forn(i, n) for (ll i = 1; i <= n; ++i)
#define fi first
#define sc second
#define endl '\\n'
#define po(x) (1ll << x)
#define log sdfdsfdsf

const ll DIM = 1E3 + 7;
const ll MOD = 1e9 + 7;
const ll M1 = 1e9 + 7;
const ll INF = 1E17;
const ld eps = 0.00000000000001;
const ld PI = 3.14159265358979323846;
int getInt(int a = 0, int b = INT_MAX) {
  static mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  return uniform_int_distribution<int>(a, b)(rng);
}

ll gi(char ch) {
  if (ch == '#')
    return 1;
  return 0;
}
ll dist[DIM][DIM];
ll A[DIM][DIM];
ll n, m;
deque<pp> Q;
void Check(ll x, ll y, ll ds) {
  if (x < 1 || x > n || y < 1 || y > m || A[x][y] == 1)
    return;
  if (dist[x][y] == -1 || dist[x][y] > ds) {
    dist[x][y] = ds;
    if (Q.empty() || ds == dist[Q.front().fi][Q.front().sc])
      Q.push_front({x, y});
    else
      Q.push_back({x, y});
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  cin >> n >> m;
  pp start;
  cin >> start.fi >> start.sc;
  pp finish;
  cin >> finish.fi >> finish.sc;
  forn(i, n) {
    forn(j, m) {
      char ch;
      cin >> ch;
      A[i][j] = gi(ch);
    }
  }
  memset(dist, -1, sizeof(dist));
  dist[start.fi][start.sc] = 0;

  Q.pb(start);
  while (!Q.empty()) {
    pp t = Q.front();
    // if (t==finish)break;
    Q.pop_front();
    Check(t.fi, t.sc - 1, dist[t.fi][t.sc]);
    Check(t.fi, t.sc + 1, dist[t.fi][t.sc]);
    Check(t.fi - 1, t.sc, dist[t.fi][t.sc]);
    Check(t.fi + 1, t.sc, dist[t.fi][t.sc]);
    for (ll i = -2; i <= 2; ++i) {
      for (ll j = -2; j <= 2; ++j) {
        Check(t.fi + i, t.sc + j, dist[t.fi][t.sc] + 1);
      }
    }
  }
  cout << dist[finish.fi][finish.sc] << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef long ll;
typedef long double ld;
typedef pair<ll, ll> pp;
typedef tree<pp, null_type, less<pp>, rb_tree_tag,
             tree_order_statistics_node_update>
    super_set;

#define x1 dfds
#define y1 dsfdsfe
#define pb push_back
#define forn(i, n) for (ll i = 1; i <= n; ++i)
#define fi first
#define sc second
#define endl '\\n'
#define po(x) (1ll << x)
#define log sdfdsfdsf

const ll DIM = 1E3 + 7;
const ll MOD = 1e9 + 7;
const ll M1 = 1e9 + 7;
const ll INF = 1E17;
const ld eps = 0.00000000000001;
const ld PI = 3.14159265358979323846;
int getInt(int a = 0, int b = INT_MAX) {
  static mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  return uniform_int_distribution<int>(a, b)(rng);
}

ll gi(char ch) {
  if (ch == '#')
    return 1;
  return 0;
}
ll dist[DIM][DIM], A[DIM][DIM];
ll n, m;
deque<pp> Q;
void Check(ll x, ll y, ll ds) {
  if (x < 1 || x > n || y < 1 || y > m || A[x][y] == 1)
    return;
  if (dist[x][y] == -1 || dist[x][y] > ds) {
    dist[x][y] = ds;
    if (Q.empty() || ds == Q.front().fi)
      Q.push_front({x, y});
    else
      Q.push_back({x, y});
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  cin >> n >> m;
  pp start;
  cin >> start.fi >> start.sc;
  pp finish;
  cin >> finish.fi >> finish.sc;
  forn(i, n) {
    forn(j, m) {
      char ch;
      cin >> ch;
      A[i][j] = gi(ch);
    }
  }
  memset(dist, -1, sizeof(dist));
  dist[start.fi][start.sc] = 0;

  Q.pb(start);
  while (!Q.empty()) {
    pp t = Q.front();
    Q.pop_front();
    Check(t.fi, t.sc - 1, dist[t.fi][t.sc]);
    Check(t.fi, t.sc + 1, dist[t.fi][t.sc]);
    Check(t.fi - 1, t.sc, dist[t.fi][t.sc]);
    Check(t.fi + 1, t.sc, dist[t.fi][t.sc]);
    for (ll i = -2; i <= 2; ++i) {
      for (ll j = -2; j <= 2; ++j) {
        Check(t.fi + i, t.sc + j, dist[t.fi][t.sc] + 1);
      }
    }
  }
  cout << dist[finish.fi][finish.sc] << endl;

  return 0;
}