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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)

using namespace std;

int main() {
  int h, w;
  cin >> h >> w;
  int ch, cw; // Start
  cin >> ch >> cw;
  int sy = ch - 1, sx = cw - 1;
  int dh, dw; // End
  cin >> dh >> dw;
  int ey = dh - 1, ex = dw - 1;
  vector<vector<char>> grid(h, vector<char>(w));
  rep(i, h) rep(j, w) cin >> grid[i][j];
  vector<vector<int>> warp(
      h, vector<int>(w, -1)); // そのマスに到達するためのwarp回数,-1で初期化

  queue<pair<int, int>> que;  // moveAで移動できるマスの処理
  queue<pair<int, int>> next; // moveBで移動できるマスの処理,待機用
  que.push({sy, sx});

  int count = 0; // 現在のwarp回数
  while (que.size() != 0 || next.size() != 0) {
    while (que.size()) { // queの消化
      int y = que.front().first;
      int x = que.front().second;
      que.pop();

      if (warp[y][x] != -1 &&
          warp[y][x] <= count) // 既に処理済みのマスなら飛ばす
        continue;

      warp[y][x] = count;

      for (int dy = -2; dy <= 2; dy++) // 周囲25マスの探索
        for (int dx = -2; dx <= 2; dx++) {
          int new_y = y + dy;
          int new_x = x + dx;
          if (new_y < 0 || new_y >= h || new_x < 0 || new_x >= w) {
            continue;
          }
          if (grid[new_y][new_x] == '#') {
            continue;
          }
          if (abs(dy) + abs(dx) == 0) {
            continue;
          }
          if (abs(dy) + abs(dx) == 1) { // moveA
            if (warp[new_y][new_x] == -1) {
              que.push({new_y, new_x});
            }
          }
          if (abs(dy) + abs(dx) > 1) { // moveB
            if (warp[new_y][new_x] == -1) {
              next.push({new_y, new_x});
            }
          }
        }
    }
    count++;         // warp回数を1増やして次のループへ
    swap(que, next); // 待機中のmoveBのマスを探索する
  }
  /*
  rep(i, h) { //warp表示用
          rep(j, w)
                  cout << warp[i][j];
          cout << endl;
  }
  */

  cout << warp[ey][ex] << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)

using namespace std;

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

int main() {
  int h, w;
  cin >> h >> w;
  int ch, cw; // Start
  cin >> ch >> cw;
  int sy = ch - 1, sx = cw - 1;
  int dh, dw; // End
  cin >> dh >> dw;
  int ey = dh - 1, ex = dw - 1;
  vector<vector<char>> grid(h, vector<char>(w));
  rep(i, h) rep(j, w) cin >> grid[i][j];
  vector<vector<int>> warp(
      h, vector<int>(w, -1)); // そのマスに到達するためのwarp回数,-1で初期化
  warp[sy][sx] = 0;

  queue<pair<int, int>> que;
  queue<pair<int, int>> next;
  que.push({sy, sx});
  int count = 0; // 現在のwarp回数
  while (que.size() != 0 || next.size() != 0) {
    while (que.size()) { // queの消化
      int y = que.front().first;
      int x = que.front().second;
      que.pop();

      if (warp[y][x] != -1 && warp[y][x] < count)
        continue;

      warp[y][x] = count;

      for (int dy = -2; dy <= 2; dy++) // 周囲25マスの探索
        for (int dx = -2; dx <= 2; dx++) {
          int new_y = y + dy;
          int new_x = x + dx;
          if (new_y < 0 || new_y >= h || new_x < 0 || new_x >= w) {
            continue;
          }
          if (grid[new_y][new_x] == '#') {
            continue;
          }
          if (abs(dy) + abs(dx) == 0) {
            continue;
          }
          if (abs(dy) + abs(dx) == 1) { // moveA
            if (warp[new_y][new_x] == -1) {
              que.push({new_y, new_x});
            }
          }
          if (abs(dy) + abs(dx) > 1) { // moveB
            if (warp[new_y][new_x] == -1) {
              next.push({new_y, new_x});
            }
          }
        }
    }
    count++;
    swap(que, next);
  }
  /*
  rep(i, h) { //warp表示用
          rep(j, w)
                  cout << warp[i][j];
          cout << endl;
  }
  */
  cout << warp[ey][ex] << endl;

  return 0;
}