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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

const int di[] = {-1, 0, 1, 0};
const int dj[] = {0, -1, 0, 1};

int main() {
  int h, w;
  cin >> h >> w;
  // スタートの座標
  int si, sj;
  cin >> si >> sj;
  // ゴールの座標
  int ti, tj;
  cin >> ti >> tj;
  --si;
  --sj;
  --ti;
  --tj;
  // 迷路
  vector<string> s(h);
  rep(i, h) cin >> s[i];

  const int INF = 1e9;
  // 移動距離
  vector<vector<int>> dist(h, vector<int>(w, INF));
  dist[si][sj] = 0;
  // キューに前後から詰めるため、dequeを使う
  deque<P> q;
  q.emplace_back(si, sj);

  while (!q.empty()) {
    int i = q.front().first;
    int j = q.front().second;
    int d = dist[i][j];
    q.pop_front();

    //?
    if (dist[i][j] != d)
      continue;

    // 通常の前後左右の移動
    rep(v, 4) {
      int ni = i + di[v], nj = j + dj[v];
      if (ni < 0 || ni >= h || nj < 0 || nj >= w)
        continue;
      if (s[ni][nj] == '#')
        continue;
      if (dist[ni][nj] <= d)
        continue; //
      dist[ni][nj] = d;
      // ワープ回数が増えない場合は、前から詰める
      q.emplace_front(ni, nj);
    }
    // ワープ移動
    for (int ei = -2; ei <= 2; ++ei) {
      for (int ej = -2; ej <= 2; ++ej) {
        int ni = i + ei, nj = j + ej;
        if (ni < 0 || ni >= h || nj < 0 || nj >= w)
          continue;
        if (s[ni][nj] == '#')
          continue;
        if (dist[ni][nj] <= d + 1)
          continue;
        dist[ni][nj] = d + 1;
        // ワープ回数が増えるものは後ろから詰める
        q.emplace_back(ni, nj);
      }
    }
  }
  int ans = dist[ti][tj];
  if (ans == INF)
    ans = -1;
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <queue>

using namespace std;

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<vector<char>> maze(h, vector<char>(w));
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> maze[i][j];
    }
  }

  vector<vector<int>> visited(h, vector<int>(w, -1));
  visited[ch][cw] = 0;
  queue<pair<int, int>> q;
  q.push(make_pair(ch, cw));

  vector<int> dy(4);
  dy = {1, -1, 0, 0};
  vector<int> dx(4);
  dx = {0, 0, 1, -1};

  vector<int> wy(20);
  vector<int> wx(20);
  wy = {2, 2, 2, 2, 2, 1, 1, 1, 1, 0, 0, -1, -1, -1, -1, -2, -2, -2, -2, -2};
  wx = {-2, -1, 0, 1, 2, -2, -1, 1, 2, -2, 2, -2, -1, 1, 2, -2, -1, 0, 1, 2};

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

    int cnt = visited[y][x];

    for (int i = 0; i < 4; i++) {
      int ny = y + dy[i];
      int nx = x + dx[i];
      if (ny < 0 || ny >= h || nx < 0 || nx >= w)
        continue;
      if (maze[ny][nx] == '#')
        continue;
      if (visited[ny][nx] <= cnt && visited[ny][nx] != -1)
        continue;

      visited[ny][nx] = cnt;
      q.push(make_pair(ny, nx));
    }

    cnt++;
    for (int i = 0; i < 20; i++) {
      int ny = y + wy[i];
      int nx = x + wx[i];
      if (ny < 0 || ny >= h || nx < 0 || nx >= w)
        continue;
      if (maze[ny][nx] == '#')
        continue;
      if (visited[ny][nx] <= cnt && visited[ny][nx] != -1)
        continue;

      visited[ny][nx] = cnt;
      q.push(make_pair(ny, nx));
    }
  }
  cout << visited[dh][dw] << endl;
}