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

#include <bits/stdc++.h>

#ifdef DEBUG
#define LOG(var, sep) (cerr << #var << " = " << (var) << (sep))

#define LOGM(msg) (cerr << (msg))

#define LOGV(vec, sep)                                                         \\
  {                                                                            \\
    cerr << #vec << " =";                                                      \\
    for (const auto &v : (vec))                                                \\
      cerr << " " << v;                                                        \\
    cerr << (sep);                                                             \\
  }

#define LOGA(arr, N, sep)                                                      \\
  {                                                                            \\
    cerr << #arr << " =";                                                      \\
    for (int i = 0; i < N; ++i)                                                \\
      cerr << " " << (arr)[i];                                                 \\
    cerr << (sep);                                                             \\
  }

#define LOGA2(arr, N, M, sep)                                                  \\
  {                                                                            \\
    cerr << #arr << " =\\n";                                                    \\
    for (int i = 0; i < N; ++i) {                                              \\
      for (int j = 0; j < M; ++j)                                              \\
        cerr << (arr)[i][j] << " ";                                            \\
      cerr << endl;                                                            \\
    }                                                                          \\
    cerr << (sep);                                                             \\
  }
#else
#define LOG(var, sep)
#define LOGM(msg)
#define LOGV(vec, sep)
#define LOGA(arr, N, sep)
#define LOGA2(arr, N, M, sep)
#endif

using namespace std;
using ll = long long;
using ii = pair<int, int>;
using iii = tuple<int, int, int>;

const int MAX{1010};
const vector<ii> dirs{ii(-1, 0), ii(1, 0), ii(0, -1), ii(0, 1)};
const vector<ii> dirs2{ii(-2, 0),  ii(2, 0),  ii(0, -2), ii(0, 2),
                       ii(-2, -2), ii(-2, 2), ii(2, -2), ii(2, 2)};

set<int> adj[MAX * MAX];
int cs[MAX][MAX];

void dfs(int i, int j, int H, int W, int N, const vector<string> &S) {
  if (cs[i][j])
    return;

  cs[i][j] = N;

  for (auto d : dirs) {
    auto u = i + d.first, v = j + d.second;

    if (0 <= u and u < H and 0 <= v and v < W and S[u][v] != '#')
      dfs(u, v, H, W, N, S);
  }
}

int solve(int H, int W, int Cx, int Cy, int Dx, int Dy,
          const vector<string> &S) {
  int N = 0;

  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      if (cs[i][j] or S[i][j] == '#')
        continue;

      ++N;
      dfs(i, j, H, W, N, S);
    }
  }

  LOG(N, endl);

  LOGA2(cs, H, W, endl);

  for (int x = 0; x < H; ++x) {
    for (int y = 0; y < W; ++y) {
      if (cs[x][y] == 0)
        continue;

      for (int i = -2; i <= 2; ++i)
        for (int j = -2; j <= 2; ++j) {
          auto u = x + i, v = y + j;

          if (0 <= u and u < H and 0 <= v and v < W and cs[u][v] > 0 and
              cs[u][v] != cs[x][y]) {
            auto c = cs[x][y];
            auto d = cs[u][v];
            adj[c].insert(d);
            adj[d].insert(c);
          }
        }
    }
  }

  auto C = cs[Cx - 1][Cy - 1], D = cs[Dx - 1][Dy - 1];
  LOG(C, ' ');
  LOG(D, endl);
  queue<ii> q;
  vector<int> visited(N + 1, 0);
  q.push(ii(C, 0));
  visited[C] = true;

  while (not q.empty()) {
    auto c = q.front().first, dist = q.front().second;
    q.pop();

    LOG(c, ' ');
    LOG(dist, endl);

    if (c == D)
      return dist;

    for (auto d : adj[c])
      if (not visited[d]) {
        visited[d] = true;
        q.push(ii(d, dist + 1));
      }
  }

  return -1;
}

int main() {
  ios::sync_with_stdio(false);

  int H, W;
  cin >> H >> W;

  int Cx, Cy, Dx, Dy;
  cin >> Cx >> Cy >> Dx >> Dy;

  vector<string> S(H);

  for (int i = 0; i < H; ++i)
    cin >> S[i];

  auto ans = solve(H, W, Cx, Cy, Dx, Dy, S);

  cout << ans << '\\n';

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#ifdef DEBUG
#define LOG(var, sep) (cerr << #var << " = " << (var) << (sep))

#define LOGM(msg) (cerr << (msg))

#define LOGV(vec, sep)                                                         \\
  {                                                                            \\
    cerr << #vec << " =";                                                      \\
    for (const auto &v : (vec))                                                \\
      cerr << " " << v;                                                        \\
    cerr << (sep);                                                             \\
  }

#define LOGA(arr, N, sep)                                                      \\
  {                                                                            \\
    cerr << #arr << " =";                                                      \\
    for (int i = 0; i < N; ++i)                                                \\
      cerr << " " << (arr)[i];                                                 \\
    cerr << (sep);                                                             \\
  }

#define LOGA2(arr, N, M, sep)                                                  \\
  {                                                                            \\
    cerr << #arr << " =\\n";                                                    \\
    for (int i = 0; i < N; ++i) {                                              \\
      for (int j = 0; j < M; ++j)                                              \\
        cerr << (arr)[i][j] << " ";                                            \\
      cerr << endl;                                                            \\
    }                                                                          \\
    cerr << (sep);                                                             \\
  }
#else
#define LOG(var, sep)
#define LOGM(msg)
#define LOGV(vec, sep)
#define LOGA(arr, N, sep)
#define LOGA2(arr, N, M, sep)
#endif

using namespace std;
using ll = long long;
using ii = pair<int, int>;
using iii = tuple<int, int, int>;

const int MAX{1010};
const vector<ii> dirs{ii(-1, 0), ii(1, 0), ii(0, -1), ii(0, 1)};
const vector<ii> dirs2{ii(-2, 0),  ii(2, 0),  ii(0, -2), ii(0, 2),
                       ii(-2, -2), ii(-2, 2), ii(2, -2), ii(2, 2)};

set<int> adj[MAX * MAX];
int cs[MAX][MAX];

void dfs(int i, int j, int H, int W, int N, const vector<string> &S) {
  if (cs[i][j])
    return;

  cs[i][j] = N;

  for (auto d : dirs) {
    auto u = i + d.first, v = j + d.second;

    if (0 <= u and u < H and 0 <= v and v < W and S[u][v] != '#')
      dfs(u, v, H, W, N, S);
  }
}

int solve(int H, int W, int Cx, int Cy, int Dx, int Dy,
          const vector<string> &S) {
  int N = 0;

  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      if (cs[i][j] or S[i][j] == '#')
        continue;

      ++N;
      dfs(i, j, H, W, N, S);
    }
  }

  LOG(N, endl);

  LOGA2(cs, H, W, endl);

  for (int x = 0; x < H; ++x) {
    for (int y = 0; y < W; ++y) {
      if (cs[x][y] == 0)
        continue;

      for (auto dir : dirs2) {
        auto u = x + dir.first, v = y + dir.second;

        if (0 <= u and u < H and 0 <= v and v < W and cs[u][v] > 0 and
            cs[u][v] != cs[x][y]) {
          auto c = cs[x][y];
          auto d = cs[u][v];
          adj[c].insert(d);
          adj[d].insert(c);
        }
      }
    }
  }

  auto C = cs[Cx - 1][Cy - 1], D = cs[Dx - 1][Dy - 1];
  LOG(C, ' ');
  LOG(D, endl);
  queue<ii> q;
  vector<int> visited(N + 1, 0);
  q.push(ii(C, 0));
  visited[C] = true;

  while (not q.empty()) {
    auto c = q.front().first, dist = q.front().second;
    q.pop();

    LOG(c, ' ');
    LOG(dist, endl);

    if (c == D)
      return dist;

    for (auto d : adj[c])
      if (not visited[d])
        q.push(ii(d, dist + 1));
  }

  return -1;
}

int main() {
  ios::sync_with_stdio(false);

  int H, W;
  cin >> H >> W;

  int Cx, Cy, Dx, Dy;
  cin >> Cx >> Cy >> Dx >> Dy;

  vector<string> S(H);

  for (int i = 0; i < H; ++i)
    cin >> S[i];

  auto ans = solve(H, W, Cx, Cy, Dx, Dy, S);

  cout << ans << '\\n';

  return 0;
}