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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, s, n) for (int i = (s); i < (n); i++)
#define all(v) v.begin(), v.end()
#define outve(v)                                                               \\
  for (auto i : v)                                                             \\
    cout << i << " ";                                                          \\
  cout << endl
#define outmat(v)                                                              \\
  for (auto i : v) {                                                           \\
    for (auto j : i)                                                           \\
      cout << j << " ";                                                        \\
    cout << endl;                                                              \\
  }
#define in(n, v)                                                               \\
  for (int i = 0; i < (n); i++) {                                              \\
    cin >> v[i];                                                               \\
  }
#define out(n) cout << (n) << endl
#define fi first
#define se second
#define pb push_back
#define si(v) int(v.size())
#define len(v) int(v.length())
#define lob(v, n) lower_bound(all(v), n)
#define lobi(v, n) lower_bound(all(v), n) - v.begin()
#define upb(v, n) upper_bound(all(v), n)
#define upbi(v, n) upper_bound(all(v), n) - v.begin()
#define mod 1000000007
#define infi 1900000000
#define infl 1100000000000000000
#define cyes cout << "Yes" << endl
#define cno cout << "No" << endl
#define csp << " " <<
#define outset(n) cout << fixed << setprecision(n);
using namespace std;
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vs = vector<string>;
using vc = vector<char>;
using vd = vector<double>;
using vvd = vector<vector<double>>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vvp = vector<vector<pair<int, int>>>;
template <typename T> using ve = vector<T>;
template <typename T> using pq2 = priority_queue<T>;
template <typename T> using pq1 = priority_queue<T, vector<T>, greater<T>>;
template <typename T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int H, W;
ve<ve<char>> G;
ve<ve<int>> telpote;

ve<int> dh = {0, -1, 1, 0};
ve<int> dw = {1, 0, 0, -1};

set<pii> B;

void bfs(int h, int w) {

  G[h][w] = '-';

  reps(i, -2, 3) reps(j, -2, 3) {
    if (i == 0 && j == 0)
      continue;
    int nh = h + i;
    int nw = w + j;

    if (nh >= 0 && nh < H && nw >= 0 && nw < W) {
      if (G[nh][nw] == '.') {
        if (chmin(telpote[nh][nw], telpote[h][w] + 1))
          B.insert({nh, nw});
      }
    }
  }

  rep(kkk, 4) {
    int nh = h + dh[kkk];
    int nw = w + dw[kkk];

    if (nh >= 0 && nh < H && nw >= 0 && nw < W) {
      if (G[nh][nw] != '.')
        continue;
      if (telpote[nh][nw] == telpote[h][w])
        continue;
      telpote[nh][nw] = telpote[h][w];
      bfs(nh, nw);
    }
  }
}

int main() {

  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  cin >> H >> W;
  G.resize(H, ve<char>(W));
  int Ch, Cw, Dh, Dw;
  cin >> Ch >> Cw >> Dh >> Dw;
  Ch--, Cw--, Dh--, Dw--;

  telpote.assign(H, vi(W, infi));
  telpote[Ch][Cw] = 0;

  rep(i, H) rep(j, W) cin >> G[i][j];
  B.insert({Ch, Cw});

  int a = 0;
  while (1) {
    int ff = 0;
    if (B.empty())
      break;
    set<pii> A = B;
    set<pii> C;
    B = C;
    while (!A.empty()) {
      pii p = *A.begin();
      A.erase(p);

      if (G[p.fi][p.se] == '.' && telpote[p.fi][p.se] == a) {
        bfs(p.fi, p.se);
        ff++;
      }
    }

    if (ff == 0)
      break;
    a++;
  }

  if (telpote[Dh][Dw] == infi)
    cout << -1 << endl;
  else
    cout << telpote[Dh][Dw] << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, s, n) for (int i = (s); i < (n); i++)
#define all(v) v.begin(), v.end()
#define outve(v)                                                               \\
  for (auto i : v)                                                             \\
    cout << i << " ";                                                          \\
  cout << endl
#define outmat(v)                                                              \\
  for (auto i : v) {                                                           \\
    for (auto j : i)                                                           \\
      cout << j << " ";                                                        \\
    cout << endl;                                                              \\
  }
#define in(n, v)                                                               \\
  for (int i = 0; i < (n); i++) {                                              \\
    cin >> v[i];                                                               \\
  }
#define out(n) cout << (n) << endl
#define fi first
#define se second
#define pb push_back
#define si(v) int(v.size())
#define len(v) int(v.length())
#define lob(v, n) lower_bound(all(v), n)
#define lobi(v, n) lower_bound(all(v), n) - v.begin()
#define upb(v, n) upper_bound(all(v), n)
#define upbi(v, n) upper_bound(all(v), n) - v.begin()
#define mod 1000000007
#define infi 1900000000
#define infl 1100000000000000000
#define cyes cout << "Yes" << endl
#define cno cout << "No" << endl
#define csp << " " <<
#define outset(n) cout << fixed << setprecision(n);
using namespace std;
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vs = vector<string>;
using vc = vector<char>;
using vd = vector<double>;
using vvd = vector<vector<double>>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vvp = vector<vector<pair<int, int>>>;
template <typename T> using ve = vector<T>;
template <typename T> using pq2 = priority_queue<T>;
template <typename T> using pq1 = priority_queue<T, vector<T>, greater<T>>;
template <typename T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int H, W;
ve<ve<char>> G;
ve<ve<int>> telpote;

ve<int> dh = {0, -1, 1, 0};
ve<int> dw = {1, 0, 0, -1};

void bfs(int h, int w) {

  G[h][w] = '-';

  reps(i, -2, 3) reps(j, -2, 3) {
    if (i == 0 && j == 0)
      continue;
    int nh = h + i;
    int nw = w + j;

    if (nh >= 0 && nh < H && nw >= 0 && nw < W) {
      if (G[nh][nw] == '.')
        chmin(telpote[nh][nw], telpote[h][w] + 1);
    }
  }

  rep(kkk, 4) {
    int nh = h + dh[kkk];
    int nw = w + dw[kkk];

    if (nh >= 0 && nh < H && nw >= 0 && nw < W) {
      if (G[nh][nw] != '.')
        continue;
      if (telpote[nh][nw] == telpote[h][w])
        continue;
      telpote[nh][nw] = telpote[h][w];
      bfs(nh, nw);
    }
  }
}

int main() {

  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  cin >> H >> W;
  G.resize(H, ve<char>(W));
  int Ch, Cw, Dh, Dw;
  cin >> Ch >> Cw >> Dh >> Dw;
  Ch--, Cw--, Dh--, Dw--;

  telpote.assign(H, vi(W, infi));
  telpote[Ch][Cw] = 0;

  rep(i, H) rep(j, W) cin >> G[i][j];

  int a = 0;
  while (1) {
    int ff = 0;
    rep(i, H) rep(j, W) {
      if (G[i][j] == '.' && telpote[i][j] == a) {
        bfs(i, j);
        ff++;
      }
    }
    if (ff == 0)
      break;
    a++;
  }

  if (telpote[Dh][Dw] == infi)
    cout << -1 << endl;
  else
    cout << telpote[Dh][Dw] << endl;

  return 0;
}