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

#include <bits/stdc++.h>
using ll = long long;
#define FOR(i, k, n) for (ll i = (k); i < (n); i++)
#define FORe(i, k, n) for (ll i = (k); i <= (n); i++)
#define FORr(i, k, n) for (ll i = (k)-1; i > (n); i--)
#define FORre(i, k, n) for (ll i = (k)-1; i >= (n); i--)
#define REP(i, n) FOR(i, 0, n)
#define REPr(i, n) FORre(i, n, 0)
#define ALL(x) (x).begin(), (x).end()
#define ALLr(x) (x).rbegin(), (x).rend()
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
using namespace std;

const int INF = 1001001001;

using P = pair<ll, ll>;

int main(void) {
  ll h, w;
  cin >> h >> w;
  ll c[2], d[2];
  cin >> c[0] >> c[1];
  cin >> d[0] >> d[1];
  c[0]--;
  c[1]--;
  d[0]--;
  d[1]--;
  vector<string> s(h);
  REP(i, h) cin >> s[i];

  vector<vector<ll>> cost(h, vector<ll>(w, INF));
  deque<P> q;
  cost[c[0]][c[1]] = 0;
  q.emplace_front(c[0], c[1]);
  ll dx[] = {-1, 0, 1, 0};
  ll dy[] = {0, -1, 0, 1};
  ll dh[] = {-2, -1, 0, 1, 2};
  ll dw[] = {-2, -1, 0, 1, 2};
  while (!q.empty()) {
    ll y = q.front().first;
    ll x = q.front().second;
    q.pop_front();
    REP(i, 4) {
      ll nx = x + dx[i];
      ll ny = y + dy[i];
      if (nx < 0 || nx >= w || ny < 0 || ny >= h)
        continue;
      if (s[ny][nx] == '#')
        continue;
      if (cost[y][x] < cost[ny][nx]) {
        cost[ny][nx] = cost[y][x];
        q.emplace_front(ny, nx);
      }
    }
    REP(i, 5) REP(j, 5) {
      if (dh[i] == 0 && dw[j] == 0)
        continue;
      ll ny = y + dh[i];
      ll nx = x + dw[j];
      if (nx < 0 || nx >= w || ny < 0 || ny >= h)
        continue;
      if (s[ny][nx] == '#')
        continue;
      if (cost[y][x] + 1 < cost[ny][nx]) {
        cost[ny][nx] = cost[y][x] + 1;
        q.emplace_back(ny, nx);
      }
    }
  }
  if (cost[d[0]][d[1]] == INF)
    cout << -1 << endl;
  else
    cout << cost[d[0]][d[1]] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using ll = long long;
#define FOR(i, k, n) for (ll i = (k); i < (n); i++)
#define FORe(i, k, n) for (ll i = (k); i <= (n); i++)
#define FORr(i, k, n) for (ll i = (k)-1; i > (n); i--)
#define FORre(i, k, n) for (ll i = (k)-1; i >= (n); i--)
#define REP(i, n) FOR(i, 0, n)
#define REPr(i, n) FORre(i, n, 0)
#define ALL(x) (x).begin(), (x).end()
#define ALLr(x) (x).rbegin(), (x).rend()
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
using namespace std;

const int INF = 1001001001;

using P = pair<ll, ll>;

int main(void) {
  ll h, w;
  cin >> h >> w;
  ll c[2], d[2];
  cin >> c[0] >> c[1];
  cin >> d[0] >> d[1];
  c[0]--;
  c[1]--;
  d[0]--;
  d[1]--;
  vector<string> s(h);
  REP(i, h) cin >> s[i];

  vector<vector<ll>> cost(h, vector<ll>(w, INF));
  queue<P> q;
  cost[c[0]][c[1]] = 0;
  q.emplace(c[0], c[1]);
  ll dx[] = {-1, 0, 1, 0};
  ll dy[] = {0, -1, 0, 1};
  ll dh[] = {-2, -1, 0, 1, 2};
  ll dw[] = {-2, -1, 0, 1, 2};
  while (!q.empty()) {
    ll y = q.front().first;
    ll x = q.front().second;
    q.pop();
    REP(i, 4) {
      ll nx = x + dx[i];
      ll ny = y + dy[i];
      if (nx < 0 || nx >= w || ny < 0 || ny >= h)
        continue;
      if (s[ny][nx] == '#')
        continue;
      if (cost[y][x] < cost[ny][nx]) {
        cost[ny][nx] = cost[y][x];
        q.emplace(ny, nx);
      }
    }
    REP(i, 5) REP(j, 5) {
      if (dh[i] == 0 && dw[j] == 0)
        continue;
      ll ny = y + dh[i];
      ll nx = x + dw[j];
      if (nx < 0 || nx >= w || ny < 0 || ny >= h)
        continue;
      if (s[ny][nx] == '#')
        continue;
      if (cost[y][x] + 1 < cost[ny][nx]) {
        cost[ny][nx] = cost[y][x] + 1;
        q.emplace(ny, nx);
      }
    }
  }
  if (cost[d[0]][d[1]] == INF)
    cout << -1 << endl;
  else
    cout << cost[d[0]][d[1]] << endl;
  return 0;
}