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

#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using pii = pair<int, int>;

#define vt vector
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define fi first
#define se second

vt<pii> adj{{1, 0}, {0, 1}, {-1, 0}, {0, -1}};

void solve(int tc) {
  int h, w;
  cin >> h >> w;
  int ch, cw, dh, dw;
  cin >> ch >> cw >> dh >> dw;
  ch--, cw--, dh--, dw--;
  vt<string> s(h);
  for (auto &i : s)
    cin >> i;

  vt<vt<bool>> vis(h, vt<bool>(w));
  queue<pii> q;
  vt<pii> curVis;
  vis[ch][cw] = true;
  q.emplace(ch, cw);
  for (int r = 0;; r++) {
    while (!q.empty()) {
      pii cur = q.front();
      q.pop();

      if (cur.fi == dh && cur.se == dw) {
        cout << r;
        return;
      }

      curVis.push_back(cur);

      for (pii &i : adj) {
        int x = cur.fi + i.fi, y = cur.se + i.se;
        if (x < 0 || y < 0 || x >= h || y >= w)
          continue;
        if (vis[x][y] || s[x][y] == '#')
          continue;
        vis[x][y] = true;
        q.emplace(x, y);
      }
    }

    if (curVis.empty()) {
      cout << -1;
      return;
    }

    for (pii cur : curVis) {
      for (int i = -2; i <= 2; i++)
        for (int j = -2; j <= 2; j++) {
          int x = cur.fi + i, y = cur.se + j;
          if (x < 0 || y < 0 || x >= h || y >= w)
            continue;
          if (vis[x][y] || s[x][y] == '#')
            continue;
          vis[x][y] = true;
          q.emplace(x, y);
        }
    }
    curVis.clear();
  }
}

signed main() {
  ios::sync_with_stdio(0), cin.tie(0);

  int tcs = 1;
  for (int tc = 1; tc <= tcs; tc++)
    solve(tc);
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using pii = pair<int, int>;

#define vt vector
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define fi first
#define se second

vt<pii> adj{{1, 0}, {0, 1}, {-1, 0}, {0, -1}};

void solve(int tc) {
  int h, w;
  cin >> h >> w;
  int ch, cw, dh, dw;
  cin >> ch >> cw >> dh >> dw;
  ch--, cw--, dh--, dw--;
  vt<string> s(h);
  for (auto &i : s)
    cin >> i;

  vt<vt<bool>> vis(h, vt<bool>(w));
  queue<pii> q;
  set<pii> tp;
  vis[ch][cw] = true;
  q.emplace(ch, cw);
  for (int r = 0;; r++) {
    while (!q.empty()) {
      pii cur = q.front();
      q.pop();

      if (cur.fi == dh && cur.se == dw) {
        cout << r;
        return;
      }

      for (pii &i : adj) {
        int x = cur.fi + i.fi, y = cur.se + i.se;
        if (x < 0 || y < 0 || x >= h || y >= w)
          continue;
        if (vis[x][y] || s[x][y] == '#')
          continue;
        vis[x][y] = true;
        q.emplace(x, y);
      }

      for (int i = -2; i <= 2; i++)
        for (int j = -2; j <= 2; j++) {
          int x = cur.fi + i, y = cur.se + j;
          if (x < 0 || y < 0 || x >= h || y >= w)
            continue;
          if (vis[x][y] || s[x][y] == '#')
            continue;
          tp.emplace(x, y);
        }
    }

    if (tp.empty()) {
      cout << -1;
      return;
    }

    for (pii i : tp) {
      if (vis[i.fi][i.se])
        continue;
      q.emplace(i);
    }
    tp.clear();
  }
}

signed main() {
  ios::sync_with_stdio(0), cin.tie(0);

  int tcs = 1;
  for (int tc = 1; tc <= tcs; tc++)
    solve(tc);
}