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

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

int main() {
  long long int H, W, Ch, Cw, Dh, Dw, i, j, k, l;
  cin >> H >> W >> Ch >> Cw >> Dh >> Dw;
  vector<vector<long long int>> board(
      H, vector<long long int>(W)); /* -2が壁、10000000が未開の道 */
  char c;
  for (i = 0; i < H; i++) {
    for (j = 0; j < W; j++) {
      cin >> c;
      if (c == '#') {
        board[i][j] = -2;
      } else {
        board[i][j] = 10000000;
      }
    }
  }
  set<pair<long long int, long long int>> group[2];
  group[0].insert(make_pair(Ch - 1, Cw - 1));
  long long int now = 0;
  long long int next = 1;
  long long int dist = 0;
  while (!group[now].empty()) {
    set<pair<long long int, long long int>>::iterator it = group[now].begin();
    while (!group[now].empty()) {
      i = (*it).first;
      j = (*it).second;
      board[i][j] = dist;
      if (i - 1 > -1 && board[i - 1][j] != -2 && board[i - 1][j] > dist) {
        board[i - 1][j] = dist;
        group[now].insert(make_pair(i - 1, j));
        group[next].erase(make_pair(i - 1, j));
      }
      if (j - 1 > -1 && board[i][j - 1] != -2 && board[i][j - 1] > dist) {
        board[i][j - 1] = dist;
        group[now].insert(make_pair(i, j - 1));
        group[next].erase(make_pair(i, j - 1));
      }
      if (i + 1 < H && board[i + 1][j] != -2 && board[i + 1][j] > dist) {
        board[i + 1][j] = dist;
        group[now].insert(make_pair(i + 1, j));
        group[next].erase(make_pair(i + 1, j));
      }
      if (j + 1 < W && board[i][j + 1] != -2 && board[i][j + 1] > dist) {
        board[i][j + 1] = dist;
        group[now].insert(make_pair(i, j + 1));
        group[next].erase(make_pair(i, j + 1));
      }
      for (k = i - 2; k < i + 3; k++) {
        for (l = j - 2; l < j + 3; l++) {
          if (-1 < k && k < H && -1 < l && l < W && board[k][l] != -2 &&
              board[k][l] > dist + 1) {
            group[next].insert(make_pair(k, l));
            board[k][l] = dist + 1;
          }
        }
      }
      group[now].erase(it);
      it = group[now].begin();
    }
    if (now == 0) {
      now = 1;
      next = 0;
    } else {
      now = 0;
      next = 1;
    }
    dist += 1;
  }
  if (board[Dh - 1][Dw - 1] == 10000000) {
    cout << -1 << endl;
  } else {
    cout << board[Dh - 1][Dw - 1] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  long int H, W, Ch, Cw, Dh, Dw, i, j, k, l;
  cin >> H >> W >> Ch >> Cw >> Dh >> Dw;
  char s;
  vector<vector<long int>> group(
      H, vector<long int>(W)); /* -2は#で-1は未開拓の. */
  for (i = 0; i < H; i++) {
    for (j = 0; j < W; j++) {
      cin >> s;
      if (s == '#') {
        group[i][j] = -2;
      } else {
        group[i][j] = -1;
      }
    }
  }
  queue<pair<long int, long int>> q;
  long int color = 0;
  for (i = 0; i < H; i++) {
    for (j = 0; j < W; j++) {
      if (group[i][j] == -1) {
        q.push(make_pair(i, j));
        while (!q.empty()) {
          k = q.front().first;
          l = q.front().second;
          group[k][l] = color;
          q.pop();
          if (k - 1 > -1 && group[k - 1][l] == -1) {
            q.push(make_pair(k - 1, l));
          }
          if (l - 1 > -1 && group[k][l - 1] == -1) {
            q.push(make_pair(k, l - 1));
          }
          if (k + 1 < H && group[k + 1][l] == -1) {
            q.push(make_pair(k + 1, l));
          }
          if (l + 1 < W && group[k][l + 1] == -1) {
            q.push(make_pair(k, l + 1));
          }
        }
        color += 1;
      }
    }
  }
  vector<set<long int>> dist(color, set<long int>());
  for (i = 0; i < H; i++) {
    for (j = 0; j < W; j++) {
      for (k = i - 2; k < i + 3; k++) {
        for (l = j - 2; l < j + 3; l++) {
          if (-1 < k && k < H && -1 < l && l < W && group[k][l] != -2 &&
              group[i][j] != -2 && group[k][l] != group[i][j]) {
            dist[group[i][j]].insert(group[k][l]);
            dist[group[k][l]].insert(group[i][j]);
          }
        }
      }
    }
  }

  vector<long int> from_C(color);
  for (i = 0; i < color; i++) {
    from_C[i] = 10000000;
  }
  from_C[group[Ch - 1][Cw - 1]] = 0;
  queue<long int> next;
  next.push(group[Ch - 1][Cw - 1]);
  long int now_color;
  while (!next.empty()) {
    now_color = next.front();
    next.pop();
    set<long int>::iterator it = dist[now_color].begin();
    while (it != dist[now_color].end()) {
      if (from_C[now_color] + 1 < from_C[*it]) {
        from_C[*it] = from_C[now_color] + 1;
        next.push(*it);
        if (group[Dh - 1][Dw - 1] == *it) {
          goto OUT;
        }
      }
      it++;
    }
  }
OUT:
  if (from_C[group[Dh - 1][Dw - 1]] == 10000000) {
    cout << -1 << endl;
  } else {
    cout << from_C[group[Dh - 1][Dw - 1]] << endl;
  }
  return 0;
}