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

#include <bits/stdc++.h>
#define sz(x) (int)((x).size())
#define F first
#define S second
#define pb push_back
#define ub upper_bound
#define lb lower_bound
#define all(x) x.begin(), x.end()
#define pqueue priority_queue
#define mset multiset
#define umap unordered_map
#define Speed()                                                                \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define rep(i, n) for (int i = 0; i < n; ++i)
#define forn(i, n) for (int i = 1; i <= n; ++i)
#define lr(i, l, r)                                                            \\
  for (int i = l; (l > r ? i >= r : i <= r); (l > r ? --i : ++i))

using namespace std;

template <class T> void umin(T &a, T b) { a = min(a, b); }
template <class T> void umax(T &a, T b) { a = max(a, b); }

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int dx[] = {1, 0, -1, 0, -1, -1, 1, 1},
          dy[] = {0, 1, 0, -1, -1, 1, -1, 1};
const int mod = 1e9 + 7;
const ll N = 1e3 + 4;

int h, w;
char c[N][N];
int used[N][N];
pii A, B;

void solve() {
  cin >> h >> w;
  cin >> A.F >> A.S >> B.F >> B.S;
  for (int i = 1; i <= h; ++i) {
    for (int j = 1; j <= w; ++j) {
      cin >> c[i][j];
    }
  }
  queue<pii> q;
  used[A.F][A.S] = 1;
  q.push(A);
  int cnt = -1;
  auto check = [&](int x, int y) {
    return x >= 1 && x <= h && y >= 1 && y <= w && c[x][y] == '.' &&
           !used[x][y];
  };
  while (!q.empty()) {
    cnt++;
    vector<pii> vec;
    while (!q.empty()) {
      pii v = q.front();
      vec.pb(v);
      if (v == B) {
        cout << cnt;
        return;
      }
      q.pop();
      for (int i = 0; i < 4; ++i) {
        int x = v.F + dx[i], y = v.S + dy[i];
        if (check(x, y)) {
          used[x][y] = 1;
          q.push({x, y});
        }
      }
    }
    for (auto num : vec) {
      for (int i = -2; i <= 2; i++) {
        for (int j = -2; j <= 2; ++j) {
          if (check(num.F + i, num.S + j)) {
            used[num.F + i][num.S + j] = 1;
            q.push({num.F + i, num.S + j});
          }
        }
      }
    }
  }
  cout << -1;
}

int main() {
  Speed();
  int T = 1;
  // cin>>T;
  while (T--)
    solve();
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define sz(x) (int)((x).size())
#define F first
#define S second
#define pb push_back
#define ub upper_bound
#define lb lower_bound
#define all(x) x.begin(), x.end()
#define pqueue priority_queue
#define mset multiset
#define umap unordered_map
#define Speed()                                                                \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define rep(i, n) for (int i = 0; i < n; ++i)
#define forn(i, n) for (int i = 1; i <= n; ++i)
#define lr(i, l, r)                                                            \\
  for (int i = l; (l > r ? i >= r : i <= r); (l > r ? --i : ++i))

using namespace std;

template <class T> void umin(T &a, T b) { a = min(a, b); }
template <class T> void umax(T &a, T b) { a = max(a, b); }

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int dx[] = {1, 0, -1, 0, -1, -1, 1, 1},
          dy[] = {0, 1, 0, -1, -1, 1, -1, 1};
const int mod = 1e9 + 7;
const ll N = 1e3 + 4;

int h, w;
char c[N][N];
int used[N][N];
pii A, B;

void solve() {
  cin >> h >> w;
  cin >> A.F >> A.S >> B.F >> B.S;
  for (int i = 1; i <= h; ++i) {
    for (int j = 1; j <= w; ++j) {
      cin >> c[i][j];
    }
  }
  queue<pii> q;
  used[A.F][A.S] = 1;
  q.push(A);
  int cnt = -1;
  auto check = [&](int x, int y) {
    return x >= 1 && x <= h && y >= 1 && y <= w && c[x][y] == '.' &&
           !used[x][y];
  };
  vector<pii> vec;
  while (!q.empty()) {
    cnt++;
    while (!q.empty()) {
      pii v = q.front();
      vec.pb(v);
      if (v == B) {
        cout << cnt;
        return;
      }
      q.pop();
      for (int i = 0; i < 4; ++i) {
        int x = v.F + dx[i], y = v.S + dy[i];
        if (check(x, y)) {
          used[x][y] = 1;
          q.push({x, y});
        }
      }
    }
    for (auto num : vec) {
      for (int i = -2; i <= 2; i++) {
        for (int j = -2; j <= 2; ++j) {
          if (check(num.F + i, num.S + j)) {
            used[num.F + i][num.S + j] = 1;
            q.push({num.F + i, num.S + j});
          }
        }
      }
    }
  }
  cout << -1;
}

int main() {
  Speed();
  int T = 1;
  // cin>>T;
  while (T--)
    solve();
  return 0;
}