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

#include <algorithm>
#include <deque>
#include <iostream>
#include <set>
#include <string>
#include <vector>
using namespace std;
int S[1000][1000];

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;
  deque<pair<int, int>> stack3;

  pair<int, int> a(C[0], C[1]);

  int result = 0;
  while (true) {
    while (true) {
      int x = a.first;
      int y = a.second;
      if (x >= 0 && x < W && y >= 0 && y < H && S[x][y] == 1) {
        stack2.push_back(a);
        S[x][y] = 2;
        stack3.push_back({x - 1, y});
        stack3.push_back({x + 1, y});
        stack3.push_back({x, y - 1});
        stack3.push_back({x, y + 1});
      }
      if (stack3.size() == 0) {
        break;
      }
      a = stack3.back();
      stack3.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 = x - 2; xx < x + 3; xx++) {
        for (int yy = y - 2; yy < y + 3; yy++) {
          if (xx >= 0 && xx < W && yy >= 0 && yy < H && S[xx][yy] == 1) {
            stack3.push_back({xx, yy});
            //						S[x + xx][y + yy] = 4;
            frag = true;
          }
        }
      }
    }
    stack2.clear();

    if (!frag) {
      cout << -1;
      return 0;
    }
    a = stack3.back();
    stack3.pop_back();
    result++;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int S[1000][1000];

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;
  deque<pair<int, int>> stack3;

  pair<int, int> a(C[0], C[1]);

  int result = 0;
  while (true) {
    while (true) {
      int x = a.first;
      int y = a.second;
      if (x >= 0 && x < W && y >= 0 && y < H && S[x][y] == 1) {
        stack2.push_back(a);
        S[x][y] = 2;
        stack3.push_back({x - 1, y});
        stack3.push_back({x + 1, y});
        stack3.push_back({x, y - 1});
        stack3.push_back({x, y + 1});
      }
      if (stack3.size() == 0) {
        break;
      }
      a = stack3.back();
      stack3.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) {
            stack3.push_back({xx + x, yy + y});
            //						S[x + xx][y + yy] = 4;
            frag = true;
          }
        }
      }
    }

    if (!frag) {
      cout << -1;
      return 0;
    }
    a = stack3.back();
    stack3.pop_back();
    result++;
  }
}