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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <iomanip> //double精度 setprecision
#include <iostream>
#include <map>
#include <numeric> //lcm
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

// #include <atcoder/all>

using namespace std;
// using namespace atcoder;

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = n - 1; i >= 0; --i)
#define rep1(i, n) for (int i = 1; i <= (n); ++i)
#define rrep1(i, n) for (int i = (n); i >= 1; --i)
#define REP(i, n, m) for (int i = (n); i < (m); ++i)
#define all(vec) (vec).begin(), (vec).end()

#define debug(vec)                                                             \\
  for (auto v : vec)                                                           \\
    cerr << v << " ";                                                          \\
  cerr << endl;
#define debug2D(vec2D, w)                                                      \\
  for (auto vec : vec2D) {                                                     \\
    for (auto v : vec)                                                         \\
      cerr << setw(w) << v << " ";                                             \\
    cerr << endl;                                                              \\
  }
#define debugP(vec)                                                            \\
  for (auto v : vec)                                                           \\
    cerr << "(" << v.first << "," << v.second << ") ";                         \\
  cerr << endl;
#define debug2DP(vec2D)                                                        \\
  for (auto vec : vec2D) {                                                     \\
    for (auto v : vec)                                                         \\
      cerr << "(" << v.first << "," << v.second << ") ";                       \\
    cerr << endl;                                                              \\
  }

const char en = '\\n';

typedef long long ll;

constexpr ll INF = 1000000000; // 10^10
// const ll MOD = 998244353;
constexpr ll MOD = 1000000007;

typedef vector<vector<int>> Graph;
void add_edge(Graph &g, int from, int to, bool isDirected) {
  g[from].push_back(to);
  if (!isDirected)
    g[to].push_back(from);
}

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

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

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

  int h, w;
  cin >> h >> w;

  int si, sj, gi, gj;
  cin >> si >> sj >> gi >> gj;
  si--;
  sj--;
  gi--;
  gj--;
  vector<string> s(h);
  rep(i, h) cin >> s[i];

  vector<vector<int>> dist(h, vector<int>(w, h * w));

  deque<pair<int, int>> deq;
  deq.push_back({si * w + sj, 0});

  while (!deq.empty()) {
    pair<int, int> p = deq.front();
    deq.pop_front();
    int i = p.first / w, j = p.first % w;

    if (i == gi && j == gj) {
      cout << p.second << endl;
      return 0;
    }

    if (i < 0 || j < 0 || i >= h || j >= w || dist[i][j] <= p.second)
      continue;
    dist[i][j] = p.second;

    for (int di = -2; di <= 2; di++)
      for (int dj = -2; dj <= 2; dj++) {
        if (!(0 <= j + dj && j + dj < w && 0 <= i + di && i + di < h) ||
            (di == 0 && dj == 0) || s[i + di][j + dj] == '#')
          continue;
        if (abs(di + dj) < 2 && abs(di - dj) < 2)
          deq.push_front({p.first + dj + w * di, p.second});
        else
          deq.push_back({p.first + dj + w * di, p.second + 1});
      }
  }

  cout << -1 << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <iomanip> //double精度 setprecision
#include <iostream>
#include <map>
#include <numeric> //lcm
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = n - 1; i >= 0; --i)
#define rep1(i, n) for (int i = 1; i <= (n); ++i)
#define rrep1(i, n) for (int i = (n); i >= 1; --i)
#define REP(i, n, m) for (int i = (n); i < (m); ++i)
#define all(vec) (vec).begin(), (vec).end()

#define debug(vec)                                                             \\
  for (auto v : vec)                                                           \\
    cerr << v << " ";                                                          \\
  cerr << endl;
#define debug2D(vec2D, w)                                                      \\
  for (auto vec : vec2D) {                                                     \\
    for (auto v : vec)                                                         \\
      cerr << setw(w) << v << " ";                                             \\
    cerr << endl;                                                              \\
  }
#define debugP(vec)                                                            \\
  for (auto v : vec)                                                           \\
    cerr << "(" << v.first << "," << v.second << ") ";                         \\
  cerr << endl;
#define debug2DP(vec2D)                                                        \\
  for (auto vec : vec2D) {                                                     \\
    for (auto v : vec)                                                         \\
      cerr << "(" << v.first << "," << v.second << ") ";                       \\
    cerr << endl;                                                              \\
  }

const char en = '\\n';

typedef long long ll;

constexpr ll INF = 1000000000; // 10^10
// const ll MOD = 998244353;
constexpr ll MOD = 1000000007;

typedef vector<vector<int>> Graph;
void add_edge(Graph &g, int from, int to, bool isDirected) {
  g[from].push_back(to);
  if (!isDirected)
    g[to].push_back(from);
}

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

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

  ll h, w, sx, sy, gx, gy;
  cin >> h >> w >> sy >> sx >> gy >> gx;
  sx--;
  sy--;
  gx--;
  gy--;
  vector<vector<char>> s(h, vector<char>(w));
  rep(y, h) rep(x, w) cin >> s[y][x];

  vector<vector<int>> cc(h, vector<int>(w, -1));

  int cci = 1;
  rep(y, h) rep(x, w) {
    if (s[y][x] == '#') {
      cc[y][x] = 0;
    } else {
      if (cc[y][x] > 0)
        continue;
      vector<vector<bool>> done(h, vector<bool>(w, false));
      stack<pair<int, int>> st;
      st.push({x, y});
      while (!st.empty()) {
        pair<int, int> v = st.top();
        st.pop();
        int vx = v.first, vy = v.second;

        if (vy < 0 || vy >= h || vx < 0 || vx >= w || done[vy][vx] ||
            s[vy][vx] == '#')
          continue;
        done[vy][vx] = true;

        cc[vy][vx] = cci;

        st.push({vx + 1, vy});
        st.push({vx - 1, vy});
        st.push({vx, vy + 1});
        st.push({vx, vy - 1});
      }

      cci++;
    }
  }

  // debug2D(cc,2);

  vector<vector<ll>> graph(cci, vector<ll>());

  rep(y, h) rep(x, w) {
    if (cc[y][x] == 0)
      continue;
    rep(_dy, 5) rep(_dx, 5) {
      int dy = _dy - 2, dx = _dx - 2;
      if (dx == 0 && dy == 0)
        continue;
      if (y + dy < 0 || y + dy >= h || x + dx < 0 || x + dx >= w)
        continue;
      if (cc[y + dy][x + dx] != 0 && cc[y][x] != cc[y + dy][x + dx]) {
        if (!graph[cc[y][x]].empty() &&
            graph[cc[y][x]].back() == cc[(y + dy)][(x + dx)])
          continue;
        graph[cc[(y + dy)][(x + dx)]].push_back(cc[y][x]);
        graph[cc[y][x]].push_back(cc[y + dy][x + dx]);
      }
    }
  }

  int ans = -1;
  vector<bool> done(cci, false);
  vector<bool> seen(cci, false);
  queue<pair<int, ll>> que;
  que.push({0, cc[sy][sx]});
  seen[cc[sy][sx]] = true;
  while (!que.empty()) {
    ll v = que.front().second;
    int cnt = que.front().first;
    que.pop();

    if (done[v])
      continue;
    done[v] = true;

    if (cc[gy][gx] == v) {
      ans = cnt;
      break;
    }

    for (ll w : graph[v]) {
      if (seen[w])
        continue;
      que.push({cnt + 1, w});
      seen[w] = true;
    }
  }

  cout << ans << en;
}