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

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<long long, long long>;
constexpr char ln = '\\n';
constexpr long long MOD = 1000000007LL;
constexpr long long INF = 1001001001LL;
constexpr long long LINF = 1001001001001001001;
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rept(i, j, n) for (int i = (j); i < (n); i++)
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;
}

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

int main() {
  int h, w;
  cin >> h >> w;
  int ch, cw, dh, dw;
  cin >> ch >> cw >> dh >> dw;
  ch--, cw--, dh--, dw--;
  vector<string> grid(h);
  rep(i, h) cin >> grid[i];

  // BFS
  vector<vector<int>> C(h, vector<int>(w, INF));
  C[ch][cw] = 0;
  vector<vector<int>> check(h, vector<int>(w));
  check[ch][cw] = 1; // 1==walk, -1,warp
  queue<pii> que;
  que.push({ch, cw});
  while (!que.empty()) {
    auto p = que.front();
    que.pop();
    int y = p.first, x = p.second;
    queue<pii> que_walk;
    que_walk.push({y, x});
    // 徒歩で移動できるだけ移動する。移動時に移動したマスをメモる。この処理は１度だけ行う
    vector<pii> memo;
    memo.push_back({y, x});
    while (!que_walk.empty()) {
      auto q = que_walk.front();
      que_walk.pop();
      rep(i, 4) {
        int ny = q.first + dy[i], nx = q.second + dx[i];
        if (ny < 0 or nx < 0 or ny >= h or nx >= w or grid[ny][nx] == '#')
          continue;
        if (check[ny][nx] <= 0 and C[ny][nx] > C[y][x]) {
          C[ny][nx] = C[y][x];
          check[ny][nx] = 1;
          que_walk.push({ny, nx});
          memo.push_back({ny, nx});
        }
      }
    }
    // 未踏域にワープする
    for (auto m : memo) {
      int vy = m.first, vx = m.second;
      for (int i = max(0, vy - 2); i <= min(h - 1, vy + 2); i++) {
        for (int j = max(0, vx - 2); j <= min(w - 1, vx + 2); j++) {
          if (grid[i][j] == '.' and check[i][j] == 0 and
              C[i][j] > C[y][x] + 1) {
            C[i][j] = C[y][x] + 1;
            check[i][j] = -1;
            que.push({i, j});
          }
        }
      }
    }
  }
  /*rep(i, h){
      rep(j, w){
          cout << C[i][j] << " ";
      }cout << ln;
  }
  rep(i, h){
      rep(j, w){
          cout << check[i][j] << " ";
      }cout << ln;
  }*/

  int res = C[dh][dw];
  if (res == INF)
    res = -1;
  cout << res << ln;
}


Error: TLE

Fixed submission:

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<long long, long long>;
constexpr char ln = '\\n';
constexpr long long MOD = 1000000007LL;
constexpr long long INF = 1001001001LL;
constexpr long long LINF = 1001001001001001001;
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rept(i, j, n) for (int i = (j); i < (n); i++)
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;
}

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

int main() {
  int h, w;
  cin >> h >> w;
  int ch, cw, dh, dw;
  cin >> ch >> cw >> dh >> dw;
  ch--, cw--, dh--, dw--;
  vector<string> grid(h);
  rep(i, h) cin >> grid[i];

  // BFS
  vector<vector<int>> C(h, vector<int>(w, INF));
  C[ch][cw] = 0;
  queue<pii> que;
  que.push({ch, cw});

  while (!que.empty()) {
    auto p = que.front();
    que.pop();
    int y = p.first, x = p.second;
    queue<pii> que_walk;
    que_walk.push({y, x});
    // 徒歩で移動できるだけ移動する。移動時に移動したマスをメモる
    vector<pii> memo;
    memo.push_back({y, x});
    while (!que_walk.empty()) {
      auto q = que_walk.front();
      que_walk.pop();
      rep(i, 4) {
        int ny = q.first + dy[i], nx = q.second + dx[i];
        if (ny < 0 or nx < 0 or ny >= h or nx >= w or grid[ny][nx] == '#')
          continue;
        if (C[ny][nx] >= C[y][x]) {
          C[ny][nx] = C[y][x];
          que_walk.push({ny, nx});
          memo.push_back({y, x});
        }
      }
    }
    // 未踏域にワープする
    for (auto m : memo) {
      int vy = m.first, vx = m.second;
      for (int i = max(0, vy - 2); i <= min(h - 1, vy + 2); i++) {
        for (int j = max(0, vx - 2); j <= min(w - 1, vx + 2); j++) {
          if (grid[i][j] == '.' and C[i][j] == INF) {
            C[i][j] = C[y][x] + 1;
            que.push({i, j});
          }
        }
      }
    }
  }
  /*rep(i, h){
      rep(j, w){
          cout << C[i][j] << " ";
      }cout << ln;
  }*/

  int res = C[dh][dw];
  if (res == INF)
    res = -1;
  cout << res << ln;
}