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

#include <bits/stdc++.h>
using namespace std;
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
#define _start                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define pb push_back
#define ss second
#define ff first
#define ll long long
#define mk make_pair
#define ld long double
#define vi vector<int>
#define pi pair<int, int>
#define mod 1000000007
#define pqg priority_queue<int>
#define pqs priority_queue<int, vi, greater<int>>
#define forn(p, n) for (int i = p; i <= n; i++)
#define w(t)                                                                   \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)
#define kick(t)                                                                \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  for (int p = 0; p < t; p++)
// typedef tree<ll, null_type, less<ll>, rb_tree_tag,
// tree_order_statistics_node_update> oset;

int n, m, ci, cj, di, dj;
string arr[1001];
int dp[1001][1001];
bool vis[1001][1001];

void bfs(int i, int j, int ct) {
  if (i < 0 || i >= n || j < 0 || j >= m || vis[i][j] || arr[i][j] == '#') {
    return;
  }
  vis[i][j] = true;
  dp[i][j] = ct;

  bfs(i + 1, j, ct);
  bfs(i, j + 1, ct);
  bfs(i - 1, j, ct);
  bfs(i, j - 1, ct);
}

int32_t main() {
  _start cin >> n >> m;
  cin >> ci >> cj;
  cin >> di >> dj;

  string str;

  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  int count = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (!vis[i][j] && arr[i][j] == '.')
        bfs(i, j, ++count);
    }
  }
  unordered_set<int> adj[count + 1];

  ci--, cj--, di--, dj--;
  int src = dp[ci][cj], dest = dp[di][dj];

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (arr[i][j] == '.') {

        for (int k = max(0, i - 2); k <= min(n - 1, i + 2); k++) {
          for (int t = max(0, j - 2); t <= min(m - 1, j + 2); t++) {
            if (arr[k][t] == '.' && dp[i][j] != dp[k][t]) {
              adj[dp[i][j]].insert(dp[k][t]);
            }
          }
        }
      }
    }
  }
  queue<pi> q;
  vector<int> vis2(count + 1, 0);
  q.push({src, 0});
  vis2[src] = 1;
  int ans = -1;
  while (!q.empty()) {
    pi cur = q.front();
    q.pop();
    if (cur.ff == dest) {
      ans = cur.ss;
      break;
    }
    for (auto a : adj[cur.ff]) {
      if (!vis2[a]) {
        vis2[a] = 1;
        q.push(make_pair(a, cur.ss + 1));
      }
    }
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
#define _start                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define pb push_back
#define ss second
#define ff first
#define ll long long
#define mk make_pair
#define ld long double
#define vi vector<int>
#define pi pair<int, int>
#define mod 1000000007
#define pqg priority_queue<int>
#define pqs priority_queue<int, vi, greater<int>>
#define forn(p, n) for (int i = p; i <= n; i++)
#define w(t)                                                                   \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)
#define kick(t)                                                                \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  for (int p = 0; p < t; p++)
// typedef tree<ll, null_type, less<ll>, rb_tree_tag,
// tree_order_statistics_node_update> oset;
ll powmod(ll base, ll exp, ll MOD = mod) {
  ll res = 1;
  while (exp > 0) {
    if (exp % 2 == 1)
      res = (res * base) % MOD;
    base = (base * base) % MOD;
    exp /= 2;
  }
  return (res % MOD);
}
ll mul_inv(ll a, ll b = mod) {
  ll t1 = a, t2 = b, t3, v1 = 1, v2 = 0, v3;
  while (t2 != 1) {
    ll x = t1 / t2;
    t3 = t1 - x * t2;
    v3 = v1 - x * v2;
    t1 = t2, t2 = t3;
    v1 = v2, v2 = v3;
  }
  return (v2 + b) % b;
}

int n, m, ci, cj, di, dj;
string arr[1001];
int dp[1001][1001];
bool vis[1001][1001];

void bfs(int i, int j, int ct) {
  if (i < 0 || i >= n || j < 0 || j >= m || vis[i][j] || arr[i][j] == '#') {
    return;
  }
  vis[i][j] = true;
  dp[i][j] = ct;

  bfs(i + 1, j, ct);
  bfs(i, j + 1, ct);
  bfs(i - 1, j, ct);
  bfs(i, j - 1, ct);
}

int32_t main() {
  _start cin >> n >> m;
  cin >> ci >> cj;
  cin >> di >> dj;

  string str;

  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  int count = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (!vis[i][j] && arr[i][j] == '.')
        bfs(i, j, ++count);
    }
  }
  vector<unordered_set<int>> adj(count + 1, unordered_set<int>());

  ci--, cj--, di--, dj--;
  int src = dp[ci][cj], dest = dp[di][dj];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (arr[i][j] == '.') {

        for (int k = max(0, i - 2); k <= min(n - 1, i + 2); k++) {
          for (int t = max(0, j - 2); t <= min(m - 1, j + 2); t++) {
            if (arr[k][t] == '.' && dp[i][j] != dp[k][t]) {
              adj[dp[i][j]].insert(dp[k][t]);
            }
          }
        }
      }
    }
  }
  queue<pi> q;
  vector<bool> vis(count + 1, 0);
  q.push({src, 0});
  int ans = -1;
  while (!q.empty()) {
    pi cur = q.front();
    q.pop();
    vis[cur.ff] = true;
    if (cur.ff == dest) {
      ans = cur.ss;
      break;
    }
    for (auto a : adj[cur.ff]) {
      if (!vis[a]) {
        q.push(make_pair(a, cur.ss + 1));
      }
    }
  }
  cout << ans << endl;
}