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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define sz(x) int(x.size())
#define show(x)                                                                \\
  {                                                                            \\
    for (auto i : x) {                                                         \\
      cout << i << " ";                                                        \\
    }                                                                          \\
    cout << endl;                                                              \\
  }
using namespace std;
using ll = long long;
using P = pair<int, int>;

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

int main() {
  // 全探索
  int H, W;
  cin >> H >> W;
  int CH, CW;
  cin >> CH >> CW;
  --CH;
  --CW;
  int DH, DW;
  cin >> DH >> DW;
  --DH;
  --DW;
  vector<string> S(H);
  rep(i, H) {
    string s;
    cin >> s;
    S[i] = s;
  }

  vector<vector<int>> dist(H, vector<int>(W, INF));
  queue<P> que;
  queue<P> queWarp;
  auto update = [&](int i, int j, int d) {
    if (dist[i][j] < d)
      return;
    dist[i][j] = d;
    que.emplace(i, j);
  };
  auto updateWarp = [&](int i, int j, int d) {
    if (dist[i][j] < d)
      return;
    dist[i][j] = d;
    queWarp.emplace(i, j);
  };
  update(CH, CW, 0);
  // queWarp.emplace(CH, CW);
  while (!que.empty() || !queWarp.empty()) {
    if (!que.empty()) {
      int ci = que.front().first;
      int cj = que.front().second;
      que.pop();
      int cd = dist[ci][cj]; // 現在のワープ魔法の回数
      rep(dir, 4) {
        int ni = ci + dx[dir];
        int nj = cj + dy[dir];
        if (ni < 0 || ni >= H || nj < 0 || nj >= W)
          continue;
        if (S[ni][nj] == '#' || dist[ni][nj] <= cd)
          continue;
        update(ni, nj, cd);
      }
      for (int i = -2; i <= 2; i++) {
        for (int j = -2; j <= 2; j++) {
          int ni = ci + i;
          int nj = cj + j;
          if (ni < 0 || ni >= H || nj < 0 || nj >= W)
            continue;
          if (S[ni][nj] == '#' || dist[ni][nj] <= cd)
            continue;
          updateWarp(ni, nj, cd + 1);
        }
      }
    } else if (!queWarp.empty()) {
      int ci = queWarp.front().first;
      int cj = queWarp.front().second;
      queWarp.pop();
      int cd = dist[ci][cj]; // 現在のワープ魔法の回数
      rep(dir, 4) {
        int ni = ci + dx[dir];
        int nj = cj + dy[dir];
        if (ni < 0 || ni >= H || nj < 0 || nj >= W)
          continue;
        if (S[ni][nj] == '#' || dist[ni][nj] <= cd)
          continue;
        update(ni, nj, cd);
      }
      for (int i = -2; i <= 2; i++) {
        for (int j = -2; j <= 2; j++) {
          int ni = ci + i;
          int nj = cj + j;
          if (ni < 0 || ni >= H || nj < 0 || nj >= W)
            continue;
          if (S[ni][nj] == '#' || dist[ni][nj] <= cd)
            continue;
          updateWarp(ni, nj, cd + 1);
        }
      }
    }
  }
  int ans = dist[DH][DW];
  if (ans == INF)
    ans = -1;
  cout << ans << '\\n';
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define sz(x) int(x.size())
#define show(x)                                                                \\
  {                                                                            \\
    for (auto i : x) {                                                         \\
      cout << i << " ";                                                        \\
    }                                                                          \\
    cout << endl;                                                              \\
  }
using namespace std;
using ll = long long;
using P = pair<int, int>;

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

int main() {
  // 全探索
  int H, W;
  cin >> H >> W;
  int CH, CW;
  cin >> CH >> CW;
  --CH;
  --CW;
  int DH, DW;
  cin >> DH >> DW;
  --DH;
  --DW;
  vector<string> S(H);
  rep(i, H) {
    string s;
    cin >> s;
    S[i] = s;
  }

  vector<vector<int>> dist(H, vector<int>(W, INF));
  queue<P> que;
  auto update = [&](int i, int j, int d) {
    if (dist[i][j] < d)
      return;
    dist[i][j] = d;
    que.emplace(i, j);
  };
  update(CH, CW, 0);
  while (!que.empty()) {
    int ci = que.front().first;
    int cj = que.front().second;
    que.pop();
    int cd = dist[ci][cj]; // 現在のワープ魔法の回数
    rep(dir, 4) {
      int ni = ci + dx[dir];
      int nj = cj + dy[dir];
      if (ni < 0 || ni >= H || nj < 0 || nj >= W)
        continue;
      if (S[ni][nj] == '#' || dist[ni][nj] <= cd)
        continue;
      update(ni, nj, cd);
    }
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        int ni = ci + i;
        int nj = cj + j;
        if (ni < 0 || ni >= H || nj < 0 || nj >= W)
          continue;
        if (S[ni][nj] == '#' || dist[ni][nj] <= cd)
          continue;
        update(ni, nj, cd + 1);
      }
    }
  }
  int ans = dist[DH][DW];
  if (ans == INF)
    ans = -1;
  cout << ans << '\\n';
  return 0;
}