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

#include <algorithm>
#include <deque>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
using ll = long long;

int main() {
  int H, W;
  cin >> H >> W;
  int ch, cw, dh, dw;
  cin >> ch >> cw >> dh >> dw;
  vector<string> s(H);
  for (int i = 0; i < H; i++)
    cin >> s[i];

  // 0～h,wの座標系で処理をするため
  ch--;
  cw--;
  dh--;
  dw--;

  deque<pair<int, int>> que;
  vector<vector<int>> dist(H, vector<int>(W));
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      dist[i][j] = -1;
    }
  }

  // 初期条件
  que.push_front(make_pair(ch, cw));
  dist[ch][cw] = 0;

  // bfs
  // distはそのマスにたどり着くために必要な最小のワープ回数
  while (!que.empty()) {
    pair<int, int> v = que.front();
    que.pop_front();

    int h = v.first;
    int w = v.second;

    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        int nh = h + i;
        int nw = w + j;

        if (nh < 0 || nh >= H || nw < 0 || nw >= W)
          continue;
        if (nh == h && nw == w)
          continue;
        if (s[nh][nw] == '#')
          continue;

        if (!((i == 0 && j == -1) || (i == 0 && j == 1) ||
              (i == -1 && j == 0) || (i == 1 && j == 0))) {
          if (dist[nh][nw] == -1) {
            dist[nh][nw] = dist[h][w] + 1;
            que.push_back(make_pair(nh, nw));
          } else if (dist[nh][nw] > dist[h][w] && dist[h][w] != -1) {
            dist[nh][nw] = dist[h][w] + 1;
            que.push_back(make_pair(nh, nw));
          }
        } else {
          if (dist[nh][nw] == -1) {
            dist[nh][nw] = dist[h][w];
            que.push_front(make_pair(nh, nw));
          } else if (dist[nh][nw] > dist[h][w] && dist[h][w] != -1) {
            dist[nh][nw] = dist[h][w];
            que.push_front(make_pair(nh, nw));
          }
        }
      }
    }
  }

  cout << dist[dh][dw] << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <deque>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
using ll = long long;

int main() {
  int H, W;
  cin >> H >> W;
  int ch, cw, dh, dw;
  cin >> ch >> cw >> dh >> dw;
  vector<string> s(H);
  for (int i = 0; i < H; i++)
    cin >> s[i];

  // 0～h,wの座標系で処理をするため
  ch--;
  cw--;
  dh--;
  dw--;

  queue<pair<int, int>> que;
  vector<vector<int>> dist(H, vector<int>(W));
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      dist[i][j] = -1;
    }
  }

  // 初期条件
  que.push(make_pair(ch, cw));
  dist[ch][cw] = 0;

  // bfs
  // distはそのマスにたどり着くために必要な最小のワープ回数
  while (!que.empty()) {
    pair<int, int> v = que.front();
    que.pop();

    int h = v.first;
    int w = v.second;

    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        int nh = h + i;
        int nw = w + j;

        if (nh < 0 || nh >= H || nw < 0 || nw >= W)
          continue;
        if (nh == h && nw == w)
          continue;
        if (s[nh][nw] == '#')
          continue;

        if (!((i == 0 && j == -1) || (i == 0 && j == 1) ||
              (i == -1 && j == 0) || (i == 1 && j == 0))) {
          if (dist[nh][nw] == -1) {
            dist[nh][nw] = dist[h][w] + 1;
            que.push(make_pair(nh, nw));
          } else if (dist[nh][nw] > dist[h][w] && dist[h][w] != -1) {
            dist[nh][nw] = dist[h][w] + 1;
            que.push(make_pair(nh, nw));
          }
        } else {
          if (dist[nh][nw] == -1) {
            dist[nh][nw] = dist[h][w];
            que.push(make_pair(nh, nw));
          } else if (dist[nh][nw] > dist[h][w] && dist[h][w] != -1) {
            dist[nh][nw] = dist[h][w];
            que.push(make_pair(nh, nw));
          }
        }
      }
    }
  }

  cout << dist[dh][dw] << endl;
}