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

#include <bits/stdc++.h>
#include <chrono>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

#define ordered_set                                                            \\
  tree<int, null_type, less<int>, rb_tree_tag,                                 \\
       tree_order_statistics_node_update>
// order_of_key (val): returns the no. of values less than val
// find_by_order (k): returns the kth largest element.(0-based)

#define ll long long int
#define ld long double
#define ff first
#define ss second
#define pb push_back
#define pi pair<ll, ll>
#define pii pair<ll, pi>
#define ppi pair<pi, ll>
#define ppp pair<pi, pi>
#define ex(str, a, b)                                                          \\
  str.substr((int)(a), min((int)str.size() - a, (int)(b - a + 1)))
#define all(X) X.begin(), X.end()
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

// for pair comparison function(ascending order) use return (i1.ff < i2.ff);

/* string operations :
   str.substr (x,y) : returns a substring str[x],str[x+1],...str[x+y-1]
   __builtin_popcount(n) : no. of set bits in n.
*/

const int M = (1 << 20) + 5;
const int md = 1e9 + 7;

priority_queue<ll, vector<ll>, greater<ll>> pq;

ll pwr(ll a, ll n, ll m) {
  ll p = 1;
  while (n > 0) {
    if (n % 2 == 1)
      p = (p * a) % m;
    a = (a * a) % m;
    n = n / 2;
  }
  return p;
}

int d[1004][1004];
string str[1004];
bool mrk[1004][1004];
priority_queue<pii, vector<pii>, greater<pii>> q;

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

  int n, m, x, y, i, j, sx, sy, tx, ty;

  cin >> n >> m;
  cin >> sx >> sy;
  cin >> tx >> ty;
  sx--;
  sy--;
  tx--;
  ty--;

  for (i = 0; i < n; ++i)
    cin >> str[i];

  for (i = 0; i < n; ++i)
    for (j = 0; j < m; ++j)
      d[i][j] = 1e9;

  d[sx][sy] = 0;
  q.push(pii(0, pi(sx, sy)));

  while (!(q.empty())) {
    pii ed = q.top();
    x = ed.ss.ff;
    y = ed.ss.ss;
    q.pop();
    if (mrk[x][y] == 1)
      continue;
    // cout << x << " " << y << " " << d[x][y] << endl;
    mrk[x][y] = 1;

    for (i = -1; i <= 1; ++i) {
      for (j = -1; j <= 1; ++j) {
        if (abs(i) == abs(j))
          continue;
        sx = x + i;
        sy = y + j;
        if (sx < 0 || sx >= n || sy < 0 || sy >= m)
          continue;
        if (str[sx][sy] == '#')
          continue;
        if (d[sx][sy] > d[x][y]) {
          d[sx][sy] = d[x][y];
          q.push(pii(d[sx][sy], pi(sx, sy)));
        }
      }
    }

    for (i = -2; i <= 2; ++i) {
      for (j = -2; j <= 2; ++j) {
        sx = x + i;
        sy = y + j;
        if (sx < 0 || sx >= n || sy < 0 || sy >= m)
          continue;
        // cout << sx << " " << sy << endl;
        if (str[sx][sy] == '#')
          continue;
        if (d[sx][sy] > d[x][y] + 1) {
          d[sx][sy] = d[x][y] + 1;
          q.push(pii(d[sx][sy], pi(sx, sy)));
        }
      }
    }
  }

  if (d[tx][ty] > 1e8)
    cout << -1 << "\\n";
  else
    cout << d[tx][ty] << "\\n";
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define fi first
#define se second
// #pragma GCC optimize "trapv"
char a[1000 + 10][1000 + 10];
int vi[1000 + 10][1000 + 10];
int ch, cw, dh, dw;
int h, w;
bool mrk[1004][1004];
void bfs(int x, int y) {
  queue<pair<int, int>> q;
  q.push({x, y});
  vi[x][y] = 0;
  while (!q.empty()) {
    int x1 = q.front().fi;
    int y1 = q.front().se;
    // cout<<x1+1<<" "<<y1+1<<"\\n";
    q.pop();
    // if(mrk[x1][y1] == 1)
    // 	continue;
    // cout << x << " " << y << " " << d[x][y] << endl;
    // mrk[x1][y1] = 1;
    for (int i = -1; i <= 1; i++) {
      for (int j = -1; j <= 1; j++) {
        if (abs(i) == abs(j))
          continue;
        int x2 = x1 + i;
        int y2 = y1 + j;
        if (x2 < 0 || x2 >= h || y2 < 0 || y2 >= w)
          continue;

        if (a[x2][y2] == '#')
          continue;

        if (vi[x2][y2] > vi[x1][y1]) {
          q.push({x2, y2});
          vi[x2][y2] = vi[x1][y1];
        }
      }
    }

    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        int x2 = x1 + i;
        int y2 = y1 + j;
        if (x2 < 0 || x2 >= h || y2 < 0 || y2 >= w)
          continue;

        if (a[x2][y2] == '#')
          continue;

        if (vi[x2][y2] > vi[x1][y1] + 1) {
          q.push({x2, y2});
          vi[x2][y2] = vi[x1][y1] + 1;
        }
      }
    }
  }
}

int main() {
  // ios_base::sync_with_stdio(false);
  // cin.tie(NULL);
  cin >> h >> w;
  cin >> ch >> cw;
  cin >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  // memset(vi,-1,sizeof(vi));
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      vi[i][j] = 1e9;
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++) {
      cin >> a[i][j];
    }

  bfs(ch, cw);
  if (vi[dh][dw] < 1e9)
    cout << vi[dh][dw] << "\\n";
  else
    cout << "-1\\n";
}