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

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
typedef pair<int, int> P;
#define SORT(a) sort((a).begin(), (a).end())
#define REV(a) reverse((a).begin(), (a).end())
#define For(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) For(i, 0, n)
#define debug(x) cerr << #x << " = " << (x) << endl;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

void coY() { cout << "Yes" << endl; }
void coN() { cout << "No" << endl; }

// Write From this Line

// const ll mod = 1e9+7;
// const ll mod = 998244353;

const int dy[] = {0, 0, 1, -1};
const int dx[] = {1, -1, 0, 0};
const int inf = 1e9;
int main() {
  int h, w;
  cin >> h >> w;

  int sh, sw;
  cin >> sh >> sw;
  int gh, gw;
  cin >> gh >> gw;
  sh--, sw--, gh--, gw--;

  vector<string> field(h);
  rep(i, h) cin >> field[i];

  vector<vector<bool>> seen(h, vector<bool>(w, false));
  vector<vector<int>> dist(h, vector<int>(w, inf));

  queue<P> q;
  queue<P> nq;
  q.push({sh, sw});
  dist[sh][sw] = 0;
  seen[sh][sw] = true; // コストが確定したらtrue
  while (1) {
    bool u = true;
    while (!q.empty()) {
      u = false;
      // BFSでコスト0で行けるところをマーキングする
      int y = q.front().first;
      int x = q.front().second;
      nq.push({y, x});
      q.pop();
      rep(i, 4) {
        int ny = y + dy[i];
        int nx = x + dx[i];
        if (ny < 0 || ny >= h || nx < 0 || nx >= w)
          continue;
        if (seen[ny][nx])
          continue;
        if (field[ny][nx] == '#')
          continue;

        q.push({ny, nx});
        seen[ny][nx] = true;
        dist[ny][nx] = dist[y][x];
      }
    }
    while (!nq.empty()) {
      u = false;
      // コスト0で行けるところはマーキング済み。
      // nq からワープを使って行けるとこを登録していく
      int y = nq.front().first;
      int x = nq.front().second;
      nq.pop();

      for (int i = -2; i <= 2; i++) {
        for (int j = -2; j <= 2; j++) {
          int ny = y + i;
          int nx = x + j;
          if (ny < 0 || ny >= h || nx < 0 || nx >= w)
            continue;
          if (seen[ny][nx])
            continue;
          if (field[ny][nx] == '#')
            continue;

          dist[ny][nx] = dist[y][x] + 1;
          seen[ny][nx] = true;
          q.push({ny, nx});
        }
      }
    }
    if (u)
      break;
  }
  int ans = dist[gh][gw];
  if (ans == inf)
    ans = -1;
  cout << ans << endl;

  // rep(i,h){
  //	rep(j,w){
  //		cout << dist[i][j] << " ";
  //	}
  //	cout << endl;
  // }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
typedef pair<int, int> P;
#define SORT(a) sort((a).begin(), (a).end())
#define REV(a) reverse((a).begin(), (a).end())
#define For(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) For(i, 0, n)
#define debug(x) cerr << #x << " = " << (x) << endl;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

void coY() { cout << "Yes" << endl; }
void coN() { cout << "No" << endl; }

// Write From this Line

// const ll mod = 1e9+7;
// const ll mod = 998244353;
const ll unit = 1e9;

const int dx[] = {-2, -2, -2, -2, -2, -1, -1, -1, -1, -1, 0, 0,
                  0,  0,  1,  1,  1,  1,  1,  2,  2,  2,  2, 2};
const int dy[] = {-2, -1, 0,  1,  2, -2, -1, 0,  1,  2, -2, -1,
                  1,  2,  -2, -1, 0, 1,  2,  -2, -1, 0, 1,  2};
int main() {
  int h, w;
  cin >> h >> w;

  int ch, cw, dh, dw;
  cin >> ch >> cw >> dh >> dw;
  ch--, cw--, dh--, dw--;
  vector<string> field(h);
  rep(i, h) cin >> field[i];

  // 道であるマス全てについて、最短何手でいけるかを更新していく。
  vector<vector<ll>> dist(1005, vector<ll>(1005, unit));
  dist[ch][cw] = 0;
  queue<pair<int, int>> nekochan;
  nekochan.push({ch, cw});

  while (!nekochan.empty()) {
    int y = nekochan.front().first;
    int x = nekochan.front().second;
    nekochan.pop();
    rep(i, 24) {
      int ny = y + dy[i];
      int nx = x + dx[i];
      if (ny < 0 || nx < 0 || ny >= h || nx >= w)
        continue;
      if (field[ny][nx] == '#')
        continue;
      else {
        int kyori = abs(dy[i]) + abs(dx[i]);
        if (kyori == 1) {
          if (chmin(dist[ny][nx], dist[y][x])) {
            nekochan.push({ny, nx});
          }
        } else {
          // use ワープ
          if (chmin(dist[ny][nx], dist[y][x] + 1)) {
            nekochan.push({ny, nx});
          }
        }
      }
    }
  }
  if (dist[dh][dw] == unit) {
    cout << -1 << endl;
  } else {
    cout << dist[dh][dw] << endl;
  }
  // rep(i,h){
  //	rep(j,w){
  //		cout << dist[i][j] << " ";
  //	}
  //	cout << endl;
  // }
}