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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define repA(i, a, n) for (ll i = a; i < (n); ++i)
#define repD(i, a, n) for (ll i = a; i > (n); --i)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (long long)(x).size()
#define fill(a) memset(a, 0, sizeof(a))
#define fst first
#define snd second
// #define mp make_pair
#define pb push_back
void __print(int x) { cout << x; }
void __print(long x) { cout << x; }
void __print(long long x) { cout << x; }
void __print(unsigned x) { cout << x; }
void __print(unsigned long x) { cout << x; }
void __print(unsigned long long x) { cout << x; }
void __print(float x) { cout << x; }
void __print(double x) { cout << x; }
void __print(long double x) { cout << x; }
void __print(char x) { cout << '\\'' << x << '\\''; }
void __print(const char *x) { cout << '\\"' << x << '\\"'; }
void __print(const string &x) { cout << '\\"' << x << '\\"'; }
void __print(bool x) { cout << (x ? "true" : "false"); }
template <typename T, typename V> void __print(const pair<T, V> &x) {
  cout << '{';
  __print(x.first);
  cout << ',';
  __print(x.second);
  cout << '}';
}
template <typename T> void __print(const T &x) {
  int f = 0;
  cout << '{';
  for (auto &i : x)
    cout << (f++ ? "," : ""), __print(i);
  cout << "}";
}
void _print() { cout << "]\\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cout << ", ";
  _print(v...);
}
#ifndef ONLINE_JUDGE
#define dbg(x...)                                                              \\
  cout << "[" << #x << "] = [";                                                \\
  _print(x)
#else
#define dbg(x...)
#endif
const long double PI = 3.141592653589793238462643383;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<long long int> vll;
typedef vector<double> vd;
typedef vector<pii> vii;
long long Ceil(long long a, long long b) {
  if (a % b == 0)
    return a / b;
  else
    return (a / b) + 1;
}
ll p = 1000000007;
float E = 0.00000000000001;
ll power(ll x, ll y) {
  if (y == 0) {
    return 1;
  }
  if (y % 2 == 0) {
    ll q = power(x, y / 2);
    q = q % p;
    return (q * q) % p;
  }
  ll q = power(x, y / 2);
  q = q % p;
  return ((x % p) * ((q * q) % p)) % p;
}
bool isPowerOfTwo(long long x) {
  /* First x in the below expression is for the case when x is 0 */
  return x && (!(x & (x - 1)));
}

long long int gcd(long long int a, long long int b) {
  if (a == 0)
    return b;
  return gcd(b % a, a);
}
long long int mod(long long int a, long long int b) {
  long long int ret = a % b;
  return ret >= 0 ? ret : ret + b;
}
bool proper(int i, int j, int n, int m) {
  return i >= 0 && j >= 0 && i < n && j < m;
}
void dfs_visit(int i, int j, vector<string> &vec, vector<vector<int>> &visited,
               vector<vector<int>> &ids, int id) {
  if (!proper(i, j, vec.size(), vec[0].size()) || vec[i][j] == '#' ||
      visited[i][j])
    return;
  visited[i][j] = 1;
  ids[i][j] = id;
  dfs_visit(i + 1, j, vec, visited, ids, id);
  dfs_visit(i - 1, j, vec, visited, ids, id);
  dfs_visit(i, j + 1, vec, visited, ids, id);
  dfs_visit(i, j - 1, vec, visited, ids, id);
}
int main() {
  std::ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int t = 1;
  // cin>>t;
  while (t--) {
    int h, w, ch, cw, dh, dw;
    cin >> h >> w >> ch >> cw >> dh >> dw;
    ch--;
    cw--;
    dh--;
    dw--;
    vector<string> vec(h);
    for (int i = 0; i < h; i++) {
      string s;
      cin >> s;
      vec[i] = s;
    }
    vector<vector<int>> visited(h, vector<int>(w, 0));
    vector<vector<int>> ids(h, vector<int>(w, -1));
    int count = 0;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (!visited[i][j] && vec[i][j] != '#') {
          dfs_visit(i, j, vec, visited, ids, count);
          count++;
        }
      }
    }
    // dbg(ids);
    // dbg(count);
    // vector <unordered_set<int>> adj(count);
    // vector<vector<int>> adj2(count);
    vector<vector<int>> adj(count);
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (vec[i][j] == '#')
          continue;
        for (int k = -2; k <= 2; k++) {
          for (int l = -2; l <= 2; l++) {
            if (!proper(i + k, j + l, h, w) || vec[i + k][j + l] == '#' ||
                ids[i + k][j + l] == ids[i][j])
              continue;
            // dbg(ids[i][j]);
            // adj[ids[i][j]].insert(ids[i+k][j+l]);
            // adj[1].insert(ids[i+k][j+l]);
            // adj2[ids[i][j]].push_back(ids[i+k][j+l]);
            adj[ids[i][j]].push_back(ids[i + k][j + l]);
          }
        }
      }
    }
    // dbg(adj);
    // vector <unordered_set<int>> adj(count);
    // for(int i = 0 ; i < count ; i++){
    // unordered_set<int> s(adj2[i].begin(), adj2[i].end());
    // adj[i] = s;
    // adj[i] = unordered_set<int>(adj2[i].begin(), adj2[i].end());
    // }
    int src = ids[ch][cw], dest = ids[dh][dw];
    queue<int> q;
    q.push(src);
    if (src == dest) {
      cout << 0 << endl;
      continue;
    }
    int ans = 0;
    vector<int> visited2(count, 0);
    while (!q.empty()) {
      int sz = q.size();
      ans++;
      for (int i = 0; i < sz; i++) {
        int node = q.front();
        q.pop();
        if (visited2[node])
          continue;
        visited2[node] = 1;
        for (auto &x : adj[node]) {
          if (x == dest) {
            cout << ans << endl;
            return 0;
          }
          q.push(x);
        }
      }
    }
    cout << -1 << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define repA(i, a, n) for (ll i = a; i < (n); ++i)
#define repD(i, a, n) for (ll i = a; i > (n); --i)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (long long)(x).size()
#define fill(a) memset(a, 0, sizeof(a))
#define fst first
#define snd second
// #define mp make_pair
#define pb push_back
void __print(int x) { cout << x; }
void __print(long x) { cout << x; }
void __print(long long x) { cout << x; }
void __print(unsigned x) { cout << x; }
void __print(unsigned long x) { cout << x; }
void __print(unsigned long long x) { cout << x; }
void __print(float x) { cout << x; }
void __print(double x) { cout << x; }
void __print(long double x) { cout << x; }
void __print(char x) { cout << '\\'' << x << '\\''; }
void __print(const char *x) { cout << '\\"' << x << '\\"'; }
void __print(const string &x) { cout << '\\"' << x << '\\"'; }
void __print(bool x) { cout << (x ? "true" : "false"); }
template <typename T, typename V> void __print(const pair<T, V> &x) {
  cout << '{';
  __print(x.first);
  cout << ',';
  __print(x.second);
  cout << '}';
}
template <typename T> void __print(const T &x) {
  int f = 0;
  cout << '{';
  for (auto &i : x)
    cout << (f++ ? "," : ""), __print(i);
  cout << "}";
}
void _print() { cout << "]\\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cout << ", ";
  _print(v...);
}
#ifndef ONLINE_JUDGE
#define dbg(x...)                                                              \\
  cout << "[" << #x << "] = [";                                                \\
  _print(x)
#else
#define dbg(x...)
#endif
const long double PI = 3.141592653589793238462643383;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<long long int> vll;
typedef vector<double> vd;
typedef vector<pii> vii;
long long Ceil(long long a, long long b) {
  if (a % b == 0)
    return a / b;
  else
    return (a / b) + 1;
}
ll p = 1000000007;
float E = 0.00000000000001;
ll power(ll x, ll y) {
  if (y == 0) {
    return 1;
  }
  if (y % 2 == 0) {
    ll q = power(x, y / 2);
    q = q % p;
    return (q * q) % p;
  }
  ll q = power(x, y / 2);
  q = q % p;
  return ((x % p) * ((q * q) % p)) % p;
}
bool isPowerOfTwo(long long x) {
  /* First x in the below expression is for the case when x is 0 */
  return x && (!(x & (x - 1)));
}

long long int gcd(long long int a, long long int b) {
  if (a == 0)
    return b;
  return gcd(b % a, a);
}
long long int mod(long long int a, long long int b) {
  long long int ret = a % b;
  return ret >= 0 ? ret : ret + b;
}
bool proper(int i, int j, int n, int m) {
  return i >= 0 && j >= 0 && i < n && j < m;
}
void dfs_visit(int i, int j, vector<string> &vec, vector<vector<int>> &visited,
               vector<vector<int>> &ids, int id) {
  if (!proper(i, j, vec.size(), vec[0].size()) || vec[i][j] == '#' ||
      visited[i][j])
    return;
  visited[i][j] = 1;
  ids[i][j] = id;
  dfs_visit(i + 1, j, vec, visited, ids, id);
  dfs_visit(i - 1, j, vec, visited, ids, id);
  dfs_visit(i, j + 1, vec, visited, ids, id);
  dfs_visit(i, j - 1, vec, visited, ids, id);
}
int main() {
  std::ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int t = 1;
  // cin>>t;
  while (t--) {
    int h, w, ch, cw, dh, dw;
    cin >> h >> w >> ch >> cw >> dh >> dw;
    ch--;
    cw--;
    dh--;
    dw--;
    vector<string> vec(h);
    for (int i = 0; i < h; i++) {
      string s;
      cin >> s;
      vec[i] = s;
    }
    vector<vector<int>> visited(h, vector<int>(w, 0));
    vector<vector<int>> ids(h, vector<int>(w, -1));
    int count = 0;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (!visited[i][j] && vec[i][j] != '#') {
          dfs_visit(i, j, vec, visited, ids, count);
          count++;
        }
      }
    }
    // dbg(ids);
    // dbg(count);
    // vector <unordered_set<int>> adj(count);
    vector<vector<int>> adj2(count);
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (vec[i][j] == '#')
          continue;
        for (int k = -2; k <= 2; k++) {
          for (int l = -2; l <= 2; l++) {
            if (!proper(i + k, j + l, h, w) || vec[i + k][j + l] == '#' ||
                ids[i + k][j + l] == ids[i][j])
              continue;
            // dbg(ids[i][j]);
            // adj[ids[i][j]].insert(ids[i+k][j+l]);
            // adj[1].insert(ids[i+k][j+l]);
            adj2[ids[i][j]].push_back(ids[i + k][j + l]);
          }
        }
      }
    }
    // dbg(adj);
    vector<unordered_set<int>> adj(count);
    for (int i = 0; i < count; i++) {
      // unordered_set<int> s(adj2[i].begin(), adj2[i].end());
      // adj[i] = s;
      adj[i] = unordered_set<int>(adj2[i].begin(), adj2[i].end());
    }
    int src = ids[ch][cw], dest = ids[dh][dw];
    queue<int> q;
    q.push(src);
    if (src == dest) {
      cout << 0 << endl;
      continue;
    }
    int ans = 0;
    while (!q.empty()) {
      int sz = q.size();
      ans++;
      for (int i = 0; i < sz; i++) {
        int node = q.front();
        q.pop();
        for (auto &x : adj[node]) {
          if (x == dest) {
            cout << ans << endl;
            return 0;
          }
          q.push(x);
        }
      }
    }
    cout << -1 << endl;
  }
  return 0;
}