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

#include <bits/stdc++.h>
const int INF = 1e9;
const int MOD = 1e9 + 7;
const long long LINF = 1e18;
#define dump(x) cout << 'x' << ' = ' << (x) << ` `;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOREACH(x, a) for (auto &(x) : (a))
typedef long long ll;
using namespace std;
typedef pair<ll, ll> P;

template <typename T> void print(const vector<T> &x) {
  int n = x.size();
  rep(i, n) {
    cout << x[i];
    if (i != n - 1)
      cout << " ";
    else
      cout << endl;
  }
}

template <typename T> void print(const vector<vector<T>> &x) {
  int n = x.size();
  rep(i, n) {
    rep(j, x[i].size()) { cout << x[i][j] << " "; }
    cout << endl;
  }
}

template <typename T> void print(const vector<T> &x, int n) {
  rep(i, n) {
    cout << x[i];
    if (i != n - 1)
      cout << " ";
    else
      cout << endl;
  }
}

template <typename T> void print(const vector<vector<T>> &x, int n, int m) {
  rep(i, n) {
    rep(j, m) { cout << x[i][j] << " "; }
    cout << 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;
}

void input_init() {
  cin.tie(0);
  ios::sync_with_stdio(false);
}

ll dp[1001][1001];

int main(int argc, char const *argv[]) {
  input_init();
  rep(i, 1001) rep(j, 1001) dp[i][j] = LINF;
  int h, w;
  cin >> h >> w;
  int sy, sx, gy, gx;
  cin >> sy >> sx >> gy >> gx;
  --sy;
  --sx;
  --gy;
  --gx;
  vector<string> g(h);
  rep(i, h) cin >> g[i];
  deque<P> q;
  q.emplace_back(sy, sx);
  dp[sy][sx] = 0;
  while (!q.empty()) {
    P cur = q.front();
    q.pop_front();
    int cy = cur.first;
    int cx = cur.second;
    if (cy == gy && cx == gx) {
      continue;
    }
    for (int dy = -2; dy <= 2; ++dy) {
      for (int dx = -2; dx <= 2; ++dx) {
        if (dy == 0 && dx == 0)
          continue;
        int cost = 1;
        if ((dy == 1 && dx == 0) || (dy == 0 && dx == 1) ||
            (dy == -1 && dx == 0) || (dy == 0 && dx == -1)) {
          cost = 0;
        }
        int ny = cy + dy;
        int nx = cx + dx;
        if (ny < 0 || ny >= h || nx < 0 || nx >= w)
          continue;
        if (g[ny][nx] == '#')
          continue;
        if (chmin(dp[ny][nx], dp[cy][cx] + cost)) {
          if (cost)
            q.emplace_back(ny, nx);
          else
            q.emplace_front(ny, nx);
        }
      }
    }
  }

  if (dp[gy][gx] != LINF)
    cout << dp[gy][gx] << '\\n';
  else
    cout << -1 << '\\n';
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
const int INF = 1e9;
const int MOD = 1e9 + 7;
const long long LINF = 1e18;
#define dump(x) cout << 'x' << ' = ' << (x) << ` `;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOREACH(x, a) for (auto &(x) : (a))
typedef long long ll;
using namespace std;
typedef pair<ll, ll> P;

template <typename T> void print(const vector<T> &x) {
  int n = x.size();
  rep(i, n) {
    cout << x[i];
    if (i != n - 1)
      cout << " ";
    else
      cout << endl;
  }
}

template <typename T> void print(const vector<vector<T>> &x) {
  int n = x.size();
  rep(i, n) {
    rep(j, x[i].size()) { cout << x[i][j] << " "; }
    cout << endl;
  }
}

template <typename T> void print(const vector<T> &x, int n) {
  rep(i, n) {
    cout << x[i];
    if (i != n - 1)
      cout << " ";
    else
      cout << endl;
  }
}

template <typename T> void print(const vector<vector<T>> &x, int n, int m) {
  rep(i, n) {
    rep(j, m) { cout << x[i][j] << " "; }
    cout << 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;
}

void input_init() {
  cin.tie(0);
  ios::sync_with_stdio(false);
}

ll dp[1001][1001];

int main(int argc, char const *argv[]) {
  input_init();
  rep(i, 1001) rep(j, 1001) dp[i][j] = LINF;
  int h, w;
  cin >> h >> w;
  int sy, sx, gy, gx;
  cin >> sy >> sx >> gy >> gx;
  --sy;
  --sx;
  --gy;
  --gx;
  vector<string> g(h);
  rep(i, h) cin >> g[i];
  queue<P> q;
  q.emplace(sy, sx);
  dp[sy][sx] = 0;
  while (!q.empty()) {
    P cur = q.front();
    q.pop();
    int cy = cur.first;
    int cx = cur.second;
    if (cy == gy && cx == gx) {
      continue;
    }
    for (int dy = -2; dy <= 2; ++dy) {
      for (int dx = -2; dx <= 2; ++dx) {
        if (dy == 0 && dx == 0)
          continue;
        int cost = 1;
        if ((dy == 1 && dx == 0) || (dy == 0 && dx == 1) ||
            (dy == -1 && dx == 0) || (dy == 0 && dx == -1)) {
          cost = 0;
        }
        int ny = cy + dy;
        int nx = cx + dx;
        if (ny < 0 || ny >= h || nx < 0 || nx >= w)
          continue;
        if (g[ny][nx] == '#')
          continue;
        if (chmin(dp[ny][nx], dp[cy][cx] + cost)) {
          q.emplace(ny, nx);
        }
      }
    }
  }

  if (dp[gy][gx] != LINF)
    cout << dp[gy][gx] << '\\n';
  else
    cout << -1 << '\\n';
  return 0;
}