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

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

int H, W;
string maze[1005];
int dp[1005][1005];
const int inf = 1 << 30;

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, -1, 0, 1};

int main(void) {
  cin >> H >> W;
  int sx, sy, gx, gy;
  cin >> sx >> sy >> gx >> gy;
  sx--;
  sy--;
  gx--;
  gy--;
  for (int i = 0; i < H; i++)
    cin >> maze[i];

  for (int i = 0; i < H; i++)
    for (int j = 0; j < W; j++)
      dp[i][j] = inf;

  queue<pair<int, int>> q;
  queue<pair<int, int>> q2;
  q.push({sx, sy});
  dp[sx][sy] = 0;
  int cnt = 0;

  while (!q.empty()) {
    while (!q.empty()) {
      int x = q.front().first;
      int y = q.front().second;
      q.pop();
      if (x == gx && y == gy) {
        cout << cnt << endl;
        return 0;
      }
      if (maze[x][y] == '#')
        continue;

      maze[x][y] = '#';
      // ４近傍
      for (int k = 0; k < 4; k++) {
        int nx = x + dx[k];
        int ny = y + dy[k];

        if (0 <= nx && nx < H && 0 <= ny && ny < W) {
          if (maze[nx][ny] == '.') {
            q.push({nx, ny});
          }
        }
      }

      for (int i = -2; i < 3; i++) {
        for (int j = -2; j < 3; j++) {
          int nx = x + i;
          int ny = y + j;
          if (0 <= nx && nx < H && 0 <= ny && ny < W) {
            if (maze[nx][ny] == '.') {
              q2.push({nx, ny});
            }
          }
        }
      }
    }
    swap(q, q2);
    cnt++;
  }
  // for(int i=0; i<H; i++){
  //     for (int j=0; j<W; j++){
  //       cout << dp[i][j] << " ";
  //     }
  //     cout << endl;
  // }
  if (dp[gx][gy] != inf)
    cout << dp[gx][gy] << endl;
  else
    cout << -1 << endl;
}


Error: TLE

Fixed submission:

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

int H, W;
string maze[1005];
int dp[1005][1005];
const int inf = 1 << 30;

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, -1, 0, 1};

int main(void) {
  cin >> H >> W;
  int sx, sy, gx, gy;
  cin >> sx >> sy >> gx >> gy;
  sx--;
  sy--;
  gx--;
  gy--;
  for (int i = 0; i < H; i++)
    cin >> maze[i];

  for (int i = 0; i < H; i++)
    for (int j = 0; j < W; j++)
      dp[i][j] = inf;

  queue<pair<int, int>> q;
  queue<pair<int, int>> q2;
  q2.push({sx, sy});
  q.push({sx, sy});
  dp[sx][sy] = 0;

  while (!q.empty() || !q2.empty()) {
    while (!q.empty()) {
      int x = q.front().first;
      int y = q.front().second;
      q.pop();

      // ４近傍
      for (int k = 0; k < 4; k++) {
        int nx = x + dx[k];
        int ny = y + dy[k];

        if (0 <= nx && nx < H && 0 <= ny && ny < W) {
          if (maze[nx][ny] == '.') {
            if (dp[nx][ny] > dp[x][y]) {
              q2.push({nx, ny});
              q.push({nx, ny});
              dp[nx][ny] = dp[x][y];
            }
          }
        }
      }
      // wa-pu
    }

    while (!q2.empty()) {
      int x2 = q2.front().first;
      int y2 = q2.front().second;
      q2.pop();
      for (int i = -2; i < 3; i++) {
        for (int j = -2; j < 3; j++) {
          int nx = x2 + i;
          int ny = y2 + j;
          if (0 <= nx && nx < H && 0 <= ny && ny < W) {
            if (maze[nx][ny] == '.') {
              if (dp[nx][ny] > dp[x2][y2] + 1) {
                q.push({nx, ny});
                q2.push({nx, ny});
                dp[nx][ny] = dp[x2][y2] + 1;
              }
            }
          }
        }
      }
    }
  }
  // for(int i=0; i<H; i++){
  //     for (int j=0; j<W; j++){
  //       cout << dp[i][j] << " ";
  //     }
  //     cout << endl;
  // }
  if (dp[gx][gy] != inf)
    cout << dp[gx][gy] << endl;
  else
    cout << -1 << endl;
}