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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> Pi;
typedef pair<int, Pi> P;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define fi first
#define se second
#define endl "\\n"

template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> ostream &operator<<(ostream &s, const complex<T> &d) {
  return s << "(" << d.real() << ", " << d.imag() << ")";
}
template <typename T1, typename T2>
ostream &operator<<(ostream &s, const pair<T1, T2> &d) {
  return s << "(" << d.first << ", " << d.second << ")";
}
template <typename T> ostream &operator<<(ostream &s, const vector<T> &d) {
  int len = d.size();
  rep(i, len) {
    s << d[i];
    if (i < len - 1)
      s << " ";
  }
  return s;
}
template <typename T>
ostream &operator<<(ostream &s, const vector<vector<T>> &d) {
  int len = d.size();
  rep(i, len) { s << d[i] << endl; }
  return s;
}
template <typename T> ostream &operator<<(ostream &s, const set<T> &v) {
  s << "{ ";
  for (auto itr = v.begin(); itr != v.end(); ++itr) {
    if (itr != v.begin()) {
      s << ", ";
    }
    s << (*itr);
  }
  s << " }";
  return s;
}
template <typename T> ostream &operator<<(ostream &s, const multiset<T> &v) {
  s << "{ ";
  for (auto itr = v.begin(); itr != v.end(); ++itr) {
    if (itr != v.begin()) {
      s << ", ";
    }
    s << (*itr);
  }
  s << " }";
  return s;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &s, const map<T1, T2> &m) {
  s << "{" << endl;
  for (auto itr = m.begin(); itr != m.end(); ++itr) {
    s << " " << (*itr).first << " : " << (*itr).second << endl;
  }
  s << "}" << endl;
  return s;
}

const ll mod = 1'000'000'007;
const ll inf = 1'000'000'000'000'000'00;
const int INF = 1'000'000'000;
const double EPS = 1e-10;
const double PI = acos(-1);

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int h, w;
  cin >> h >> w;
  Pi st, go;
  cin >> st.fi >> st.se;
  cin >> go.fi >> go.se;
  st.fi--, st.se--, go.fi--, go.se--;
  vector<string> g(h);
  rep(i, h) { cin >> g[i]; }
  vector<vector<int>> num(h + 10, vector<int>(w + 10, -1));
  vector<int> dx = {1, -1, 0, 0}, dy = {0, 0, 1, -1};
  auto check = [&](int i, int j) {
    return (0 <= i && i < h && 0 <= j && j < w);
  };
  int now = 0;
  rep(ii, h) rep(jj, w) {
    if (num[ii][jj] != -1 || g[ii][jj] == '#')
      continue;
    queue<Pi> que;
    que.push(Pi(ii, jj));
    num[ii][jj] = now;
    while (!que.empty()) {
      auto [x, y] = que.front();
      que.pop();
      rep(i, 4) {
        int nx = x + dx[i], ny = y + dy[i];
        if (check(nx, ny) && g[nx][ny] == '.' && num[nx][ny] == -1) {
          num[nx][ny] = now;
          que.push(Pi(nx, ny));
        }
      }
    }
    now++;
  }
  vector<vector<int>> dp(h + 10, vector<int>(w + 10, INF));
  dp[st.fi][st.se] = 0;
  vector<int> dist(now + 10, INF);
  int from = num[st.fi][st.se];
  dist[from] = 0;
  priority_queue<P, vector<P>, greater<P>> que;
  que.push(P(0, st));
  while (!que.empty()) {
    auto tmp = que.top();
    que.pop();
    int d = tmp.fi;
    auto [x, y] = tmp.se;
    if (dp[x][y] < d)
      continue;
    rep(i, 4) {
      int nx = x + dx[i], ny = y + dy[i];
      if (check(nx, ny) && g[nx][ny] == '.') {
        if (chmin(dp[nx][ny], dp[x][y])) {
          que.push(P(dp[nx][ny], Pi(nx, ny)));
        }
      }
    }
    FOR(i, -2, 3) FOR(j, -2, 3) {
      if (i == 0 && j == 0)
        continue;
      int nx = x + i, ny = y + j;
      if (check(nx, ny) && g[nx][ny] == '.' && num[nx][ny] != num[x][y] &&
          chmin(dp[nx][ny], dp[x][y] + 1)) {
        que.push(P(dp[nx][ny], Pi(nx, ny)));
      }
    }
  }
  int ans = dp[go.fi][go.se];
  if (ans == INF)
    cout << -1 << endl;
  else
    cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> P;
typedef pair<int, int> Pi;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define fi first
#define se second
#define endl "\\n"

template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> ostream &operator<<(ostream &s, const complex<T> &d) {
  return s << "(" << d.real() << ", " << d.imag() << ")";
}
template <typename T1, typename T2>
ostream &operator<<(ostream &s, const pair<T1, T2> &d) {
  return s << "(" << d.first << ", " << d.second << ")";
}
template <typename T> ostream &operator<<(ostream &s, const vector<T> &d) {
  int len = d.size();
  rep(i, len) {
    s << d[i];
    if (i < len - 1)
      s << " ";
  }
  return s;
}
template <typename T>
ostream &operator<<(ostream &s, const vector<vector<T>> &d) {
  int len = d.size();
  rep(i, len) { s << d[i] << endl; }
  return s;
}
template <typename T> ostream &operator<<(ostream &s, const set<T> &v) {
  s << "{ ";
  for (auto itr = v.begin(); itr != v.end(); ++itr) {
    if (itr != v.begin()) {
      s << ", ";
    }
    s << (*itr);
  }
  s << " }";
  return s;
}
template <typename T> ostream &operator<<(ostream &s, const multiset<T> &v) {
  s << "{ ";
  for (auto itr = v.begin(); itr != v.end(); ++itr) {
    if (itr != v.begin()) {
      s << ", ";
    }
    s << (*itr);
  }
  s << " }";
  return s;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &s, const map<T1, T2> &m) {
  s << "{" << endl;
  for (auto itr = m.begin(); itr != m.end(); ++itr) {
    s << " " << (*itr).first << " : " << (*itr).second << endl;
  }
  s << "}" << endl;
  return s;
}

const ll mod = 1'000'000'007;
const ll inf = 1'000'000'000'000'000'00;
const int INF = 1'000'000'000;
const double EPS = 1e-10;
const double PI = acos(-1);

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int h, w;
  cin >> h >> w;
  Pi st, go;
  cin >> st.fi >> st.se;
  cin >> go.fi >> go.se;
  st.fi--, st.se--, go.fi--, go.se--;
  vector<string> g(h);
  rep(i, h) { cin >> g[i]; }
  vector<vector<int>> num(h + 10, vector<int>(w + 10, -1));
  vector<int> dx = {1, -1, 0, 0}, dy = {0, 0, 1, -1};
  auto check = [&](int i, int j) {
    return (0 <= i && i < h && 0 <= j && j < w);
  };
  int now = 0;
  rep(ii, h) rep(jj, w) {
    if (num[ii][jj] != -1 || g[ii][jj] == '#')
      continue;
    queue<Pi> que;
    que.push(Pi(ii, jj));
    num[ii][jj] = now;
    while (!que.empty()) {
      auto [x, y] = que.front();
      que.pop();
      rep(i, 4) {
        int nx = x + dx[i], ny = y + dy[i];
        if (check(nx, ny) && g[nx][ny] == '.' && num[nx][ny] == -1) {
          num[nx][ny] = now;
          que.push(Pi(nx, ny));
        }
      }
    }
    now++;
  }
  vector<vector<int>> dp(h + 10, vector<int>(w + 10, INF));
  dp[st.fi][st.se] = 0;
  queue<Pi> que;
  que.push(st);
  while (!que.empty()) {
    auto [x, y] = que.front();
    que.pop();
    rep(i, 4) {
      int nx = x + dx[i], ny = y + dy[i];
      if (check(nx, ny) && g[nx][ny] == '.') {
        if (chmin(dp[nx][ny], dp[x][y]))
          que.push(Pi(nx, ny));
      }
    }
    FOR(i, -2, 3) FOR(j, -2, 3) {
      if (i == 0 && j == 0)
        continue;
      int nx = x + i, ny = y + j;
      if (check(nx, ny) && g[nx][ny] == '.' && num[nx][ny] != num[x][y] &&
          chmin(dp[nx][ny], dp[x][y] + 1)) {
        que.push(Pi(nx, ny));
      }
    }
  }
  int ans = dp[go.fi][go.se];
  if (ans == INF)
    cout << -1 << endl;
  else
    cout << ans << endl;
}