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

/*
    -ensure correct output format
    -ensure printing required output
    -reread the problem statement
  */

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> pll;
typedef pair<ll, pair<ll, ll>> plll;
#define fastread() (ios_base::sync_with_stdio(false), cin.tie(NULL));
#define vll(v) v.begin(), v.end()
#define all(x) x.rbegin(), x.rend()
#define min3(a, b, c) min(a, min(b, c))
#define max3(a, b, c) max(a, max(b, c))
#define F first
#define S second
#define in freopen("input.txt", "r", stdin)
#define out freopen("output.txt", "w", stdout)
#define minheap int, vector<int>, greater<int>
#define pb push_back
#define eb emplace_back
#define ischar(x) (('a' <= x && x <= 'z') || ('A' <= x && x <= 'Z'))
#define isvowel(ch)                                                            \\
  ((ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') ||          \\
   (ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U'))
#define bug cout << "BUG" << endl;
const int Max = 2e6 + 10;
const int Mod = 1e9 + 7;
const double PI = 3.141592653589793238463;
bool compare(const pair<ll, ll> &a, const pair<ll, ll> &b) {
  return (a.first > b.first);
}
ll lcm(ll a, ll b) {
  if (a == 0 || b == 0)
    return 0;

  return a / __gcd(a, b) * b;
}

void input(ll ara[], ll n) {
  for (ll i = 0; i < n; i++)
    cin >> ara[i];
}
void print(ll ara[], ll n) {
  for (ll i = 0; i < n; i++)
    cout << ara[i] << " ";
  cout << endl;
}

char ara[1002][1002];

ll dis[1002][1002];

ll r[] = {1, 0, 0, -1};

ll c[] = {0, 1, -1, 0};

int main() {

  fastread();

  ll i, j, n, m, p, a, sum = 0, k, t, b, d, cnt = 0, l;

  bool flag = false;

  string str;

  cin >> n >> m;

  pll s, e;

  cin >> s.F >> s.S >> e.F >> e.S;

  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      cin >> ara[i][j];

      dis[i][j] = 1e9;
    }
  }

  priority_queue<plll, vector<plll>, greater<plll>> q;

  q.push({0, {s.F, s.S}});
  dis[s.F][s.S] = 0;

  while (!q.empty()) {
    auto ps = q.top();

    auto u = ps.S;

    d = dis[u.F][u.S];

    q.pop();

    if (u.F == e.F && u.S == e.S)
      continue;

    for (ll p = 0; p < 4; p++) {
      i = u.F + r[p];
      j = u.S + c[p];

      if (i > 0 && j > 0 && i <= n && j <= m && ara[i][j] == '.' &&
          d < dis[i][j]) {
        dis[i][j] = d;
        q.push({d, {i, j}});
      }
    }

    for (i = u.F - 2; i <= u.F + 2; i++) {
      for (j = u.S - 2; j <= u.S + 2; j++) {
        if (i == u.F && j == u.S)
          continue;

        if (i > 0 && j > 0 && i <= n && j <= m && ara[i][j] == '.' &&
            d + 1 < dis[i][j]) {
          dis[i][j] = d + 1;
          q.push({d + 1, {i, j}});
        }
      }
    }
  }

  if (dis[e.F][e.S] == 1e9)
    dis[e.F][e.S] = -1;

  cout << dis[e.F][e.S] << endl;
}


Error: TLE

Fixed submission:

/*
    -ensure correct output format
    -ensure printing required output
    -reread the problem statement
  */

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> pll;
typedef pair<ll, pair<ll, ll>> plll;
#define fastread() (ios_base::sync_with_stdio(false), cin.tie(NULL));
#define vll(v) v.begin(), v.end()
#define all(x) x.rbegin(), x.rend()
#define min3(a, b, c) min(a, min(b, c))
#define max3(a, b, c) max(a, max(b, c))
#define F first
#define S second
#define in freopen("input.txt", "r", stdin)
#define out freopen("output.txt", "w", stdout)
#define minheap int, vector<int>, greater<int>
#define pb push_back
#define eb emplace_back
#define ischar(x) (('a' <= x && x <= 'z') || ('A' <= x && x <= 'Z'))
#define isvowel(ch)                                                            \\
  ((ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') ||          \\
   (ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U'))
#define bug cout << "BUG" << endl;
const int Max = 2e6 + 10;
const int Mod = 1e9 + 7;
const double PI = 3.141592653589793238463;
bool compare(const pair<ll, ll> &a, const pair<ll, ll> &b) {
  return (a.first > b.first);
}
ll lcm(ll a, ll b) {
  if (a == 0 || b == 0)
    return 0;

  return a / __gcd(a, b) * b;
}

void input(ll ara[], ll n) {
  for (ll i = 0; i < n; i++)
    cin >> ara[i];
}
void print(ll ara[], ll n) {
  for (ll i = 0; i < n; i++)
    cout << ara[i] << " ";
  cout << endl;
}

char ara[1002][1002];

ll dis[1002][1002];

ll r[] = {1, 0, 0, -1};

ll c[] = {0, 1, -1, 0};

int main() {

  fastread();

  ll i, j, n, m, p, a, sum = 0, k, t, b, d, cnt = 0, l;

  bool flag = false;

  string str;

  cin >> n >> m;

  pll s, e;

  cin >> s.F >> s.S >> e.F >> e.S;

  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      cin >> ara[i][j];

      dis[i][j] = 1e9;
    }
  }

  queue<pll> q;

  q.push({s.F, s.S});
  dis[s.F][s.S] = 0;

  while (!q.empty()) {
    auto u = q.front();

    d = dis[u.F][u.S];

    q.pop();

    if (u.F == e.F && u.S == e.S)
      continue;

    for (ll p = 0; p < 4; p++) {
      i = u.F + r[p];
      j = u.S + c[p];

      if (i > 0 && j > 0 && i <= n && j <= m && ara[i][j] == '.' &&
          d < dis[i][j]) {
        dis[i][j] = d;
        q.push({i, j});
      }
    }

    for (i = u.F - 2; i <= u.F + 2; i++) {
      for (j = u.S - 2; j <= u.S + 2; j++) {
        if (i == u.F && j == u.S)
          continue;

        if (i > 0 && j > 0 && i <= n && j <= m && ara[i][j] == '.' &&
            d + 1 < dis[i][j]) {
          dis[i][j] = d + 1;
          q.push({i, j});
        }
      }
    }
  }

  if (dis[e.F][e.S] == 1e9)
    dis[e.F][e.S] = -1;

  cout << dis[e.F][e.S] << endl;
}