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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, a, b) for (int i = a; i < b; i++)
#define all(x) x.begin(), x.end()
#define fi first
#define se second
#define pb push_back
#define debug(x) cerr << __LINE__ << ": " << #x << " -> " << x << endl
#define debug_vec(v)                                                           \\
  cerr << __LINE__ << ": " << #v << " -> ";                                    \\
  rep(i, v.size()) cerr << " " << v[i];                                        \\
  cerr << endl
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const int INF = (1 << 30) - 1;
const ll LINF = (1LL << 60) - 1;

int dy[] = {1, 0, -1, 0};
int dx[] = {0, 1, 0, -1};
int h, w, sy, sx, gy, gx;
string board[1010];

bool check(int i, int j) {
  return i >= 0 && i < h && j >= 0 && j < w && board[i][j] == '.';
}

void solve() {
  cin >> h >> w >> sy >> sx >> gy >> gx;
  sy--;
  sx--;
  gy--;
  gx--;
  rep(i, h) cin >> board[i];
  int warp[1010][1010];
  memset(warp, -1, sizeof(warp));
  using P = pair<int, pii>;
  priority_queue<P, vector<P>, greater<P>> pq;
  warp[sy][sx] = 0;
  pq.emplace(0, pii(sy, sx));
  while (pq.size()) {
    int y, x;
    tie(y, x) = pq.top().se;
    pq.pop();
    bool f = 0;
    rep(i, 4) {
      int ny = y + dy[i];
      int nx = x + dx[i];
      if (!check(ny, nx))
        continue;
      if (warp[ny][nx] == -1 || warp[ny][nx] > warp[y][x]) {
        warp[ny][nx] = warp[y][x];
        pq.emplace(warp[ny][nx], pii(ny, nx));
      }
    }
    REP(i, -2, 3) REP(j, -2, 3) {
      int ny = y + i;
      int nx = x + j;
      if (!check(ny, nx))
        continue;
      if (warp[ny][nx] == -1 || warp[ny][nx] > warp[y][x] + 1) {
        warp[ny][nx] = warp[y][x] + 1;
        pq.emplace(warp[ny][nx], pii(ny, nx));
      }
    }
  }
  cout << warp[gy][gx] << endl;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  solve();
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, a, b) for (int i = a; i < b; i++)
#define all(x) x.begin(), x.end()
#define fi first
#define se second
#define pb push_back
#define debug(x) cerr << __LINE__ << ": " << #x << " -> " << x << endl
#define debug_vec(v)                                                           \\
  cerr << __LINE__ << ": " << #v << " -> ";                                    \\
  rep(i, v.size()) cerr << " " << v[i];                                        \\
  cerr << endl
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const int INF = (1 << 30) - 1;
const ll LINF = (1LL << 60) - 1;

int dy[] = {1, 0, -1, 0};
int dx[] = {0, 1, 0, -1};
int h, w, sy, sx, gy, gx;
string board[1010];

bool check(int i, int j) { return i >= 0 && i < h && j >= 0 && j < w; }

void solve() {
  cin >> h >> w >> sy >> sx >> gy >> gx;
  sy--;
  sx--;
  gy--;
  gx--;
  rep(i, h) cin >> board[i];
  int warp[1010][1010];
  memset(warp, -1, sizeof(warp));
  queue<pii> que;
  warp[sy][sx] = 0;
  que.push({sy, sx});
  while (que.size()) {
    int y, x;
    tie(y, x) = que.front();
    que.pop();
    bool f = 0;
    rep(i, 4) {
      int ny = y + dy[i];
      int nx = x + dx[i];
      if (!check(ny, nx))
        continue;
      if (board[ny][nx] == '#') {
        f = 1;
        continue;
      }
      if (warp[ny][nx] == -1 || warp[ny][nx] > warp[y][x]) {
        warp[ny][nx] = warp[y][x];
        que.push({ny, nx});
      }
    }
    if (f) {
      REP(i, -2, 3) REP(j, -2, 3) {
        int ny = y + i;
        int nx = x + j;
        if (!check(ny, nx) || board[ny][nx] == '#')
          continue;
        if (warp[ny][nx] == -1 || warp[ny][nx] > warp[y][x] + 1) {
          warp[ny][nx] = warp[y][x] + 1;
          que.push({ny, nx});
        }
      }
    }
  }
  cout << warp[gy][gx] << endl;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  solve();
  return 0;
}