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;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
int main() {
  ios::sync_with_stdio(false);
  int h, w, ch, cw, dh, dw;
  cin >> h >> w >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  vector<vector<char>> s(h, vector<char>(w));
  rep(i, h) rep(j, w) cin >> s[i][j];
  int INF = INT32_MAX;
  vector<vector<int>> min_warp(h, vector<int>(w, INF));
  using P = pair<int, pair<int, int>>;
  priority_queue<P, vector<P>, greater<P>> pq; // 座標、ワープ回数
  pq.emplace(make_pair(0, make_pair(ch, cw)));
  vector<int> walk_dx = {0, 0, -1, 1};
  vector<int> walk_dy = {1, -1, 0, 0};
  vector<int> warp_dx, warp_dy;
  for (int x = -2; x <= 2; x++)
    for (int y = -2; y <= 2; y++) {
      if (x == 0 && y == 0)
        continue;
      warp_dx.push_back(x);
      warp_dy.push_back(y);
    }
  min_warp[ch][cw] = 0;
  while (!pq.empty()) {
    int y = pq.top().second.first;
    int x = pq.top().second.second;
    int warp = pq.top().first;
    pq.pop();
    if (min_warp[y][x] < warp)
      continue;
    for (int i = 0; i < 4; i++) {
      int nx = x + walk_dx[i];
      int ny = y + walk_dy[i];
      if (nx < 0 || nx >= w || ny < 0 || ny >= h)
        continue;
      if (min_warp[ny][nx] <= warp)
        continue;
      if (s[ny][nx] == '#')
        continue;
      min_warp[ny][nx] = warp;
      pq.emplace(make_pair(warp, make_pair(ny, nx)));
    }
    for (int i = 0; i < 24; i++) {
      int nx = x + warp_dx[i];
      int ny = y + warp_dy[i];
      if (nx < 0 || nx >= w || ny < 0 || ny >= h)
        continue;
      if (min_warp[ny][nx] <= warp + 1)
        continue;
      if (s[ny][nx] == '#')
        continue;
      min_warp[ny][nx] = warp + 1;
      pq.emplace(make_pair(warp + 1, make_pair(ny, nx)));
    }
  }
  if (min_warp[dh][dw] == INF)
    cout << -1 << endl;
  else
    cout << min_warp[dh][dw] << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
int main() {
  ios::sync_with_stdio(false);
  int h, w, ch, cw, dh, dw;
  cin >> h >> w >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  vector<vector<char>> s(h, vector<char>(w));
  rep(i, h) rep(j, w) cin >> s[i][j];
  int INF = INT32_MAX;
  vector<vector<int>> min_warp(h, vector<int>(w, INF));
  queue<pair<pair<int, int>, int>> q; // 座標、ワープ回数
  q.emplace(make_pair(make_pair(ch, cw), 0));
  vector<int> walk_dx = {0, 0, -1, 1};
  vector<int> walk_dy = {1, -1, 0, 0};
  vector<int> warp_dx, warp_dy;
  for (int x = -2; x <= 2; x++)
    for (int y = -2; y <= 2; y++) {
      if (x == 0 && y == 0)
        continue;
      warp_dx.push_back(x);
      warp_dy.push_back(y);
    }
  min_warp[ch][cw] = 0;
  while (!q.empty()) {
    int y = q.front().first.first;
    int x = q.front().first.second;
    int warp = q.front().second;
    q.pop();
    if (min_warp[y][x] < warp)
      continue;
    for (int i = 0; i < 4; i++) {
      int nx = x + walk_dx[i];
      int ny = y + walk_dy[i];
      if (nx < 0 || nx >= w || ny < 0 || ny >= h)
        continue;
      if (min_warp[ny][nx] <= warp)
        continue;
      if (s[ny][nx] == '#')
        continue;
      min_warp[ny][nx] = warp;
      q.emplace(make_pair(make_pair(ny, nx), warp));
    }
    for (int i = 0; i < 24; i++) {
      int nx = x + warp_dx[i];
      int ny = y + warp_dy[i];
      if (nx < 0 || nx >= w || ny < 0 || ny >= h)
        continue;
      if (min_warp[ny][nx] <= warp + 1)
        continue;
      if (s[ny][nx] == '#')
        continue;
      min_warp[ny][nx] = warp + 1;
      q.emplace(make_pair(make_pair(ny, nx), warp + 1));
    }
  }
  if (min_warp[dh][dw] == INF)
    cout << -1 << endl;
  else
    cout << min_warp[dh][dw] << endl;
}