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

#include <bits/stdc++.h>
using namespace std;
#define __ << " " <<
#define ___ << " "
#define bash push_back
#define ALL(x) x.begin(), x.end()
// #define int long long
struct IoSetup {
  IoSetup() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
    cerr << fixed << setprecision(10);
  }
} IoSetup;

using Int = __int128_t;
using ll = long long;
using pii = pair<int, int>;
constexpr int INF = 0x3f3f3f3f;
constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr int SMOD = 1000000007;
constexpr int NMOD = 998244353;
constexpr int dx[] = {1, 0, -1, 0, 1, 1, -1, -1};
constexpr int dy[] = {0, -1, 0, 1, -1, 1, -1, 1};

inline bool inside(int x, int y, int w, int h) {
  return (x >= 0 && y >= 0 && x < w && y < h);
}
template <class T> bool chmax(T &a, const T &b) {
  if (a < b)
    return (a = b, 1);
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a)
    return (a = b, 1);
  return 0;
}

int dist[1111][1111];

signed main() {

  int h, w;

  cin >> h >> w;

  int ch, cw, dh, dw;

  cin >> ch >> cw >> dh >> dw;
  ch--, cw--, dh--, dw--;

  vector<string> s(h);

  for (int i = 0; i < h; i++) {
    cin >> s[i];
  }

  memset(dist, 0x3f, sizeof(dist));

  dist[ch][cw] = 0;

  deque<pii> que;

  que.push_front({ch, cw});

  while (!que.empty()) {
    int y = que.front().first;
    int x = que.front().second;
    que.pop_front();

    for (int i = 0; i < 4; i++) {
      int nx = x + dx[i];
      int ny = y + dy[i];
      if (inside(nx, ny, w, h) and dist[ny][nx] > dist[y][x] and
          s[ny][nx] != '#') {
        dist[ny][nx] = dist[y][x];
        que.push_front({ny, nx});
      }
    }

    for (int i = 0; i < 5; i++) {
      for (int j = 0; j < 5; j++) {
        int nx = x + j - 2;
        int ny = y + i - 2;
        if (inside(nx, ny, w, h) and dist[ny][nx] > dist[y][x] + 1 and
            s[ny][nx] != '#') {
          dist[ny][nx] = dist[y][x] + 1;
          que.push_back({ny, nx});
        }
      }
    }
  }

  if (dist[dh][dw] >= INF)
    dist[dh][dw] = -1;

  cout << dist[dh][dw] << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define __ << " " <<
#define ___ << " "
#define bash push_back
#define ALL(x) x.begin(), x.end()
// #define int long long
struct IoSetup {
  IoSetup() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
    cerr << fixed << setprecision(10);
  }
} IoSetup;

using Int = __int128_t;
using ll = long long;
using pii = pair<int, int>;
constexpr int INF = 0x3f3f3f3f;
constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr int SMOD = 1000000007;
constexpr int NMOD = 998244353;
constexpr int dx[] = {1, 0, -1, 0, 1, 1, -1, -1};
constexpr int dy[] = {0, -1, 0, 1, -1, 1, -1, 1};

inline bool inside(int x, int y, int w, int h) {
  return (x >= 0 && y >= 0 && x < w && y < h);
}
template <class T> bool chmax(T &a, const T &b) {
  if (a < b)
    return (a = b, 1);
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a)
    return (a = b, 1);
  return 0;
}

int dist[1111][1111];

signed main() {

  int h, w;

  cin >> h >> w;

  int ch, cw, dh, dw;

  cin >> ch >> cw >> dh >> dw;
  ch--, cw--, dh--, dw--;

  vector<string> s(h);

  for (int i = 0; i < h; i++) {
    cin >> s[i];
  }

  memset(dist, 0x3f, sizeof(dist));

  dist[ch][cw] = 0;

  queue<pii> que;

  que.push({ch, cw});

  while (!que.empty()) {
    int y = que.front().first;
    int x = que.front().second;
    que.pop();

    for (int i = 0; i < 4; i++) {
      int nx = x + dx[i];
      int ny = y + dy[i];
      if (inside(nx, ny, w, h) and dist[ny][nx] > dist[y][x] and
          s[ny][nx] != '#') {
        dist[ny][nx] = dist[y][x];
        que.push({ny, nx});
      }
    }

    for (int i = 0; i < 5; i++) {
      for (int j = 0; j < 5; j++) {
        int nx = x + j - 2;
        int ny = y + i - 2;
        if (inside(nx, ny, w, h) and dist[ny][nx] > dist[y][x] + 1 and
            s[ny][nx] != '#') {
          dist[ny][nx] = dist[y][x] + 1;
          que.push({ny, nx});
        }
      }
    }
  }

  if (dist[dh][dw] >= INF)
    dist[dh][dw] = -1;

  cout << dist[dh][dw] << endl;

  return 0;
}