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

#include <algorithm>
#include <deque>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main() {
  int H, W;
  cin >> H >> W;
  int C[2];
  int D[2];
  cin >> C[1] >> C[0] >> D[1] >> D[0];
  C[0] = C[0] - 1;
  C[1] = C[1] - 1;
  D[0] = D[0] - 1;
  D[1] = D[1] - 1;
  vector<vector<int>> S(W, vector<int>(H));
  for (int y = 0; y < H; y++) {
    for (int x = 0; x < W; x++) {
      char c;
      cin >> c;
      if (c == '.') {
        S[x][y] = 1;
      } else {
        S[x][y] = 0;
      }
    }
  }
  deque<pair<int, int>> stack;
  deque<pair<int, int>> stack2;

  pair<int, int> a(C[0], C[1]);
  S[a.first][a.second] = 4;

  int result = 0;
  while (true) {
    while (true) {
      stack2.push_back(a);
      if (a.second + 1 < H && S[a.first][a.second + 1] == 1) {
        stack.push_back(a);
        S[a.first][a.second + 1] = 4;
        a.second++;
        continue;
      }
      if (a.second - 1 >= 0 && S[a.first][a.second - 1] == 1) {
        stack.push_back(a);
        S[a.first][a.second - 1] = 4;
        a.second--;
        continue;
      }
      if (a.first + 1 < W && S[a.first + 1][a.second] == 1) {
        stack.push_back(a);
        S[a.first + 1][a.second] = 4;
        a.first++;
        continue;
      }
      if (a.first - 1 >= 0 && S[a.first - 1][a.second] == 1) {
        stack.push_back(a);
        S[a.first - 1][a.second] = 4;
        a.first--;
        continue;
      }

      if (stack.size() == 0) {
        break;
      }
      a = stack.back();
      stack.pop_back();
    }
    if (S[D[0]][D[1]] == 2 || S[D[0]][D[1]] == 4) {
      cout << result;
      return 0;
    }
    bool frag = false;
    for (int i = 0; i < stack2.size(); i++) {
      int x = stack2[i].first;
      int y = stack2[i].second;
      for (int xx = -2; xx < 3; xx++) {
        for (int yy = -2; yy < 3; yy++) {
          if (xx + x >= 0 && xx + x < W && yy + y >= 0 && yy + y < H &&
              S[x + xx][y + yy] == 1) {
            S[x + xx][y + yy] = 4;
            stack.push_back({x + xx, y + yy});
            frag = true;
          }
        }
      }
    }
    stack2.clear();
    if (!frag) {
      cout << -1;
      return 0;
    }
    a = stack[stack.size() - 1];
    stack.pop_back();
    result++;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <deque>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main() {
  int H, W;
  cin >> H >> W;
  int C[2];
  int D[2];
  cin >> C[1] >> C[0] >> D[1] >> D[0];
  C[0] = C[0] - 1;
  C[1] = C[1] - 1;
  D[0] = D[0] - 1;
  D[1] = D[1] - 1;
  vector<vector<int>> S(W, vector<int>(H));
  for (int y = 0; y < H; y++) {
    for (int x = 0; x < W; x++) {
      char c;
      cin >> c;
      if (c == '.') {
        S[x][y] = 1;
      } else {
        S[x][y] = 0;
      }
    }
  }
  deque<pair<int, int>> stack;
  deque<pair<int, int>> stack2;

  pair<int, int> a(C[0], C[1]);
  S[a.first][a.second] = 4;

  int result = 0;
  while (true) {
    while (true) {
      stack2.push_back(a);
      if (a.second + 1 < H && S[a.first][a.second + 1] == 1) {
        stack.push_back(a);
        S[a.first][a.second + 1] = 4;
        a.second++;
        continue;
      }
      if (a.second - 1 >= 0 && S[a.first][a.second - 1] == 1) {
        stack.push_back(a);
        S[a.first][a.second - 1] = 4;
        a.second--;
        continue;
      }
      if (a.first + 1 < W && S[a.first + 1][a.second] == 1) {
        stack.push_back(a);
        S[a.first + 1][a.second] = 4;
        a.first++;
        continue;
      }
      if (a.first - 1 >= 0 && S[a.first - 1][a.second] == 1) {
        stack.push_back(a);
        S[a.first - 1][a.second] = 4;
        a.first--;
        continue;
      }

      if (stack.size() == 0) {
        break;
      }
      a = stack.back();
      stack.pop_back();
    }
    if (S[D[0]][D[1]] == 2 || S[D[0]][D[1]] == 4) {
      cout << result;
      return 0;
    }
    bool frag = false;
    for (int i = 0; i < stack2.size(); i++) {
      int x = stack2[i].first;
      int y = stack2[i].second;
      for (int xx = -2; xx < 3; xx++) {
        for (int yy = -2; yy < 3; yy++) {
          if (xx + x >= 0 && xx + x < W && yy + y >= 0 && yy + y < H &&
              S[x + xx][y + yy] == 1) {
            S[x + xx][y + yy] = 3;
            frag = true;
          }
        }
      }
    }
    stack2.clear();
    for (int y = 0; y < H; y++) {
      for (int x = 0; x < W; x++) {
        if (S[x][y] == 4) {
          S[x][y] = 2;
        }
        if (S[x][y] == 3) {
          stack.push_back({x, y});
          S[x][y] = 4;
        }
      }
    }

    if (!frag) {
      cout << -1;
      return 0;
    }
    a = stack[stack.size() - 1];
    stack.pop_back();
    result++;
  }
}