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

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

const int x[4] = {0, 0, 1, -1};
const int y[4] = {1, -1, 0, 0};
int main() {
  int H, W;
  cin >> H >> W;
  int Ch, Cw, Dh, Dw;
  cin >> Ch >> Cw >> Dh >> Dw;
  string S[H];
  for (int i = 0; i < H; i++)
    cin >> S[i];
  vector<vector<int>> C(H, vector<int>(W));
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++)
      C[i][j] = -1;
  }
  C[Ch - 1][Cw - 1] = 0;
  queue<pair<int, int>> q;
  q.push({Ch - 1, Cw - 1});
  int count = 0;
  while (!q.empty()) {
    queue<pair<int, int>> pq;
    while (!q.empty()) {
      pair<int, int> p = q.front();
      q.pop();
      pq.push(p);
      queue<pair<int, int>> qq;
      qq.push(p);
      while (!qq.empty()) {
        pair<int, int> pp = qq.front();
        qq.pop();
        for (int i = 0; i < 4; i++) {
          int xx = pp.first + x[i], yy = pp.second + y[i];
          if (xx < 0 || yy < 0 || xx >= H || yy >= W)
            continue;
          if (C[xx][yy] != -1)
            continue;
          if (S[xx][yy] == '#')
            continue;
          C[xx][yy] = C[p.first][p.second];
          qq.push({xx, yy});
          pq.push({xx, yy});
        }
      }
    }
    /*for (int i=0; i<H; i++) {
        for (int j=0; j<W; j++) {
            if (C[i][j]!=count) continue;*/
    while (!pq.empty()) {
      int i = pq.front().first;
      int j = pq.front().second;
      pq.pop();
      for (int k = -2; k < 3; k++) {
        for (int l = -2; l < 3; l++) {
          if (i + k < 0 || j + l < 0 || i + k >= H || j + l >= W)
            continue;
          if (C[i + k][j + l] != -1)
            continue;
          if (S[i + k][j + l] == '#')
            continue;
          C[i + k][j + l] = C[i][j] + 1;
          q.push({i + k, j + l});
        }
      }
    } /*
 }
}
count++;*/
  }
  /*for (int i=0; i<H; i++) {
      for (int j=0; j<W; j++) {
          cout << C[i][j] << " ";
      }
      cout << endl;
  }*/
  cout << C[Dh - 1][Dw - 1] << endl;
}

Error: TLE

Fixed submission:

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

const int x[4] = {0, 0, 1, -1};
const int y[4] = {1, -1, 0, 0};
int main() {
  int H, W;
  cin >> H >> W;
  int Ch, Cw, Dh, Dw;
  cin >> Ch >> Cw >> Dh >> Dw;
  string S[H];
  for (int i = 0; i < H; i++)
    cin >> S[i];
  vector<vector<int>> C(H, vector<int>(W));
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++)
      C[i][j] = -1;
  }
  C[Ch - 1][Cw - 1] = 0;
  queue<pair<int, int>> q;
  q.push({Ch - 1, Cw - 1});
  int count = 0;
  while (!q.empty()) {
    while (!q.empty()) {
      pair<int, int> p = q.front();
      q.pop();
      queue<pair<int, int>> qq;
      qq.push(p);
      while (!qq.empty()) {
        pair<int, int> pp = qq.front();
        qq.pop();
        for (int i = 0; i < 4; i++) {
          int xx = pp.first + x[i], yy = pp.second + y[i];
          if (xx < 0 || yy < 0 || xx >= H || yy >= W)
            continue;
          if (C[xx][yy] != -1)
            continue;
          if (S[xx][yy] == '#')
            continue;
          C[xx][yy] = C[p.first][p.second];
          qq.push({xx, yy});
        }
      }
    }
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        if (C[i][j] != count)
          continue;
        for (int k = -2; k < 3; k++) {
          for (int l = -2; l < 3; l++) {
            if (i + k < 0 || j + l < 0 || i + k >= H || j + l >= W)
              continue;
            if (C[i + k][j + l] != -1)
              continue;
            if (S[i + k][j + l] == '#')
              continue;
            C[i + k][j + l] = C[i][j] + 1;
            q.push({i + k, j + l});
          }
        }
      }
    }
    count++;
  }
  /*for (int i=0; i<H; i++) {
      for (int j=0; j<W; j++) {
          cout << C[i][j] << " ";
      }
      cout << endl;
  }*/
  cout << C[Dh - 1][Dw - 1] << endl;
}