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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
/* clang-format off */
#define MOD 1000000007
#define INF 1000000000
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define ALL(a) (a).begin(), (a).end()
#define __DEBUG__
#ifdef __DEBUG__
#define CH_P(a) cout <<"check_point("<<#a<<")" << "\\n";
#define DEBUG(x) cout<<#x<<":"<<x<<"\\n"
#define DEBUGS(v) cout << #v << ":";for(auto x:v){cout<<x<<" ";}cout<<"\\n"
#endif
#ifndef __DEBUG__
#define CH_P(a) 
#define DEBUG(x) 
#define DEBUGS(v) 
#endif
/* clang-format on */

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int h, w, sy, sx, gy, gx;
  deque<tuple<int, int, int>> walk;
  set<pair<int, int>> passed;
  vector<pair<int, int>> d = {{0, -1}, {0, 1}, {-1, 0}, {1, 0}};
  cin >> h >> w >> sy >> sx >> gy >> gx;
  sy--, sx--, gy--, gx--;
  vector<string> m(h);
  vector<vector<int>> warp_map(h, vector<int>(w, INF));
  REP(i, h) { cin >> m[i]; }

  walk.push_front({sy, sx, -1});
  passed.insert({sy, sx});

  while (!walk.empty()) {
    int y, x, warp;
    tie(y, x, warp) = walk.front();
    walk.pop_front();
    warp_map[y][x] = warp;

    // 四方に歩いて探索
    for (auto [dy, dx] : d) {
      int cy = y + dy, cx = x + dx;
      if (0 <= cy && cy < h && 0 <= cx && cx < w && !passed.count({cy, cx}) &&
          m[cy][cx] == '.') {
        walk.push_front({cy, cx, warp});
        passed.insert({cy, cx});
      }
    }

    // 5x5四方にワープして探索
    for (int dy = -2; dy <= 2; dy++) {
      for (int dx = -2; dx <= 2; dx++) {
        int cy = y + dy, cx = x + dx;
        if (0 <= cy && cy < h && 0 <= cx &&
            cx < w && //! passed.count({cy, cx}) &&
            m[cy][cx] == '.' && warp_map[cy][cx] > warp + 1) {
          walk.push_back({cy, cx, warp + 1});
          // passed.insert({cy, cx});
        }
      }
    }
  }

  // REP(i, h) { DEBUGS(warp_map[i]); }
  cout << (warp_map[gy][gx] == INF ? -1 : warp_map[gy][gx]) << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
/* clang-format off */
#define MOD 1000000007
#define INF 1000000000
#define REP(i, n) for (ll i = 0, i##_len = (n); i < i##_len; ++i)
#define ALL(a) (a).begin(), (a).end()
#define __DEBUG__
#ifdef __DEBUG__
#define CH_P(a) cout <<"check_point("<<#a<<")" << "\\n";
#define DEBUG(x) cout<<#x<<":"<<x<<"\\n"
#define DEBUGS(v) cout << #v << ":";for(auto x:v){cout<<x<<" ";}cout<<"\\n"
#endif
#ifndef __DEBUG__
#define CH_P(a) 
#define DEBUG(x) 
#define DEBUGS(v) 
#endif
/* clang-format on */

int main() {
  ll h, w, sy, sx, gy, gx;
  deque<tuple<ll, ll, ll>> walk;
  set<pair<ll, ll>> passed;
  vector<pair<ll, ll>> d = {{0, -1}, {0, 1}, {-1, 0}, {1, 0}};
  cin >> h >> w >> sy >> sx >> gy >> gx;
  sy--, sx--, gy--, gx--;
  vector<string> m(h);
  vector<vector<ll>> warp_map(h, vector<ll>(w, INF));
  REP(i, h) { cin >> m[i]; }

  walk.push_front({sy, sx, -1});
  passed.insert({sy, sx});

  while (!walk.empty()) {
    ll y, x, warp;
    tie(y, x, warp) = walk.front();
    walk.pop_front();
    warp_map[y][x] = warp;

    // 四方に歩いて探索
    for (auto [dy, dx] : d) {
      ll cy = y + dy, cx = x + dx;
      if (0 <= cy && cy < h && 0 <= cx && cx < w && !passed.count({cy, cx}) &&
          m[cy][cx] == '.') {
        walk.push_front({cy, cx, warp});
        passed.insert({cy, cx});
      }
    }

    // 5x5四方にワープして探索
    for (ll dy = -2; dy <= 2; dy++) {
      for (ll dx = -2; dx <= 2; dx++) {
        ll cy = y + dy, cx = x + dx;
        if (0 <= cy && cy < h && 0 <= cx &&
            cx < w && //! passed.count({cy, cx}) &&
            m[cy][cx] == '.' && warp_map[cy][cx] > warp + 1) {
          walk.push_back({cy, cx, warp + 1});
          // passed.insert({cy, cx});
        }
      }
    }
  }

  // REP(i, h) { DEBUGS(warp_map[i]); }
  cout << (warp_map[gy][gx] == INF ? -1 : warp_map[gy][gx]) << endl;
  return 0;
}