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

#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimization("unroll-loops")
#pragma GCC target("avx,avx2,fma")
using namespace std;
using ll = long long;
int dy[24] = {-2, -2, -2, -2, -2, -1, -1, -1, -1, -1, 0, 0,
              0,  0,  1,  1,  1,  1,  1,  2,  2,  2,  2, 2},
    dx[24] = {-2, -1, 0,  1,  2, -2, -1, 0,  1,  2, -2, -1,
              1,  2,  -2, -1, 0, 1,  2,  -2, -1, 0, 1,  2},
    n, m, sy, sx, ey, ex, gx[4] = {0, -1, 0, 1}, gy[4] = {1, 0, -1, 0};
char _map[1010][1010];
priority_queue<int, vector<int>, greater<int>> pq[1010][1010];
struct pos {
  int y, x, cnt;
  bool operator<(const pos &a) const { return cnt > a.cnt; }
};
priority_queue<pos> q;
bool in(int a, int b) { return 1 <= a && a <= n && 1 <= b && b <= m; }
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m >> sy >> sx >> ey >> ex;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      cin >> _map[i][j];
  pq[sy][sx].push(0);
  q.push({sy, sx, 0});
  while (q.size()) {
    auto [y, x, cnt] = q.top();
    q.pop();
    if (pq[y][x].top() < cnt)
      continue;
    if (y == ey && x == ex) {
      cout << cnt;
      return 0;
    }
    for (int i = 0; i < 4; i++) {
      int ny = y + gy[i], nx = x + gx[i];
      if (!in(ny, nx) || _map[ny][nx] == '#')
        continue;
      if (pq[ny][nx].empty()) {
        pq[ny][nx].push(cnt);
        q.push({ny, nx, cnt});
      } else if (pq[ny][nx].top() > cnt) {
        pq[ny][nx].pop();
        pq[ny][nx].push(cnt);
        q.push({ny, nx, cnt});
      }
    }
    for (int i = 0; i < 24; i++) {
      int ny = y + dy[i], nx = x + dx[i];
      if (!in(ny, nx) || _map[ny][nx] == '#')
        continue;
      if (pq[ny][nx].empty()) {
        pq[ny][nx].push(cnt + 1);
        q.push({ny, nx, cnt + 1});
      } else if (pq[ny][nx].top() > cnt) {
        pq[ny][nx].pop();
        pq[ny][nx].push(cnt + 1);
        q.push({ny, nx, cnt + 1});
      }
    }
  }
  cout << -1;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimization("unroll-loops")
#pragma GCC target("avx,avx2,fma")
using namespace std;
using ll = long long;
int dy[24] = {-2, -2, -2, -2, -2, -1, -1, -1, -1, -1, 0, 0,
              0,  0,  1,  1,  1,  1,  1,  2,  2,  2,  2, 2},
    dx[24] = {-2, -1, 0,  1,  2, -2, -1, 0,  1,  2, -2, -1,
              1,  2,  -2, -1, 0, 1,  2,  -2, -1, 0, 1,  2},
    n, m, sy, sx, ey, ex, gx[4] = {0, -1, 0, 1}, gy[4] = {1, 0, -1, 0};
char _map[1010][1010];
priority_queue<int, vector<int>, greater<int>> pq[1010][1010];
struct pos {
  int y, x, cnt;
};
queue<pos> q;
bool in(int a, int b) { return 1 <= a && a <= n && 1 <= b && b <= m; }
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m >> sy >> sx >> ey >> ex;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      cin >> _map[i][j];
  pq[sy][sx].push(0);
  q.push({sy, sx, 0});
  while (q.size()) {
    auto [y, x, cnt] = q.front();
    q.pop();
    if (pq[y][x].top() < cnt)
      continue;
    for (int i = 0; i < 4; i++) {
      int ny = y + gy[i], nx = x + gx[i];
      if (!in(ny, nx) || _map[ny][nx] == '#')
        continue;
      if (pq[ny][nx].empty()) {
        pq[ny][nx].push(cnt);
        q.push({ny, nx, cnt});
      } else if (pq[ny][nx].top() > cnt) {
        pq[ny][nx].pop();
        pq[ny][nx].push(cnt);
        q.push({ny, nx, cnt});
      }
    }
    for (int i = 0; i < 24; i++) {
      int ny = y + dy[i], nx = x + dx[i];
      if (!in(ny, nx) || _map[ny][nx] == '#')
        continue;
      if (pq[ny][nx].empty()) {
        pq[ny][nx].push(cnt + 1);
        q.push({ny, nx, cnt + 1});
      } else if (pq[ny][nx].top() > cnt) {
        pq[ny][nx].pop();
        pq[ny][nx].push(cnt + 1);
        q.push({ny, nx, cnt + 1});
      }
    }
  }
  if (pq[ey][ex].size())
    cout << pq[ey][ex].top();
  else
    cout << -1 << '\\n';
}