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

#include <bits/stdc++.h>
// #include <boost/multiprecision/cpp_int.hpp>
using namespace std;
// using namespace boost::multiprecision;
typedef long long int ll;
typedef long double ld;
typedef pair<ll, ll> P;
#define PI 3.141592653589793
#define MOD 1000000007
// #define MOD 998244353
#define ALL(obj) (obj).begin(), (obj).end()
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const ll INF = 1LL << 60;

// 四方向への移動ベクトル
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

struct edge { // グラフに使うヤツ
  ll from, to, cost;
};

typedef vector<vector<edge>> G;
ll gcd(ll a, ll b) {
  if (a % b == 0)
    return (b);
  else
    return (gcd(b, a % b));
}

int main() {
  ll h, w, cx, cy, dx, dy;
  cin >> h >> w >> cx >> cy >> dx >> dy;
  cx--;
  cy--;
  dx--;
  dy--;
  string s[h];
  for (ll i = 0; i < h; i++)
    cin >> s[i];
  deque<P> que;

  que.push_back(P(cx, cy));
  bool visited[h][w];
  ll d[h][w];
  for (ll i = 0; i < h; i++) {
    fill(visited[i], visited[i] + w, false);
    fill(d[i], d[i] + w, INF);
  }
  d[cx][cy] = 0;
  visited[cx][cy] = true;

  while (!que.empty()) {
    P p = que.front();
    ll X = p.first, Y = p.second;
    if (X == dx and Y == dy) {
      cout << d[X][Y] << endl;
      return 0;
    }
    visited[X][Y] = true;
    que.pop_front();
    for (ll i = -2; i < 3; i++) {
      for (ll j = -2; j < 3; j++) {
        ll x = X + i, y = Y + j;
        if (x < h and x >= 0 and y < w and y >= 0 and s[x][y] == '.') {
          if (visited[x][y])
            continue;
          if (abs(i) + abs(j) < 2) {
            que.push_front(P(x, y));
            d[x][y] = d[X][Y];
            visited[i][j] = true;
          } else {
            if (d[X][Y] + 1 < d[x][y]) {
              d[x][y] = d[X][Y] + 1;
              que.push_back(P(x, y));
            }
          }
        }
      }
    }
  }
  cout << -1 << endl;
  return 0;
}


Error: TLE

Fixed submission:

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
// #include <boost/multiprecision/cpp_int.hpp>
using namespace std;
// using namespace boost::multiprecision;
typedef long long ll;
typedef long double ld;
#define PI 3.141592653589793
#define MOD 1000000007
#define ALL(obj) (obj).begin(), (obj).end()
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const int INF = 1LL << 20;

// 四方向への移動ベクトル
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

struct edge { // グラフに使うヤツ
  int from, to, cost;
};

typedef vector<vector<edge>> G;
int gcd(int a, int b) {
  if (a % b == 0)
    return (b);
  else
    return (gcd(b, a % b));
}

int dx2[20] = {2, 2,  2,  2,  2,  1,  1,  1,  1,  0,
               0, -1, -1, -1, -1, -2, -2, -2, -2, -2};
int dy2[20] = {2,  1, 0, -1, -2, 2, 1, -1, -2, 2,
               -2, 2, 1, -1, -2, 2, 1, 0,  -1, -2};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int h, w, ch, cw, dh, dw;
  cin >> h >> w >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  string s[h];
  for (int i = 0; i < h; i++) {
    cin >> s[i];
  }
  int c[h][w];
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      c[i][j] = INF;
    }
  }
  c[ch][cw] = 0;
  int i = 0;
  queue<pair<int, int>> que;
  while (true) {
    for (int j = 0; j < h; j++) {
      for (int k = 0; k < w; k++) {
        if (c[j][k] == i) {
          que.push(make_pair(j, k));
        }
      }
    }
    if (que.empty())
      break;
    while (!que.empty()) {
      pair<int, int> p = que.front();
      que.pop();
      int x = p.first, y = p.second;
      for (int j = 0; j < 4; j++) {
        int xx = x + dx[j], yy = y + dy[j];
        if (xx >= 0 and xx < h and yy >= 0 and yy < w and i < c[xx][yy] and
            s[xx][yy] == '.') {
          c[xx][yy] = i;
          que.push(make_pair(xx, yy));
        }
      }
      for (int k = 0; k < 20; k++) {
        int xx = x + dx2[k], yy = y + dy2[k];
        if (xx >= 0 and xx < h and yy >= 0 and yy < w and i + 1 < c[xx][yy] and
            s[xx][yy] == '.') {
          c[xx][yy] = i + 1;
        }
      }
    }
    i++;
  }
  if (c[dh][dw] == INF) {
    cout << -1 << '\\n';
    return 0;
  }
  cout << c[dh][dw] << '\\n';
  return 0;
}