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

#include <algorithm>
#include <array>
#include <deque>
#include <iostream>
#include <numeric>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
using ll = long long;
#define rep(i, n) for (decltype(n) i = 0; i < n; ++i)
#define revrep(i, n) for (decltype(n) i = n - 1; n >= 0; --i)
inline constexpr ll Inf = (1ULL << 62) - 1;

template <class T> void updatemax(T &a, T b) {
  if (b > a)
    a = b;
}

char s[1005][1005];
ll cost[1005][1005];
struct cell {
  ll h, w;
  ll warp;
};
int offset[] = {-1, 0, 1, 0, -1};
int main() {
  ll ans = 0, H, W, Ch, Cw, Dh, Dw;
  cin >> H >> W >> Ch >> Cw >> Dh >> Dw;
  rep(i, H + 1) rep(j, W + 1) cost[i][j] = Inf;
  rep(i, H) rep(j, W) cin >> s[i + 1][j + 1];
  deque<cell> posq;
  posq.push_front({Ch, Cw, 0});
  cost[Ch][Cw] = 0;
  while (posq.size()) {
    auto p = posq.front();
    posq.pop_front();
    rep(i, 4) {
      auto h = p.h + offset[i];
      auto w = p.w + offset[i + 1];
      if (h <= 0 || w <= 0 || h > H || w > W)
        continue;
      if (s[h][w] == '.' && cost[h][w] > p.warp) {
        posq.push_front({h, w, p.warp});
        cost[h][w] = p.warp;
      }
    }
    for (int i = -2; i <= 2; ++i) {
      for (int j = -2; j <= 2; ++j) {
        if (i == 0 && j == 0)
          continue;
        if (abs(i) == 1 && j == 0)
          continue;
        if (i == 0 && abs(j) == 1)
          continue;
        auto h = p.h + i;
        auto w = p.w + j;
        if (h <= 0 || w <= 0 || h > H || w > W)
          continue;
        if (s[h][w] == '.' && cost[h][w] > p.warp + 1) {
          posq.push_back({h, w, p.warp + 1});
          cost[h][w] = p.warp + 1;
        }
      }
    }
  }
  ans = cost[Dh][Dw];
  if (ans == Inf)
    ans = -1;
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <deque>
#include <iostream>
#include <numeric>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
using ll = long long;
#define rep(i, n) for (decltype(n) i = 0; i < n; ++i)
#define revrep(i, n) for (decltype(n) i = n - 1; n >= 0; --i)
inline constexpr ll Inf = (1ULL << 62) - 1;

template <class T> void updatemax(T &a, T b) {
  if (b > a)
    a = b;
}

char s[1005][1005];
ll dp[1005][1005];
struct cell {
  ll h, w;
  ll warp;
};
int offset[] = {-1, 0, 1, 0, -1};
int main() {
  ll ans = 0, H, W, Ch, Cw, Dh, Dw;
  cin >> H >> W >> Ch >> Cw >> Dh >> Dw;
  rep(i, H + 1) rep(j, W + 1) dp[i][j] = Inf;
  rep(i, H) rep(j, W) cin >> s[i + 1][j + 1];
  deque<cell> posq;
  posq.push_front({Ch, Cw, 0});
  dp[Ch][Cw] = 0;
  while (posq.size()) {
    auto p = posq.front();
    posq.pop_front();
    if (p.h <= 0 || p.w <= 0 || p.h > H || p.w > W)
      continue;
    if (s[p.h][p.w] == '#')
      continue;
    if (p.warp < dp[p.h][p.w])
      dp[p.h][p.w] = p.warp;
    rep(i, 4) {
      if (dp[p.h + offset[i]][p.w + offset[i + 1]] > p.warp)
        posq.push_front({p.h + offset[i], p.w + offset[i + 1], p.warp});
    }
    for (int i = -2; i <= 2; ++i) {
      for (int j = -2; j <= 2; ++j) {
        if (i == 0 && j == 0)
          continue;
        if (i == 1 && j == 0)
          continue;
        if (i == -1 && j == 0)
          continue;
        if (i == 0 && j == 1)
          continue;
        if (i == 0 && j == -1)
          continue;
        if (dp[p.h + i][p.w + j] > p.warp + 1)
          posq.push_back({p.h + i, p.w + j, p.warp + 1});
      }
    }
  }
  ans = dp[Dh][Dw];
  if (ans == Inf)
    ans = -1;
  cout << ans << endl;
  return 0;
}