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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define repr(i, n) for (int i = n - 1; i >= 0; i--)
#define repr1(i, n) for (int i = n; i >= 1; i--)
#define all(v) v.begin(), v.end()
using ll = long long;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
const int INF = 1e9;
const ll LLINF = 1e18;
const ll MOD = 1e9 + 7;
const double EPS = 1e-10;
const double PI = acos(-1);
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 dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, -1, 0, 1};

int h, w, sy, sx, gy, gx;
string s[1010];
int dist[1010][1010];

int main() {
  cin >> h >> w;
  cin >> sy >> sx >> gy >> gx;
  sy--;
  sx--;
  gy--;
  gx--;
  rep(i, h) cin >> s[i];

  // BFS
  deque<pii> q;
  q.emplace_back(sy, sx);
  rep(y, h) rep(x, w) dist[y][x] = INF;
  dist[sy][sx] = 0;
  while (q.size()) {
    pii now = q.front();
    q.pop_front();
    int y = now.first;
    int x = now.second;

    // move to an adjacent square
    rep(i, 4) {
      int ny = y + dy[i];
      int nx = x + dx[i];
      if (nx < 0 || nx >= w || ny < 0 || ny >= h)
        continue;
      if (s[ny][nx] == '#')
        continue;
      if (chmin(dist[ny][nx], dist[y][x]))
        q.emplace_front(ny, nx);
    }

    // warp
    for (int ry = -2; ry <= 2; ry++) {
      for (int rx = -2; rx <= 2; rx++) {
        int ny = y + ry;
        int nx = x + rx;
        if (nx < 0 || nx >= w || ny < 0 || ny >= h)
          continue;
        if (s[ny][nx] == '#')
          continue;
        if (chmin(dist[ny][nx], dist[y][x] + 1))
          q.emplace_back(ny, nx);
      }
    }
  }

  int ans = dist[gy][gx];
  if (ans == INF)
    ans = -1;
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define repr(i, n) for (int i = n - 1; i >= 0; i--)
#define repr1(i, n) for (int i = n; i >= 1; i--)
#define all(v) v.begin(), v.end()
using ll = long long;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
const int INF = 1e9;
const ll LLINF = 1e18;
const ll MOD = 1e9 + 7;
const double EPS = 1e-10;
const double PI = acos(-1);
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 dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, -1, 0, 1};

int h, w, sy, sx, gy, gx;
string s[1010];
int dist[1010][1010];

int main() {
  cin >> h >> w;
  cin >> sy >> sx >> gy >> gx;
  sy--;
  sx--;
  gy--;
  gx--;
  rep(i, h) cin >> s[i];

  // BFS
  queue<pii> q;
  q.emplace(sy, sx);
  rep(y, h) rep(x, w) dist[y][x] = INF;
  dist[sy][sx] = 0;
  while (q.size()) {
    pii now = q.front();
    q.pop();
    int y = now.first;
    int x = now.second;

    // move to an adjacent square
    rep(i, 4) {
      int ny = y + dy[i];
      int nx = x + dx[i];
      if (nx < 0 || nx >= w || ny < 0 || ny >= h)
        continue;
      if (s[ny][nx] == '#')
        continue;
      if (chmin(dist[ny][nx], dist[y][x]))
        q.emplace(ny, nx);
    }

    // warp
    for (int ry = -2; ry <= 2; ry++) {
      for (int rx = -2; rx <= 2; rx++) {
        if (ry == 0 && rx == 0)
          continue;
        int ny = y + ry;
        int nx = x + rx;
        if (nx < 0 || nx >= w || ny < 0 || ny >= h)
          continue;
        if (s[ny][nx] == '#')
          continue;
        if (chmin(dist[ny][nx], dist[y][x] + 1))
          q.emplace(ny, nx);
      }
    }
  }

  int ans = dist[gy][gx];
  if (ans == INF)
    ans = -1;
  cout << ans << endl;
}