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

#include <bits/stdc++.h>
#define rep(i, n) for (long long i = 0; i < (long long)(n); ++i)
#define all(a) a.begin(), a.end()
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

const ll MOD = 1e9 + 7;
const ll INF = 1e18;
const double pi = acos(-1);
int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};
int dy[8] = {0, 1, 0, -1, -1, 1, 1, -1};
int ddx[5] = {2, 1, 0, -1, -2};
int ddy[5] = {2, 1, 0, -1, -2};

const int MAX = 510000;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int h, w;
  cin >> h >> w;

  int sy, sx;
  cin >> sy >> sx;
  sy--;
  sx--;

  int gy, gx;
  cin >> gy >> gx;
  gy--;
  gx--;

  vector<string> vec(h);
  rep(i, h) cin >> vec[i];

  int d[h][w];
  rep(i, h) rep(j, w) d[i][j] = 1e9;
  d[sy][sx] = 0;

  deque<P> que;
  que.push_back(P(sy, sx));
  while (!que.empty()) {
    int y = que.front().first;
    int x = que.front().second;
    que.pop_front();
    rep(i, 4) {
      int ny = y + dy[i];
      int nx = x + dx[i];
      if (ny < 0 || h <= ny || nx < 0 || w <= nx)
        continue;
      if (vec[ny][nx] == '#')
        continue;
      if (d[ny][nx] > d[y][x]) {
        d[ny][nx] = d[y][x];
        que.push_front(P(ny, nx));
      }
    }
    rep(i, 5) rep(j, 5) {
      if (i == 2 && j == 2)
        continue;
      int ny = y + ddy[i];
      int nx = x + ddx[j];
      if (ny < 0 || h <= ny || nx < 0 || w <= nx)
        continue;
      if (vec[ny][nx] == '#')
        continue;
      if (d[ny][nx] > d[y][x] + 1) {
        d[ny][nx] = d[y][x] + 1;
        que.push_back(P(ny, nx));
      }
    }
  }
  if (d[gy][gx] == 1e9)
    cout << -1 << endl;
  else
    cout << d[gy][gx] << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (long long i = 0; i < (long long)(n); ++i)
#define all(a) a.begin(), a.end()
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

const ll MOD = 1e9 + 7;
const ll INF = 1e18;
const double pi = acos(-1);
int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};
int dy[8] = {0, 1, 0, -1, -1, 1, 1, -1};
int ddx[5] = {2, 1, 0, -1, -2};
int ddy[5] = {2, 1, 0, -1, -2};

const int MAX = 510000;
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int h, w;
  cin >> h >> w;

  int sy, sx;
  cin >> sy >> sx;
  sy--;
  sx--;

  int gy, gx;
  cin >> gy >> gx;
  gy--;
  gx--;

  vector<string> vec(h);
  rep(i, h) cin >> vec[i];
  bool ok = false;
  rep(i, 5) rep(j, 5) {
    if (i == 2 && j == 2)
      continue;
    int y = gy + i - 2;
    int x = gx + j - 2;
    if (y < 0 || h <= y || x < 0 || w <= x)
      continue;
    if (vec[y][x] == '.')
      ok = true;
  }
  rep(i, 5) rep(j, 5) {
    if (i == 2 && j == 2)
      continue;
    int y = sy + i - 2;
    int x = sx + j - 2;
    if (y < 0 || h <= y || x < 0 || w <= x)
      continue;
    if (vec[y][x] == '.')
      ok = true;
  }
  if (!ok) {
    cout << -1 << endl;
    return 0;
  }
  queue<P> que;
  que.push(P(sy, sx));
  vector<vector<ll>> d(h, vector<ll>(w, 1e8));
  d[sy][sx] = 0;
  while (!que.empty()) {
    int y = que.front().first;
    int x = que.front().second;
    que.pop();
    rep(i, 4) {
      int ny = y + dy[i];
      int nx = x + dx[i];
      if (ny < 0 || h <= ny || nx < 0 || w <= nx)
        continue;
      if (vec[ny][nx] == '#')
        continue;
      if (d[ny][nx] <= d[y][x])
        continue;
      if (ny == gy && nx == gx) {
        d[ny][nx] = d[y][x];
        break;
      }
      d[ny][nx] = d[y][x];
      que.push(P(ny, nx));
    }
    rep(i, 5) rep(j, 5) {
      int ny = y + ddy[i];
      int nx = x + ddx[j];
      if (i == 2 && j == 2)
        continue;
      if (ny < 0 || h <= ny || nx < 0 || w <= nx)
        continue;
      if (vec[ny][nx] == '#')
        continue;
      if (d[ny][nx] <= d[y][x] + 1)
        continue;
      d[ny][nx] = d[y][x] + 1;
      que.push(P(ny, nx));
    }
  }
  if (d[gy][gx] == 1e8)
    cout << -1 << endl;
  else
    cout << d[gy][gx] << endl;

  return 0;
}