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

#pragma GCC optimize("Ofast", "unroll-loops", "omit-frame-pointer",            \\
                     "inline") // Optimization flags
#pragma GCC option("arch=native", "tune=native", "no-zero-upper") // Enable AVX
#pragma GCC target("avx2")                                        // Enable AVX

#include <bits/stdc++.h>

using namespace std;

#define db(x) cout << (x) << '\\n';
#define all(x) x.begin(), x.end()
#define UNIQUE(x) sort(all(x)), x.resize(unique(all(x)) - x.begin())
#define LB(v, x) lower_bound(all(v), x)
#define UB(v, x) upper_bound(all(v), x)
#define endl "\\n"
#define F first
#define S second
#define PB push_back
#define PF push_front
#define int long long
#define double long double
#define trick int m = (l + r) >> 1, lc = n << 1, rc = lc | 1

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vii;

int dy[] = {0, 1, 0, -1};
int dx[] = {-1, 0, 1, 0};
const int maxn = 1e5;
const int OO = 1e18;
const int mod = 1e9 + 7;
const double PI = acos(-1);

void maxself(int &a, int b) { a = max(a, b); }
void minself(int &a, int b) { a = min(a, b); }
int n, m;
bool ok(int x, int y) {
  if (x >= 0 and x < n and y >= 0 and y < m)
    return 1;
  else
    return 0;
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  int N, M, sx, sy, ex, ey;
  cin >> N >> M >> sx >> sy >> ex >> ey;
  n = N, m = M;
  sx--, sy--, ex--, ey--;

  vector<vector<char>> v(N, vector<char>(M));

  for (int i = 0; i < N; i++)
    for (int j = 0; j < M; j++)
      cin >> v[i][j];

  vector<vector<int>> dist(N, vector<int>(M, OO));
  dist[sx][sy] = 0;

  priority_queue<pair<int, pii>, vector<pair<int, pii>>,
                 greater<pair<int, pii>>>
      pq;
  pq.push({0, {sx, sy}});

  while (pq.size()) {
    int cost = pq.top().F;
    int x = pq.top().S.F;
    int y = pq.top().S.S;

    pq.pop();

    if (cost != dist[x][y])
      continue;

    for (int i = 0; i < 4; i++) {
      int nx = x + dx[i];
      int ny = y + dy[i];

      if (ok(nx, ny) and dist[nx][ny] > dist[x][y] and v[nx][ny] == '.') {
        dist[nx][ny] = dist[x][y];
        pq.push({dist[nx][ny], {nx, ny}});
      }
    }

    for (int i = -2; i <= 2; i++)
      for (int j = -2; j <= 2; j++) {
        int nx = x + i;
        int ny = y + j;

        if (ok(nx, ny) and dist[nx][ny] > dist[x][y] + 1 and v[nx][ny] == '.') {
          dist[nx][ny] = dist[x][y] + 1;
          pq.push({dist[nx][ny], {nx, ny}});
        }
      }
  }

  if (dist[ex][ey] != OO)
    cout << dist[ex][ey];
  else
    cout << -1;

  return 0;
}


Error: TLE

Fixed submission:

#pragma GCC optimize("Ofast", "unroll-loops", "omit-frame-pointer",            \\
                     "inline") // Optimization flags
#pragma GCC option("arch=native", "tune=native", "no-zero-upper") // Enable AVX
#pragma GCC target("avx2")                                        // Enable AVX

#include <bits/stdc++.h>

using namespace std;

#define db(x) cout << (x) << '\\n';
#define all(x) x.begin(), x.end()
#define UNIQUE(x) sort(all(x)), x.resize(unique(all(x)) - x.begin())
#define LB(v, x) lower_bound(all(v), x)
#define UB(v, x) upper_bound(all(v), x)
#define endl "\\n"
#define F first
#define S second
#define PB push_back
#define PF push_front
#define int long long
#define double long double
#define trick int m = (l + r) >> 1, lc = n << 1, rc = lc | 1

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vii;

int dy[] = {0, 1, 0, -1};
int dx[] = {-1, 0, 1, 0};
const int maxn = 1e5;
const int OO = 1e18;
const int mod = 1e9 + 7;
const double PI = acos(-1);

void maxself(int &a, int b) { a = max(a, b); }
void minself(int &a, int b) { a = min(a, b); }
int n, m;
bool ok(int x, int y) {
  if (x >= 0 and x < n and y >= 0 and y < m)
    return 1;
  else
    return 0;
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  int N, M, sx, sy, ex, ey;
  cin >> N >> M >> sx >> sy >> ex >> ey;
  n = N, m = M;
  sx--, sy--, ex--, ey--;

  vector<vector<char>> v(N, vector<char>(M));

  for (int i = 0; i < N; i++)
    for (int j = 0; j < M; j++)
      cin >> v[i][j];

  vector<vector<int>> dist(N, vector<int>(M, OO));
  dist[sx][sy] = 0;

  priority_queue<pair<int, pii>> pq;
  pq.push({0, {sx, sy}});

  while (pq.size()) {
    int cost = pq.top().F;
    int x = pq.top().S.F;
    int y = pq.top().S.S;

    pq.pop();

    if (cost != dist[x][y])
      continue;

    for (int i = 0; i < 4; i++) {
      int nx = x + dx[i];
      int ny = y + dy[i];

      if (ok(nx, ny) and dist[nx][ny] > dist[x][y] and v[nx][ny] == '.') {
        dist[nx][ny] = dist[x][y];
        pq.push({dist[nx][ny], {nx, ny}});
      }
    }

    for (int i = -2; i <= 2; i++)
      for (int j = -2; j <= 2; j++) {
        int nx = x + i;
        int ny = y + j;

        if (ok(nx, ny) and dist[nx][ny] > dist[x][y] + 1 and v[nx][ny] == '.') {
          dist[nx][ny] = dist[x][y] + 1;
          pq.push({dist[nx][ny], {nx, ny}});
        }
      }
  }

  if (dist[ex][ey] != OO)
    cout << dist[ex][ey];
  else
    cout << -1;

  return 0;
}