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

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

#define WHITE 0
#define BLACK 1

ll H, W, Ch, Cw, Dh, Dw;
vector<string> S;
vector<vector<ll>> col;

struct BFSDroplet {
  BFSDroplet(ll first, ll second, ll d) : first(first), second(second), d(d) {}
  ll first, second, d;
};

ll solve(ll Ch, ll Cw) {
  queue<BFSDroplet> QMotherList;
  QMotherList.push(BFSDroplet(Ch, Cw, 0));
  while (!QMotherList.empty()) {
    auto v = QMotherList.front();
    QMotherList.pop();
    if (col[v.first][v.second] == BLACK)
      continue;
    queue<BFSDroplet> QOngoingList;
    queue<BFSDroplet> QAfterList;
    QOngoingList.push(v);
    while (!QOngoingList.empty()) {
      auto u = QOngoingList.front();
      QOngoingList.pop();
      col[u.first][u.second] = BLACK;
      QAfterList.push(u);
      if (u.first + 1 < H) {
        if (col[u.first + 1][u.second] == WHITE &&
            S[u.first + 1][u.second] == '.') {
          col[u.first + 1][u.second] = BLACK;
          QOngoingList.push(BFSDroplet(u.first + 1, u.second, u.d));
        }
      }
      if (u.second + 1 < W) {
        if (col[u.first][u.second + 1] == WHITE &&
            S[u.first][u.second + 1] == '.') {
          col[u.first][u.second + 1] = BLACK;
          QOngoingList.push(BFSDroplet(u.first, u.second + 1, u.d));
        }
      }
      if (u.first - 1 >= 0) {
        if (col[u.first - 1][u.second] == WHITE &&
            S[u.first - 1][u.second] == '.') {
          col[u.first - 1][u.second] = BLACK;
          QOngoingList.push(BFSDroplet(u.first - 1, u.second, u.d));
        }
      }
      if (u.second - 1 >= 0) {
        if (col[u.first][u.second - 1] == WHITE &&
            S[u.first][u.second - 1] == '.') {
          col[u.first][u.second - 1] = BLACK;
          QOngoingList.push(BFSDroplet(u.first, u.second - 1, u.d));
        }
      }
    }
    while (!QAfterList.empty()) {
      auto u = QAfterList.front();
      QAfterList.pop();
      if (u.first == Dh && u.second == Dw) {
        return u.d;
      }
      for (ll h = u.first - 2; h <= u.first + 2; h++) {
        if (h < 0 || h >= H)
          continue;
        for (ll w = u.second - 2; w <= u.second + 2; w++) {
          if (w < 0 || w >= W)
            continue;
          if (col[h][w] == WHITE && S[h][w] == '.') {
            QMotherList.push(BFSDroplet(h, w, u.d + 1));
          }
        }
      }
    }
  }
  return -1;
}

int main() {
  cin >> H >> W >> Ch >> Cw >> Dh >> Dw;
  Ch--;
  Cw--;
  Dh--;
  Dw--;
  S.resize(H);
  col.resize(H);
  for (ll h = 0; h < H; h++) {
    cin >> S[h];
    col[h].resize(W);
    for (ll w = 0; w < W; w++) {
      col[h][w] = WHITE;
    }
  }
  cout << solve(Ch, Cw) << endl;
  return 0;
}


Error: TLE

Fixed submission:

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

#define WHITE 0
#define BLACK 1

ll H, W, Ch, Cw, Dh, Dw;
vector<string> S;
vector<vector<ll>> col;

struct BFSDroplet {
  BFSDroplet(ll first, ll second, ll d) : first(first), second(second), d(d) {}
  ll first, second, d;
};

ll solve(ll Ch, ll Cw) {
  queue<BFSDroplet> QMotherList;
  QMotherList.push(BFSDroplet(Ch, Cw, 0));
  while (!QMotherList.empty()) {
    auto v = QMotherList.front();
    QMotherList.pop();
    if (col[v.first][v.second] == BLACK)
      continue;
    queue<BFSDroplet> QOngoingList;
    queue<BFSDroplet> QAfterList;
    QOngoingList.push(v);
    while (!QOngoingList.empty()) {
      auto u = QOngoingList.front();
      QOngoingList.pop();
      col[u.first][u.second] = BLACK;
      QAfterList.push(u);
      if (u.first + 1 < H) {
        if (col[u.first + 1][u.second] == WHITE &&
            S[u.first + 1][u.second] == '.') {
          QOngoingList.push(BFSDroplet(u.first + 1, u.second, u.d));
        }
      }
      if (u.second + 1 < W) {
        if (col[u.first][u.second + 1] == WHITE &&
            S[u.first][u.second + 1] == '.') {
          QOngoingList.push(BFSDroplet(u.first, u.second + 1, u.d));
        }
      }
      if (u.first - 1 >= 0) {
        if (col[u.first - 1][u.second] == WHITE &&
            S[u.first - 1][u.second] == '.') {
          QOngoingList.push(BFSDroplet(u.first - 1, u.second, u.d));
        }
      }
      if (u.second - 1 >= 0) {
        if (col[u.first][u.second - 1] == WHITE &&
            S[u.first][u.second - 1] == '.') {
          QOngoingList.push(BFSDroplet(u.first, u.second - 1, u.d));
        }
      }
    }
    while (!QAfterList.empty()) {
      auto u = QAfterList.front();
      QAfterList.pop();
      if (u.first == Dh && u.second == Dw) {
        return u.d;
      }
      for (ll h = u.first - 2; h <= u.first + 2; h++) {
        if (h < 0 || h >= H)
          continue;
        for (ll w = u.second - 2; w <= u.second + 2; w++) {
          if (w < 0 || w >= W)
            continue;
          if (col[h][w] == WHITE && S[h][w] == '.') {
            QMotherList.push(BFSDroplet(h, w, u.d + 1));
          }
        }
      }
    }
  }
  return -1;
}

int main() {
  cin >> H >> W >> Ch >> Cw >> Dh >> Dw;
  Ch--;
  Cw--;
  Dh--;
  Dw--;
  S.resize(H);
  col.resize(H);
  for (ll h = 0; h < H; h++) {
    cin >> S[h];
    col[h].resize(W);
    for (ll w = 0; w < W; w++) {
      col[h][w] = WHITE;
    }
  }
  cout << solve(Ch, Cw) << endl;
  return 0;
}