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

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

int main() {
  int H;
  int W;
  int h, w;
  int nh, nw;
  pair<int, int> S;
  pair<int, int> G;

  cin >> H >> W;
  cin >> S.first >> S.second;
  cin >> G.first >> G.second;
  S.first -= 1;
  S.second -= 1;
  G.first -= 1;
  G.second -= 1;

  vector<string> c(H);
  for (int i = 0; i < H; i++) {
    cin >> c[i];
  }
  vector<vector<int>> dist(H, vector<int>(W));
  for (int i = 0; i < H; i++) {
    for (int g = 0; g < W; g++) {
      if (c[i][g] == '#') {
        dist[i][g] = -2;
      } else {
        dist[i][g] = -1;
      }
    }
  }

  set<pair<int, int>> checkList;
  queue<pair<int, int>> que;
  que.push(S);

  vector<vector<int>> directions{{0, 1}, {1, 0}, {0, -1}, {-1, 0}};

  int cost = 0;

  while (true) {
    if (que.empty()) {
      cost += 1;
      for (auto x : checkList) {
        for (int i = -2; i < 3; i++) {
          for (int g = -2; g < 3; g++) {
            nh = x.first + i;
            nw = x.second + g;
            if (nw < 0 || nw > W - 1 || nh < 0 || nh > H - 1) {
              continue;
            } else {
              if (dist[nh][nw] == -1) {
                que.push({nh, nw});
              }
            }
          }
        }
      }
      checkList.clear();
      if (que.empty()) {
        cout << -1 << endl;
        return 0;
      }
    } else {
      h = que.front().first;
      w = que.front().second;
      dist[h][w] = cost;
      que.pop();
      if (h == G.first && w == G.second) {
        cout << cost << endl;
        return 0;
      }
      for (int i = 0; i < 4; i++) {
        nh = h + directions[i][0];
        nw = w + directions[i][1];
        if (nw < 0 || nw > W - 1 || nh < 0 || nh > H - 1) {
          continue;
        }
        if (dist[nh][nw] == -1) {
          que.push({nh, nw});
          dist[nh][nw] = cost;
        } else if (dist[nh][nw] == -2) {
          checkList.insert({h, w});
        }
      }
    }
  }
}


Error: TLE

Fixed submission:

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

int main() {
  int H;
  int W;
  int h, w;
  int nh, nw;
  pair<int, int> S;
  pair<int, int> G;

  cin >> H >> W;
  cin >> S.first >> S.second;
  cin >> G.first >> G.second;
  S.first -= 1;
  S.second -= 1;
  G.first -= 1;
  G.second -= 1;

  vector<string> c(H);
  for (int i = 0; i < H; i++) {
    cin >> c[i];
  }
  vector<vector<int>> dist(H, vector<int>(W));
  for (int i = 0; i < H; i++) {
    for (int g = 0; g < W; g++) {
      if (c[i][g] == '#') {
        dist[i][g] = -2;
      } else {
        dist[i][g] = -1;
      }
    }
  }

  set<pair<int, int>> checkList;
  queue<pair<int, int>> que;
  que.push(S);
  dist[S.first][S.second] = 0;

  vector<vector<int>> directions{{0, 1}, {1, 0}, {0, -1}, {-1, 0}};

  int cost = 0;

  while (true) {
    if (que.empty()) {
      cost += 1;
      for (auto x : checkList) {
        for (int i = -2; i < 3; i++) {
          for (int g = -2; g < 3; g++) {
            nh = x.first + i;
            nw = x.second + g;
            if (nw < 0 || nw > W - 1 || nh < 0 || nh > H - 1) {
              continue;
            } else {
              if (dist[nh][nw] == -1) {
                que.push({nh, nw});
              }
            }
          }
        }
      }
      checkList.clear();
      if (que.empty()) {
        cout << -1 << endl;
        return 0;
      }
    } else {
      h = que.front().first;
      w = que.front().second;
      que.pop();
      if (h == G.first && w == G.second) {
        cout << cost << endl;
        return 0;
      }
      for (int i = 0; i < 4; i++) {
        nh = h + directions[i][0];
        nw = w + directions[i][1];
        if (nw < 0 || nw > W - 1 || nh < 0 || nh > H - 1) {
          continue;
        }
        if (dist[nh][nw] == -1) {
          que.push({nh, nw});
          dist[nh][nw] = cost;
        } else if (dist[nh][nw] == -2) {
          checkList.insert({h, w});
        }
      }
    }
  }
}