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

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define mp make_pair
#define deb(x) cout << #x << " " << x << "\\n";
#define MAX 9223372036854775807
#define MIN -9223372036854775807
#define PI 3.141592653589
#define setbits(n) __builtin_popcountll(n)
const ll mod = 1e9 + 7;

ll x[4] = {1, 0, -1, 0};
ll y[4] = {0, 1, 0, -1};
ll n, m;

bool legal(ll x, ll y) { return (x >= 0 && y >= 0 && x < n && y < m); }

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll T = clock();

  ll t = 1;
  // cin>>t;
  while (t--) {
    ll cx, cy, dx, dy;
    cin >> n >> m >> cx >> cy >> dx >> dy;
    cx--, cy--, dx--, dy--;
    vector<string> s(n);
    for (ll i = 0; i < n; i++)
      cin >> s[i];
    deque<pair<ll, ll>> dq;
    vector<vector<ll>> d(n, vector<ll>(m, mod));
    d[cx][cy] = 0;
    dq.push_front({cx, cy});
    while (!dq.empty()) {
      pair<ll, ll> p = dq.front();
      dq.pop_front();
      for (int i = 0; i < 4; i++) {
        ll a = p.first + x[i], b = p.second + y[i];
        if (a < 0 || b < 0 || a >= n || b >= m)
          continue;
        if (d[a][b] > d[p.first][p.second] && s[a][b] == '.') {
          dq.push_front({a, b});
          d[a][b] = d[p.first][p.second];
        }
      }
      for (int i = p.first - 2; i < p.first + 3; i++) {
        for (int j = p.second - 2; j < p.second + 3; j++) {
          if (i < 0 || j < 0 || i >= n || j >= m)
            continue;
          if (d[i][j] > d[p.first][p.second] + 1 && s[i][j] == '.') {
            dq.push_back({i, j});
            d[i][j] = d[p.first][p.second] + 1;
          }
        }
      }
    }
    if (d[dx][dy] == mod)
      cout << -1;
    else
      cout << d[dx][dy];
  }

  cerr << "\\n\\nTIME: " << (double)(clock() - T) / CLOCKS_PER_SEC << " sec\\n";
  T = clock();
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define mp make_pair
#define deb(x) cout << #x << " " << x << "\\n";
#define MAX 9223372036854775807
#define MIN -9223372036854775807
#define PI 3.141592653589
#define setbits(n) __builtin_popcountll(n)
const ll mod = 1e9 + 7;

const int N = 1e3 + 1;
ll vis[N][N];
vector<string> a;
ll xx[4] = {1, 0, -1, 0};
ll yy[4] = {0, 1, 0, -1};
ll n, m;

bool isvalid(ll x, ll y) {
  if (x < 0 || x >= n || y < 0 || y >= m)
    return false;
  return true;
}

void bfs(ll x, ll y, ll val) {
  vector<vector<bool>> v(n, vector<bool>(m));
  queue<pair<ll, ll>> q;
  q.push({x, y});
  vis[x][y] = val;
  v[x][y] = true;
  while (!q.empty()) {
    pair<ll, ll> p = q.front();
    q.pop();
    ll c1 = p.first, c2 = p.second;
    for (ll i = 0; i < 4; i++) {
      ll d1 = c1 + xx[i], d2 = c2 + yy[i];
      if (!isvalid(d1, d2))
        continue;
      if (a[d1][d2] == '.' && !v[d1][d2]) {
        v[d1][d2] = true;
        vis[d1][d2] = val;
        q.push({d1, d2});
      }
    }
  }
}

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll T = clock();

  cin >> n >> m;
  ll c1, c2, d1, d2;
  cin >> c1 >> c2 >> d1 >> d2;
  c1--, c2--, d1--, d2--;
  for (ll i = 0; i < n; i++) {
    string s;
    cin >> s;
    a.pb(s);
  }
  for (ll i = 0; i < n; i++)
    for (ll j = 0; j < m; j++)
      vis[i][j] = -1;
  ll r = 0;
  for (ll i = 0; i < n; i++) {
    for (ll j = 0; j < m; j++) {
      if (vis[i][j] == -1 && a[i][j] == '.') {
        bfs(i, j, r);
        r++;
      }
    }
  }
  // cout<<r<<" ";
  set<pair<ll, ll>> ss;
  vector<vector<ll>> adj(r);
  for (ll i = 0; i < n; i++) {
    for (ll j = 0; j < m; j++) {
      for (ll x = max(0ll, i - 2); x < min(n, i + 3); x++) {
        for (ll y = max(0ll, j - 2); y < min(m, j + 3); y++) {
          ll st = vis[i][j], ed = vis[x][y];
          if (st == ed || st == -1 || ed == -1)
            continue;
          // cout<<st<<" "<<ed<<"\\n";
          if (st > ed)
            swap(st, ed);
          if (ss.find({st, ed}) != ss.end())
            continue;
          adj[st].pb(ed);
          adj[ed].pb(st);
          ss.insert({st, ed});
        }
      }
    }
  }
  ll st = vis[c1][c2];
  queue<ll> q;
  vector<bool> vis1(r);
  vector<ll> h(r, -1);
  vis1[st] = true;
  h[st] = 0;
  q.push(st);
  while (!q.empty()) {
    ll v = q.front();
    q.pop();
    for (ll u : adj[v]) {
      if (!vis1[u]) {
        vis1[u] = true;
        q.push(u);
        h[u] = h[v] + 1;
      }
    }
  }
  ll ed = vis[d1][d2];
  cout << h[ed];

  cerr << "\\n\\nTIME: " << (double)(clock() - T) / CLOCKS_PER_SEC << " sec\\n";
  T = clock();
  return 0;
}