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

#include <bits/stdc++.h>
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define pb push_back
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
using namespace std;
using ll = long long;
using vi = vector<int>;
using pii = pair<int, int>;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fLL;
ll bpow(ll b, ll p, ll m = 1000000007LL) {
  ll rt = 1;
  for (; p; p >>= 1, b = b * b % m)
    if (p & 1)
      rt = rt * b % m;
  return rt;
}
char g[1005][1005];
int d[1005][1005];
int dy[4] = {-1, 0, 1, 0};
int dx[4] = {0, 1, 0, -1};
struct wtf {
  int dis, rr, cc;
  const bool operator<(const wtf &e) const { return dis > e.dis; }
};
int main() {
  IOS;
  int r, c;
  cin >> r >> c;
  int sr, sc, er, ec;
  cin >> sr >> sc >> er >> ec;
  for (int i = 1; i <= r; i++) {
    for (int j = 1; j <= c; j++) {
      cin >> g[i][j];
      d[i][j] = INF;
    }
  }
  d[sr][sc] = 0;
  priority_queue<wtf> q;
  q.push({0, sr, sc});
  while (!q.empty()) {
    int rr = q.top().rr, cc = q.top().cc;
    q.pop();
    for (int k = 0; k < 4; k++) {
      int nr = rr + dy[k], nc = cc + dx[k];
      if (nr < 1 || nc < 1 || nr > r || nc > c || g[nr][nc] != '.')
        continue;
      if (d[nr][nc] > d[rr][cc])
        d[nr][nc] = d[rr][cc], q.push({d[nr][nc], nr, nc});
    }
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        int nr = rr + i, nc = cc + j;
        if (nr < 1 || nc < 1 || nr > r || nc > c || g[nr][nc] != '.')
          continue;
        if (d[nr][nc] > d[rr][cc] + 1)
          d[nr][nc] = d[rr][cc] + 1, q.push({d[nr][nc], nr, nc});
      }
    }
  }
  cout << (d[er][ec] == INF ? -1 : d[er][ec]) << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define pb push_back
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
using namespace std;
using ll = long long;
using vi = vector<int>;
using pii = pair<int, int>;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fLL;
ll bpow(ll b, ll p, ll m = 1000000007LL) {
  ll rt = 1;
  for (; p; p >>= 1, b = b * b % m)
    if (p & 1)
      rt = rt * b % m;
  return rt;
}
char g[1005][1005];
int d[1005][1005];
int dy[4] = {-1, 0, 1, 0};
int dx[4] = {0, 1, 0, -1};
int main() {
  IOS;
  int r, c;
  cin >> r >> c;
  int sr, sc, er, ec;
  cin >> sr >> sc >> er >> ec;
  for (int i = 1; i <= r; i++) {
    for (int j = 1; j <= c; j++) {
      cin >> g[i][j];
      d[i][j] = INF;
    }
  }
  d[sr][sc] = 0;
  queue<pii> q;
  q.push({sr, sc});
  while (!q.empty()) {
    int rr = q.front().ff, cc = q.front().ss;
    q.pop();
    for (int k = 0; k < 4; k++) {
      int nr = rr + dy[k], nc = cc + dx[k];
      if (nr < 1 || nc < 1 || nr > r || nc > c || g[nr][nc] != '.')
        continue;
      if (d[nr][nc] > d[rr][cc])
        d[nr][nc] = d[rr][cc], q.push({nr, nc});
    }
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        int nr = rr + i, nc = cc + j;
        if (nr < 1 || nc < 1 || nr > r || nc > c || g[nr][nc] != '.')
          continue;
        if (d[nr][nc] > d[rr][cc] + 1)
          d[nr][nc] = d[rr][cc] + 1, q.push({nr, nc});
      }
    }
  }

  cout << (d[er][ec] == INF ? -1 : d[er][ec]) << endl;
}