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

//{{{
#include <algorithm>
#include <cmath>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <sys/time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using ll = long long;
enum : int { M = (int)1e9 + 7 };
enum : ll { MLL = (ll)1e18L + 9 };
using namespace std;
#ifdef LOCAL
#include "rprint2.hpp"
#else
#define FUNC(name)                                                             \\
  template <ostream &out = cout, class... T> void name(T &&...) {}
FUNC(printde)
FUNC(printdbe) FUNC(printdwe) FUNC(printdu) FUNC(printe) FUNC(printe0);
#endif
template <template <class T, class = std::allocator<T>> class V, class E>
istream &operator>>(istream &in, V<E> &v) {
  for (auto &e : v) {
    in >> e;
  }
  return in;
}
//}}}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int h, w;
  cin >> h >> w;
  int ch, cw;
  cin >> ch >> cw;
  ch--;
  cw--;
  int dh, dw;
  cin >> dh >> dw;
  dh--;
  dw--;
  vector<string> ss(h);
  cin >> ss;
  vector<pair<int, int>> stack;
  vector<pair<int, int>> nexts;
  stack.emplace_back(ch, cw);
  ss[ch][cw] = 'o';
  int ans = 0;
  while (ss[dh][dw] == '.') {
    auto add = [&](int y, int x) {
      if (y < 0 || h <= y || x < 0 || w <= x) {
        return;
      }
      if (ss[y][x] != '.') {
        return;
      }
      ss[y][x] = 'o';
      stack.emplace_back(y, x);
    };
    if (stack.empty()) {
      ans++;
      for (auto &p : nexts) {
        for (int i = -2; i <= 2; i++) {
          for (int j = -2; j <= 2; j++) {
            add(p.first + i, p.second + j);
          }
        }
      }
      nexts.clear();
      if (stack.empty()) {
        break;
      }
    }
    auto p = stack.back();
    nexts.push_back(p);
    stack.pop_back();
    add(p.first + 1, p.second);
    add(p.first, p.second + 1);
    add(p.first - 1, p.second);
    add(p.first, p.second - 1);
  }
  cout << (ss[dh][dw] == '.' ? -1 : ans) << '\\n';
}


Error: TLE

Fixed submission:

//{{{
#include <algorithm>
#include <cmath>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <sys/time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using ll = long long;
enum : int { M = (int)1e9 + 7 };
enum : ll { MLL = (ll)1e18L + 9 };
using namespace std;
#ifdef LOCAL
#include "rprint2.hpp"
#else
#define FUNC(name)                                                             \\
  template <ostream &out = cout, class... T> void name(T &&...) {}
FUNC(printde)
FUNC(printdbe) FUNC(printdwe) FUNC(printdu) FUNC(printe) FUNC(printe0);
#endif
template <template <class T, class = std::allocator<T>> class V, class E>
istream &operator>>(istream &in, V<E> &v) {
  for (auto &e : v) {
    in >> e;
  }
  return in;
}
//}}}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int h, w;
  cin >> h >> w;
  int ch, cw;
  cin >> ch >> cw;
  ch--;
  cw--;
  int dh, dw;
  cin >> dh >> dw;
  dh--;
  dw--;
  vector<string> ss(h);
  cin >> ss;
  vector<pair<int, int>> stack;
  vector<pair<int, int>> nexts;
  stack.emplace_back(ch, cw);
  ss[ch][cw] = 'o';
  int ans = 0;
  while (ss[dh][dw] == '.') {
    auto add = [&](int y, int x) {
      if (y < 0 || h <= y || x < 0 || w <= x) {
        return;
      }
      if (ss[y][x] != '.') {
        return;
      }
      ss[y][x] = 'o';
      stack.emplace_back(y, x);
    };
    if (stack.empty()) {
      ans++;
      for (auto &p : nexts) {
        for (int i = -2; i <= 2; i++) {
          for (int j = -2; j <= 2; j++) {
            add(p.first + i, p.second + j);
          }
        }
      }
      if (stack.empty()) {
        break;
      }
    }
    auto p = stack.back();
    nexts.push_back(p);
    stack.pop_back();
    add(p.first + 1, p.second);
    add(p.first, p.second + 1);
    add(p.first - 1, p.second);
    add(p.first, p.second - 1);
  }
  cout << (ss[dh][dw] == '.' ? -1 : ans) << '\\n';
}