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

#include <bits/stdc++.h>
using namespace std;
using P = pair<int, int>;
int main() {
  int H, W, sx, sy, gx, gy;
  cin >> H >> W >> sx >> sy >> gx >> gy;
  vector<vector<char>> maze(H + 2, vector<char>(W + 2, '#'));
  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      cin >> maze[i][j];
    }
  }

  int INF = 1e9;

  vector<vector<int>> dist(H + 2, vector<int>(W + 2, INF));
  queue<P> Q;
  P start = {sx, sy};
  Q.push(start);
  dist[sx][sy] = 0;

  queue<P> Q2;
  Q2.push(start);

  vector<int> dx = {0, 1, -1, 2, -2};
  vector<int> dy = {0, 1, -1, 2, -2};

  vector<int> DX = {0, 1, 0, -1};
  vector<int> DY = {1, 0, -1, 0};

  while (Q.size()) {
    P p = Q.front();
    Q.pop();
    for (int i = 0; i < 4; i++) {
      int a = p.first + DX[i];
      int b = p.second + DY[i];
      if (maze[a][b] == '#') {
        continue;
      }
      if (dist[a][b] > dist[p.first][p.second]) {
        dist[a][b] = dist[p.first][p.second];
        P add = {a, b};
        Q.push(add);
        Q2.push(add);
      }
    }
    if (Q.size() == 0) {
      vector<P> after(0);
      while (Q2.size()) {
        P q = Q2.front();
        Q2.pop();
        for (int i = 0; i < 5; i++) {
          for (int j = 0; j < 5; j++) {
            int x = q.first + dx[i];
            int y = q.second + dy[j];
            if (x < 0 || x >= H + 2 || y < 0 || y >= W + 2) {
              continue;
            }
            if (maze[x][y] == '#') {
              continue;
            }
            if (abs(dx[i]) + abs(dy[j]) > 1) {
              if (dist[x][y] > dist[q.first][q.second] + 1) {
                dist[x][y] = dist[q.first][q.second] + 1;
                P add = {x, y};
                Q.push(add);
                after.push_back(add);
              }
            }
          }
        }
      }
      for (int i = 0; i < after.size(); i++) {
        Q2.push(after[i]);
      }
    }
  }

  if (dist[gx][gy] == INF) {
    cout << -1 << endl;
    return 0;
  }
  cout << dist[gx][gy] << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using P = pair<int, int>;
int main() {
  int H, W, sx, sy, gx, gy;
  cin >> H >> W >> sx >> sy >> gx >> gy;
  vector<vector<char>> maze(H + 2, vector<char>(W + 2, '#'));
  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      cin >> maze[i][j];
    }
  }

  int INF = 1e9;

  vector<vector<int>> dist(H + 2, vector<int>(W + 2, INF));
  queue<P> Q;
  P start = {sx, sy};
  Q.push(start);
  dist[sx][sy] = 0;

  queue<P> Q2;
  Q2.push(start);

  vector<int> dx = {0, 1, -1, 2, -2};
  vector<int> dy = {0, 1, -1, 2, -2};

  vector<int> DX = {0, 1, 0, -1};
  vector<int> DY = {1, 0, -1, 0};

  while (Q.size()) {
    P p = Q.front();
    Q.pop();
    for (int i = 0; i < 4; i++) {
      int a = p.first + DX[i];
      int b = p.second + DY[i];
      if (maze[a][b] == '#') {
        continue;
      }
      if (dist[a][b] > dist[p.first][p.second]) {
        dist[a][b] = dist[p.first][p.second];
        P add = {a, b};
        Q.push(add);
        Q2.push(add);
      }
    }
    if (Q.size() == 0) {
      while (Q2.size()) {
        P q = Q2.front();
        Q2.pop();
        for (int i = 0; i < 5; i++) {
          for (int j = 0; j < 5; j++) {
            int x = q.first + dx[i];
            int y = q.second + dy[j];
            if (x < 0 || x >= H + 2 || y < 0 || y >= W + 2) {
              continue;
            }
            if (maze[x][y] == '#') {
              continue;
            }
            if (abs(dx[i]) + abs(dy[j]) > 1) {
              if (dist[x][y] > dist[q.first][q.second] + 1) {
                dist[x][y] = dist[q.first][q.second] + 1;
                P add = {x, y};
                Q.push(add);
                Q2.push(add);
              }
            }
          }
        }
      }
    }
  }

  if (dist[gx][gy] == INF) {
    cout << -1 << endl;
    return 0;
  }
  cout << dist[gx][gy] << endl;
}