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

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

#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
const ll mxx = 3001;

int id[mxx][mxx];
bool vis[mxx][mxx];
string grid[mxx];
map<int, int> ans;
int num;
int a, b, x, y, n, m;
map<int, set<int>> adj;

bool in(int i, int j) {
  if (i < 0 || j < 0 || i >= n || j >= m)
    return false;
  return true;
}

void dfs(int i, int j) {
  vis[i][j] = true;
  id[i][j] = num;
  if (in(i + 1, j) && grid[i + 1][j] == '.' && !vis[i + 1][j])
    dfs(i + 1, j);
  if (in(i - 1, j) && grid[i - 1][j] == '.' && !vis[i - 1][j])
    dfs(i - 1, j);
  if (in(i, j + 1) && grid[i][j + 1] == '.' && !vis[i][j + 1])
    dfs(i, j + 1);
  if (in(i, j - 1) && grid[i][j - 1] == '.' && !vis[i][j - 1])
    dfs(i, j - 1);
}

int main() {
  fast

          cin >>
      n >> m >> a >> b >> x >> y;
  a--;
  b--;
  x--;
  y--;
  for (int i = 0; i < n; i++)
    cin >> grid[i];

  if (grid[x][y] == '#') {
    cout << -1;
    return 0;
  }
  memset(vis, false, sizeof vis);
  num = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (grid[i][j] == '.' && !vis[i][j]) {
        num++;
        dfs(i, j);
      }
    }
  }

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (grid[i][j] == '#')
        continue;
      for (int k = -2; k <= 2; k++) {
        for (int t = -2; t <= 2; t++) {
          if (abs(t) + abs(k) <= 1)
            continue;
          if (in(i + k, j + t) && grid[i + k][j + t] == '.' &&
              id[i + k][j + t] != id[i][j]) {
            adj[id[i + k][j + t]].insert(id[i][j]);
            adj[id[i][j]].insert(id[i + k][j + t]);
          }
        }
      }
    }
  }

  queue<pair<int, int>> q;
  q.push({id[x][y], 0});
  ans[id[x][y]] = 0;
  while (!q.empty()) {
    pair<int, int> cur = q.front();
    q.pop();
    for (auto x : adj[cur.first]) {
      if (!ans.count(x)) {
        ans[x] = cur.second + 1;
        q.push({x, cur.second + 1});
      }
    }
  }
  if (!ans.count(id[a][b]))
    cout << -1;
  else
    cout << ans[id[a][b]];
}

Error: TLE

Fixed submission:

#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
#include <bits/stdc++.h>
#include <chrono>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std::chrono;
using namespace std;
typedef long long ll;
using namespace __gnu_pbds;
#define ordered_set                                                            \\
  tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>
#define ordered_mset                                                           \\
  tree<pair<ll, ll>, null_type, less<pair<ll, ll>>, rb_tree_tag,               \\
       tree_order_statistics_node_update>
#define fr(i, a, b) for (ll i = (a); i < (b); ++i)
#define frr(i, a, b) for (ll i = (a - 1); i >= (b); --i)
#define vi vector<int>
#define vl vector<ll>
#define pb push_back
#define ppb pop_back()
#define pf push_front
#define ppf pop_front()
#define F first
#define S second
#define pii pair<ll, ll>
#define rev(a) reverse(a.begin(), a.end())
#define srt(a) sort(a.begin(), a.end())
#define rsrt(a) sort(a.begin(), a.end(), greater<ll>())
#define sz(a) (ll) a.size()
#define endl "\\n"
#define po(a, b) (ll)(pow(a, b) + 0.5)
#define double long double
#define print(a)                                                               \\
  for (auto x : a)                                                             \\
    cout << x << " ";                                                          \\
  cout << endl;
#define setbits(a) __builtin_popcount(a)
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
const ll M = 1e9 + 7;
const ll mxx = 1e6 + 5;
string grid[1001];
int n, m, cn, cm, fn, fm;
int vis[1001][1001];
bool done[1001][1001];
int ans;

bool in(int i, int j) {
  if (i < 0 || j < 0 || i >= n || j >= m)
    return false;
  return true;
}

void dfs(int i, int j, int move) {
  if (move >= ans)
    return;
  if (i == fn && j == fm) {
    ans = min(ans, move);
    return;
  }

  vis[i][j] = move;
  int a[4] = {};
  if (in(i + 1, j) && vis[i + 1][j] > move && grid[i + 1][j] == '.') {
    a[0] = 1;
    vis[i + 1][j] = move;
  }
  if (in(i, j + 1) && vis[i][j + 1] > move && grid[i][j + 1] == '.') {
    a[1] = 1;
    vis[i][j + 1] = move;
  }
  if (in(i - 1, j) && vis[i - 1][j] > move && grid[i - 1][j] == '.') {
    a[2] = 1;
    vis[i - 1][j] = move;
  }
  if (in(i, j - 1) && vis[i][j - 1] > move && grid[i][j - 1] == '.') {
    a[3] = 1;
    vis[i][j - 1] = move;
  }

  if (in(i + 1, j) && grid[i + 1][j] == '.' && a[0])
    dfs(i + 1, j, move);
  if (in(i, j + 1) && grid[i][j + 1] == '.' && a[1])
    dfs(i, j + 1, move);
  if (in(i - 1, j) && grid[i - 1][j] == '.' && a[2])
    dfs(i - 1, j, move);
  if (in(i, j - 1) && grid[i][j - 1] == '.' && a[3])
    dfs(i, j - 1, move);

  for (int ii = -2; ii <= 2; ii++) {
    for (int jj = -2; jj <= 2; jj++) {
      if (abs(ii) + abs(jj) <= 1)
        continue;
      if (in(i + ii, j + jj) && vis[ii + i][jj + j] > move + 1 &&
          grid[ii + i][jj + j] == '.')
        dfs(ii + i, jj + j, move + 1);
    }
  }
}
int main() {
  fast cin >> n >> m;
  cin >> cn >> cm;
  cin >> fn >> fm;
  fn--;
  fm--;
  cn--;
  cm--;
  for (int i = 0; i < n; i++)
    cin >> grid[i];
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      vis[i][j] = M;
  memset(done, false, sizeof done);
  ans = M;
  vis[cn][cm] = 0;
  if (grid[fn][fm] == '#') {
    cout << -1;
    return 0;
  }
  dfs(cn, cm, 0);
  if (ans == M)
    cout << -1;
  else
    cout << ans;
}