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

#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <vector>

using ll = long long;
const int INF = 1000000000;
int dy[4] = {-1, 0, 1, 0};
int dx[4] = {0, 1, 0, -1};
int h, w;
int color = 0;

void dfs(int i, int j, std::vector<std::vector<char>> &m,
         std::vector<std::vector<int>> &group, int c) {
  group[i][j] = c;
  for (int k = 0; k < 4; k++) {
    int ii = i + dy[k], jj = j + dx[k];
    if (ii < 0 || jj < 0 || ii >= h || jj >= w)
      continue;
    if (m[ii][jj] == '#')
      continue;
    if (group[ii][jj] == -1) {
      dfs(ii, jj, m, group, c);
    }
  }
}

int bfs(int u, int t, std::vector<std::set<int>> &map) {
  std::queue<int> que;
  std::vector<int> distance(color, INF);

  int now = u;
  distance[now] = 0;
  que.push(now);

  while (!que.empty()) {
    now = que.front();
    que.pop();

    for (auto next : map[now]) {
      if (distance[next] > distance[now] + 1) {
        distance[next] = distance[now] + 1;
        que.push(next);
      }
    }
  }

  if (distance[t] == INF)
    return -1;
  return distance[t];
}

int main() {
  std::cin >> h >> w;
  int ch, cw, dh, dw;
  std::cin >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  std::vector<std::vector<char>> m(h, std::vector<char>(w));
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      std::cin >> m[i][j];
    }
  }

  std::vector<std::vector<int>> group(h, std::vector<int>(w, -1));
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (m[i][j] == '#')
        continue;
      if (group[i][j] == -1) {
        dfs(i, j, m, group, color);
        color++;
      }
    }
  }

  // どの群からどの群へ移れるか？
  std::vector<std::set<int>> map(color);
  std::vector<std::set<std::pair<int, int>>> belong(color);
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (group[i][j] != -1) {
        belong[group[i][j]].emplace(i, j);
      }
    }
  }

  /*
  for(int i = 0; i < color; i++){
      for(int j = i + 1; j < color; j++){
          for(auto s1: belong[i]){
              for(auto s2: belong[j]){
                  int diff_x = std::abs(s1.first - s2.first);
                  int diff_y = std::abs(s1.second - s2.second);
                  if(diff_x <= 2 && diff_y <= 2){
                      map[i][j] = true;
                      map[j][i] = true;
                  }
              }
          }
          for(auto s1: belong[j]){
              for(auto s2: belong[i]){
                  int diff_x = std::abs(s1.first - s2.first);
                  int diff_y = std::abs(s1.second - s2.second);
                  if(diff_x <= 2 && diff_y <= 2){
                      map[i][j] = true;
                      map[j][i] = true;
                  }
              }
          }
      }
  }
  */

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (m[i][j] == '#')
        continue;
      for (int x = -2; x <= 2; x++) {
        for (int y = -2; y <= 2; y++) {
          int ii = i + x, jj = j + y;
          if (ii < 0 || jj < 0 || ii >= h || jj >= w)
            continue;
          if (m[ii][jj] == '#')
            continue;
          if (group[i][j] != group[ii][jj]) {
            map[group[i][j]].insert(group[ii][jj]);
            map[group[ii][jj]].insert(group[i][j]);
          }
        }
      }
    }
  }

  int startColor = group[ch][cw], toColor = group[dh][dw];
  std::cout << bfs(startColor, toColor, map) << std::endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <vector>

using ll = long long;
const int INF = 1000000000;
int dy[4] = {-1, 0, 1, 0};
int dx[4] = {0, 1, 0, -1};
int h, w;
int color = 0;

void dfs(int i, int j, std::vector<std::vector<char>> &m,
         std::vector<std::vector<int>> &group, int c) {
  group[i][j] = c;
  for (int k = 0; k < 4; k++) {
    int ii = i + dy[k], jj = j + dx[k];
    if (ii < 0 || jj < 0 || ii >= h || jj >= w)
      continue;
    if (m[ii][jj] == '#')
      continue;
    if (group[ii][jj] == -1) {
      dfs(ii, jj, m, group, c);
    }
  }
}

int bfs(int u, int t, std::vector<std::vector<bool>> &map) {
  std::queue<int> que;
  std::vector<int> distance(color, INF);

  int now = u;
  distance[now] = 0;
  que.push(now);

  while (!que.empty()) {
    now = que.front();
    que.pop();

    for (int i = 0; i < color; i++) {
      if (map[now][i]) {
        if (distance[i] > distance[now] + 1) {
          distance[i] = distance[now] + 1;
          que.push(i);
        }
      }
    }
  }

  if (distance[t] == INF)
    return -1;
  return distance[t];
}

int main() {
  std::cin >> h >> w;
  int ch, cw, dh, dw;
  std::cin >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  std::vector<std::vector<char>> m(h, std::vector<char>(w));
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      std::cin >> m[i][j];
    }
  }

  std::vector<std::vector<int>> group(h, std::vector<int>(w, -1));
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (m[i][j] == '#')
        continue;
      if (group[i][j] == -1) {
        dfs(i, j, m, group, color);
        color++;
      }
    }
  }

  /*
  for(int i = 0; i < h; i++){
      for(int j = 0; j < w; j++){
          printf("%2d ", group[i][j]);
      }
      std::cout << "\\n";
  }
  */

  // どの群からどの群へ移れるか？
  std::vector<std::vector<bool>> map(color, std::vector<bool>(color, false));
  std::vector<std::set<std::pair<int, int>>> belong(color);
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (group[i][j] != -1) {
        belong[group[i][j]].emplace(i, j);
      }
    }
  }

  for (int i = 0; i < color; i++) {
    for (int j = i + 1; j < color; j++) {
      for (auto s1 : belong[i]) {
        for (auto s2 : belong[j]) {
          int diff_x = std::abs(s1.first - s2.first);
          int diff_y = std::abs(s1.second - s2.second);
          if (diff_x <= 2 && diff_y <= 2) {
            map[i][j] = true;
            map[j][i] = true;
          }
        }
      }
      for (auto s1 : belong[j]) {
        for (auto s2 : belong[i]) {
          int diff_x = std::abs(s1.first - s2.first);
          int diff_y = std::abs(s1.second - s2.second);
          if (diff_x <= 2 && diff_y <= 2) {
            map[i][j] = true;
            map[j][i] = true;
          }
        }
      }
    }
  }

  /*
  for(int i = 0; i < color; i++){
      for(auto s : map[i]){
          std::cout << s << " ";
      }
      std::cout << "\\n";
  }
  */

  int startColor = group[ch][cw], toColor = group[dh][dw];
  std::cout << bfs(startColor, toColor, map) << std::endl;
  return 0;
}