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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define int long long
#define endl '\\n'
#define pb push_back
#define fi first
#define se second
#define all(c) (c).begin(), (c).end()
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;

template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define TRACE
#ifndef ONLINE_JUDGE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define trace(...)
#endif

const ll inf = 2e18;
const int mod = 1e9 + 7;
const int N = 2e5 + 10;

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

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

  int h, w;
  cin >> h >> w;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  x2--;
  y1--;
  y2--;
  string s[h];
  for (int i = 0; i < h; i++) {
    cin >> s[i];
  }
  int d[h][w];
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      d[i][j] = inf;
    }
  }
  bool vis[h][w];
  memset(vis, 0, sizeof(vis));
  queue<pii> q;
  d[x1][y1] = 0;
  vis[x1][y1] = 1;
  vector<pii> have;
  have.pb({x1, y1});
  while (!have.empty()) {
    vector<pii> vv;
    for (auto it : have) {
      q.push(it);
    }
    have.clear();
    while (!q.empty()) {
      pii cur = q.front();
      vv.pb(cur);
      q.pop();
      for (int i = 0; i < 4; i++) {
        int tx = cur.fi + dx[i];
        int ty = cur.se + dy[i];
        if (tx >= 0 && tx < h && ty >= 0 && ty < w && !vis[tx][ty] &&
            s[tx][ty] == '.') {
          d[tx][ty] = min(d[tx][ty], d[cur.fi][cur.se]);
          q.push({tx, ty});
          vis[tx][ty] = 1;
        }
      }
    }
    for (auto cur : vv) {
      for (int i = -2; i <= 2; i++) {
        for (int j = -2; j <= 2; j++) {
          int tx = cur.fi + i;
          int ty = cur.se + j;
          if (tx >= 0 && tx < h && ty >= 0 && ty < w && !vis[tx][ty] &&
              s[tx][ty] == '.') {
            d[tx][ty] = min(d[tx][ty], d[cur.fi][cur.se] + 1);
            vis[tx][ty] = 1;
            have.pb({tx, ty});
          }
        }
      }
    }
  }
  if (d[x2][y2] == inf) {
    d[x2][y2] = -1;
  }
  cout << d[x2][y2];

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define int long long
#define endl '\\n'
#define pb push_back
#define fi first
#define se second
#define all(c) (c).begin(), (c).end()
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;

template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define TRACE
#ifndef ONLINE_JUDGE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define trace(...)
#endif

const ll inf = 2e18;
const int mod = 1e9 + 7;
const int N = 2e5 + 10;

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

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

  int h, w;
  cin >> h >> w;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  x2--;
  y1--;
  y2--;
  string s[h];
  for (int i = 0; i < h; i++) {
    cin >> s[i];
  }
  int d[h][w];
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      d[i][j] = inf;
    }
  }
  bool vis[h][w];
  memset(vis, 0, sizeof(vis));
  queue<pii> q;
  d[x2][y2] = 0;
  set<pii> have;
  have.insert({x2, y2});
  while (!have.empty()) {
    for (auto it : have) {
      q.push(it);
    }
    have.clear();
    while (!q.empty()) {
      pii cur = q.front();
      q.pop();
      for (int i = -2; i <= 2; i++) {
        for (int j = -2; j <= 2; j++) {
          if (i == 0 && j == 0) {
            continue;
          }
          int tx = cur.fi + i;
          int ty = cur.se + j;
          if (tx >= 0 && tx < h && ty >= 0 && ty < w && !vis[tx][ty] &&
              s[tx][ty] == '.') {
            d[tx][ty] = min(d[tx][ty], d[cur.fi][cur.se] + 1);
            have.insert({tx, ty});
          }
        }
      }
      for (int i = 0; i < 4; i++) {
        int tx = cur.fi + dx[i];
        int ty = cur.se + dy[i];
        if (tx >= 0 && tx < h && ty >= 0 && ty < w && !vis[tx][ty] &&
            s[tx][ty] == '.') {
          d[tx][ty] = min(d[tx][ty], d[cur.fi][cur.se]);
          q.push({tx, ty});
          vis[tx][ty] = 1;
          if (!have.empty()) {
            auto it = have.find({tx, ty});
            if (it != have.end()) {
              have.erase(it);
            }
          }
        }
      }
    }
    // trace(have.size());
  }
  if (d[x1][y1] == inf) {
    d[x1][y1] = -1;
  }
  cout << d[x1][y1];

  return 0;
}