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

#include "cmath"
#include "unordered_map"
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>

using namespace std;

struct pair_hash {
  long long hash_seed = (int)1e9 + 7;
  long long operator()(const pair<int, int> &x) const {
    return hash_seed * x.first + x.second;
  }
};

typedef unordered_map<pair<int, int>, int, pair_hash> megamap;
pair<int, int> go[4] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
vector<int> graph[(int)1e6 + 7];
void explore(int k, megamap &belong, map<pair<int, int>, bool> &g,
             pair<int, int> v, vector<pair<int, int>> &carry) {
  if (!g[v])
    return;
  else {
    carry.push_back(v);
    for (auto x : go) {
      g[v] = false;
      belong[v] = k;
      explore(k, belong, g, {v.first + x.first, v.second + x.second}, carry);
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  int ch, cw;
  cin >> ch >> cw;
  int dh, dw;
  cin >> dh >> dw;
  map<pair<int, int>, bool> x;
  for (int i = 0; i < n; i++) {
    string g;
    cin >> g;
    for (int j = 0; j < m; j++) {
      x[{i + 1, j + 1}] = g[j] == '.';
    }
  }
  unordered_map<pair<int, int>, int, pair_hash> belong;
  vector<vector<pair<int, int>>> comps;
  int k = 1;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (x[{i, j}]) {
        vector<pair<int, int>> new_comp;
        explore(k, belong, x, {i, j}, new_comp);
        comps.push_back(new_comp);
        ++k;
      }
    }
  }

  for (auto &v : comps) {
    for (auto c : v) {
      for (int i = max(1, c.first - 2); i <= min(n, c.first + 2); ++i) {
        for (int j = max(1, c.second - 2); j <= min(m, c.second + 2); ++j) {
          if (belong[{i, j}] != 0 && belong[{i, j}] != belong[c]) {
            graph[belong[c]].push_back(belong[{i, j}]);
            graph[belong[{i, j}]].push_back(belong[c]);
            ;
          }
        }
      }
    }
  }
  vector<int> used(comps.size() + 1, 0);
  vector<int> d(comps.size() + 1, (int)1e9 + 7);
  queue<int> bfs;
  bfs.push(belong[{ch, cw}]);
  d[belong[{ch, cw}]] = 0;
  while (!bfs.empty()) {
    int v = bfs.front();
    bfs.pop();
    used[v] = true;
    for (auto to : graph[v]) {
      if (!used[to]) {
        bfs.push(to);
        used[to] = true;
        d[to] = d[v] + 1;
      }
    }
  }
  if (d[belong[{dh, dw}]] == (int)1e9 + 7) {
    printf("-1\\n");
  } else
    printf("%d", d[belong[{dh, dw}]]);
}

Error: TLE

Fixed submission:

#include "cmath"
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>

using namespace std;

typedef unsigned u;
pair<int, int> go[4] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
vector<int> graph[(int)1e6 + 7];
void explore(int k, map<pair<int, int>, int> &belong,
             map<pair<int, int>, bool> &g, pair<int, int> v,
             vector<pair<int, int>> &carry) {
  if (!g[v])
    return;
  else {
    carry.push_back(v);
    for (auto x : go) {
      g[v] = false;
      belong[v] = k;
      explore(k, belong, g, {v.first + x.first, v.second + x.second}, carry);
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  int ch, cw;
  cin >> ch >> cw;
  int dh, dw;
  cin >> dh >> dw;
  map<pair<int, int>, bool> x;
  for (int i = 0; i < n; i++) {
    string g;
    cin >> g;
    for (int j = 0; j < m; j++) {
      x[{i + 1, j + 1}] = g[j] == '.';
    }
  }
  map<pair<int, int>, int> belong;
  vector<vector<pair<int, int>>> comps;
  int k = 1;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (x[{i, j}]) {
        vector<pair<int, int>> new_comp;
        explore(k, belong, x, {i, j}, new_comp);
        comps.push_back(new_comp);
        ++k;
      }
    }
  }

  for (auto &v : comps) {
    for (auto c : v) {
      for (int i = max(1, c.first - 2); i <= min(n, c.first + 2); ++i) {
        for (int j = max(1, c.second - 2); j <= min(m, c.second + 2); ++j) {
          if (belong[{i, j}] != 0 && belong[{i, j}] != belong[c]) {
            graph[belong[c]].push_back(belong[{i, j}]);
            graph[belong[{i, j}]].push_back(belong[c]);
            ;
          }
        }
      }
    }
  }
  vector<int> used(comps.size() + 1, 0);
  vector<int> d(comps.size() + 1, (int)1e9 + 7);
  queue<int> bfs;
  bfs.push(belong[{ch, cw}]);
  d[belong[{ch, cw}]] = 0;
  while (!bfs.empty()) {
    int v = bfs.front();
    bfs.pop();
    used[v] = true;
    for (auto to : graph[v]) {
      if (!used[to]) {
        bfs.push(to);
        used[to] = true;
        d[to] = d[v] + 1;
      }
    }
  }
  if (d[belong[{dh, dw}]] == (int)1e9 + 7) {
    printf("-1\\n");
  } else
    printf("%d", d[belong[{dh, dw}]]);
}