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

#define _USE_MATH_DEFIMES
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

const int MOD = 1'000'000'007;
const int INF = 1'000'000'000; // 1e9
const int NIL = -1;
const long long LINF = 1'000'000'000'000'000'000; // 1e18
const long double EPS = 1E-10;

template <class T, class S> inline bool chmax(T &a, const S &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T, class S> inline bool chmin(T &a, const S &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

int dx[4] = {0, -1, 0, 1};
int dy[4] = {1, 0, -1, 0};

int main() {
  int H, W;
  std::cin >> H >> W;
  int Ch, Cw;
  std::cin >> Ch >> Cw;
  int Dh, Dw;
  std::cin >> Dh >> Dw;
  --Ch;
  --Cw;
  --Dh;
  --Dw;
  std::vector<std::string> S(H);
  for (int i(0); i < H; ++i)
    std::cin >> S[i];
  std::queue<std::pair<int, int>> que;
  std::vector<std::vector<int>> d(H, std::vector<int>(W, INF));

  d[Ch][Cw] = 0;
  que.emplace(Ch, Cw);
  for (int k(0); k < 2'000'000; ++k) {
    std::set<std::pair<int, int>> cur, nxt;
    while (!que.empty()) {
      auto [h, w] = que.front();
      if (h == Dh && w == Dw) {
        std::cout << d[Dh][Dw] << std::endl;
        return 0;
      }
      cur.emplace(que.front());
      que.pop();
      {
        int nh, nw;
        for (int i(0); i < 4; ++i) {
          nh = h + dx[i];
          nw = w + dy[i];
          if (nh < 0 || H <= nh || nw < 0 || W <= nw || S[nh][nw] == '#')
            continue;
          if (chmin(d[nh][nw], d[h][w])) {
            que.emplace(nh, nw);
            cur.emplace(nh, nw);
            if (nh == Dh && nw == Dw) {
              std::cout << d[Dh][Dw] << std::endl;
              return 0;
            }
          }
        }
      }
    }

    for (auto [a, b] : cur) {
      int nh, nw;
      for (int i(-2); i <= 2; ++i) {
        for (int j(-2); j <= 2; ++j) {
          nh = a + i;
          nw = b + j;
          if (nh < 0 || H <= nh || nw < 0 || W <= nw || S[nh][nw] == '#')
            continue;
          if (chmin(d[nh][nw], k + 1)) {
            if (nxt.find({nh, nw}) == std::end(nxt)) {
              que.emplace(nh, nw);
            }
            nxt.emplace(nh, nw);
          }
        }
      }
    }
  }

  std::cout << (d[Dh][Dw] != INF ? d[Dh][Dw] : -1) << std::endl;
  return 0;
}


Error: TLE

Fixed submission:

#define _USE_MATH_DEFIMES
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

const int MOD = 1'000'000'007;
const int INF = 1'000'000'000; // 1e9
const int NIL = -1;
const long long LINF = 1'000'000'000'000'000'000; // 1e18
const long double EPS = 1E-10;

template <class T, class S> inline bool chmax(T &a, const S &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T, class S> inline bool chmin(T &a, const S &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

int dx[4] = {0, -1, 0, 1};
int dy[4] = {1, 0, -1, 0};

int main() {
  int H, W;
  std::cin >> H >> W;
  int Ch, Cw;
  std::cin >> Ch >> Cw;
  int Dh, Dw;
  std::cin >> Dh >> Dw;
  --Ch;
  --Cw;
  --Dh;
  --Dw;
  std::vector<std::string> S(H);
  for (int i(0); i < H; ++i)
    std::cin >> S[i];
  std::queue<std::pair<int, int>> que;
  std::vector<std::vector<int>> d(H, std::vector<int>(W, INF));
  d[Ch][Cw] = 0;
  que.emplace(Ch, Cw);
  while (!que.empty()) {
    auto [h, w] = que.front();
    que.pop();
    {
      int nh, nw;
      for (int i(0); i < 4; ++i) {
        nh = h + dx[i];
        nw = w + dy[i];
        if (nh < 0 || H <= nh || nw < 0 || W <= nw || S[nh][nw] == '#')
          continue;
        if (chmin(d[nh][nw], d[h][w])) {
          que.emplace(nh, nw);
        }
      }
      for (int i(-2); i <= 2; ++i) {
        for (int j(-2); j <= 2; ++j) {
          nh = h + i;
          nw = w + j;
          if (nh < 0 || H <= nh || nw < 0 || W <= nw || S[nh][nw] == '#')
            continue;
          if (chmin(d[nh][nw], d[h][w] + 1)) {
            que.emplace(nh, nw);
          }
        }
      }
    }
  }
  std::cout << (d[Dh][Dw] != INF ? d[Dh][Dw] : -1) << std::endl;
  return 0;
}