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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

void bfs(int Sh, int Sw, vector<string> &S, vector<vector<bool>> &visited,
         queue<pair<int, int>> &now_pos) {
  vector<vector<int>> directions = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
  queue<pair<int, int>> q;
  q.push(make_pair(Sh, Sw));
  int h, w, y, x;
  while (!q.empty()) {
    tie(h, w) = q.front();
    q.pop();
    now_pos.push(make_pair(h, w));
    for (auto next : directions) {
      y = h + next[0];
      x = w + next[1];
      if (S[y][x] == '#')
        continue;
      if (visited[y][x])
        continue;
      q.push(make_pair(y, x));
      visited[y][x] = true;
    }
  }
}

void move_b(int Sh, int Sw, vector<string> &S, vector<vector<bool>> &visited,
            queue<pair<int, int>> &next_pos) {
  int h, w;
  for (int i : {-2, -1, 0, 1, 2}) {
    h = Sh + i;
    for (int j : {-2, -1, 0, 1, 2}) {
      w = Sw + j;
      if (S[h][w] == '#')
        continue;
      if (visited[h][w])
        continue;
      next_pos.push(make_pair(h, w));
      visited[h][w] = true;
    }
  }
}

int main() {
  int H, W, Ch, Cw, Dh, Dw;
  cin >> H >> W >> Ch >> Cw >> Dh >> Dw;
  Ch++;
  Cw++;
  Dh++;
  Dw++;
  vector<string> S(H + 4);
  for (int i : {0, 1, H + 2, H + 3}) {
    S[i] = string(W + 4, '#');
  }
  string s;
  for (int i = 2; i < H + 2; i++) {
    cin >> s;
    S[i] = "##" + s + "##";
  }

  int ans = 0;
  vector<vector<bool>> visited(H + 4, vector<bool>(W + 4, false));
  queue<pair<int, int>> now_pos;
  queue<pair<int, int>> next_pos;
  next_pos.push(make_pair(Ch, Cw));
  visited[Ch][Cw] = true;
  int h, w;
  while (true) {
    while (!next_pos.empty()) {
      tie(h, w) = next_pos.front();
      next_pos.pop();
      bfs(h, w, S, visited, now_pos);
    }

    if (visited[Dh][Dw])
      break;

    while (!now_pos.empty()) {
      tie(h, w) = now_pos.front();
      now_pos.pop();
      move_b(h, w, S, visited, next_pos);
    }
    if (next_pos.empty()) {
      ans = -1;
      break;
    }
    ans++;
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

void bfs(int Sh, int Sw, vector<string> &S, vector<vector<bool>> &visited,
         vector<pair<int, int>> &now_pos) {
  vector<vector<int>> directions = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
  queue<pair<int, int>> q;
  q.push(make_pair(Sh, Sw));
  int h, w, y, x;
  while (!q.empty()) {
    tie(h, w) = q.front();
    q.pop();
    if (!visited[h][w])
      now_pos.push_back(make_pair(h, w));
    visited[h][w] = true;
    for (auto next : directions) {
      y = h + next[0];
      x = w + next[1];
      if (S[y][x] == '#')
        continue;
      if (visited[y][x])
        continue;
      q.push(make_pair(y, x));
    }
  }
}

void move_b(int Sh, int Sw, vector<string> &S, vector<vector<bool>> &visited,
            vector<pair<int, int>> &next_pos) {
  int h, w;
  for (int i : {-2, -1, 0, 1, 2}) {
    h = Sh + i;
    for (int j : {-2, -1, 0, 1, 2}) {
      w = Sw + j;
      if (S[h][w] == '#')
        continue;
      if (visited[h][w])
        continue;
      visited[h][w] = true;
      next_pos.push_back(make_pair(h, w));
    }
  }
}

int main() {
  int H, W, Ch, Cw, Dh, Dw;
  cin >> H >> W >> Ch >> Cw >> Dh >> Dw;
  Ch++;
  Cw++;
  Dh++;
  Dw++;
  vector<string> S(H + 4);
  for (int i : {0, 1, H + 2, H + 3}) {
    S[i] = string(W + 4, '#');
  }
  string s;
  for (int i = 2; i < H + 2; i++) {
    cin >> s;
    S[i] = "##" + s + "##";
  }

  int ans = 0;
  vector<vector<bool>> visited(H + 4, vector<bool>(W + 4, false));
  vector<pair<int, int>> next_pos;
  next_pos.push_back(make_pair(Ch, Cw));
  int h, w;
  while (true) {
    vector<pair<int, int>> now_pos;
    for (auto pos : next_pos) {
      tie(h, w) = pos;
      bfs(h, w, S, visited, now_pos);
      now_pos.push_back(make_pair(h, w));
    }

    if (visited[Dh][Dw])
      break;

    next_pos.clear();
    for (auto pos : now_pos) {
      tie(h, w) = pos;
      move_b(h, w, S, visited, next_pos);
    }
    if (next_pos.empty()) {
      ans = -1;
      break;
    }
    ans++;
  }
  cout << ans << endl;
  return 0;
}