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

#include "bits/stdc++.h"
using namespace std;

#define mp make_pair
typedef pair<int, int> pii;
int h, w, cx, cy, dx, dy;
const int N = 1001;
bool vis[N][N];
int depth[N][N];

int main() {
  cin.tie(0);

  cin >> h >> w >> cx >> cy >> dx >> dy;

  string s[h];

  for (int i = 0; i < h; i++)
    cin >> s[i];
  cx--;
  cy--;
  dx--;
  dy--;
  deque<pii> q;
  q.push_back(mp(cx, cy));

  for (int i = 0; i < N; i++)
    for (int j = 0; j < N; j++)
      depth[i][j] = 1e8;

  depth[cx][cy] = 0;
  vis[cx][cy] = true;

  while (!q.empty()) {
    auto N = q.front();
    q.pop_front();
    int x = N.first, y = N.second;
    if (x == dx && y == dy) {
      cout << depth[x][y] << endl;
      return 0;
    }

    for (int i = -2; i < 3; i++)
      for (int j = -2; j < 3; j++) {
        int nx = x + i, ny = y + j;
        if (nx >= 0 && nx < h && ny >= 0 && ny < w && s[nx][ny] == '.' &&
            !vis[nx][ny]) {

          if (abs(i) + abs(j) < 2) {
            vis[nx][ny] = true;
            depth[nx][ny] = depth[x][y];
            q.push_front(mp(nx, ny));
          }

          if (depth[nx][ny] > depth[x][y] + 1) {
            depth[nx][ny] = depth[x][y] + 1;
            q.push_back(mp(nx, ny));
          }
        }
      }
  }
  cout << -1 << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define FOR(i, n, j) for (int i = (j); i < (n); ++i)
#define ssort(n) sort((n).begin(), (n).end())
#define rsort(n) sort((n).begin(), (n).end(), greater<int>())
using ll = long long;
using ld = long double;
typedef pair<int, int> P;
typedef pair<P, int> COST;
#define repl(i, n) for (ll i = 0; i < (n); ++i)
#define Yes cout << "Yes" << endl
#define No cout << "No" << endl
#define YES cout << "YES" << endl
#define NO cout << "NO" << endl
using Graf_char = vector<vector<char>>;
using Graf_int = vector<vector<int>>;
#define MAX 10000000

int main() {
  int h, w;
  cin >> h >> w;
  int c_h, c_w, d_h, d_w;
  cin >> c_h >> c_w;
  cin >> d_h >> d_w;
  c_h--;
  c_w--;
  d_h--;
  d_w--;
  Graf_char s(h, vector<char>(w));
  Graf_int dist(h, vector<int>(w));
  rep(i, h) {
    rep(j, w) {
      cin >> s[i][j];
      dist[i][j] = MAX;
    }
  }
  queue<COST> q;
  COST p, temp;
  p.first.first = c_h;
  p.first.second = c_w;
  p.second = 0;
  q.push(p);
  while (!q.empty()) {
    p = q.front();

    if (dist[p.first.first][p.first.second] > p.second) {
      dist[p.first.first][p.first.second] = p.second;
      rep(i, 5) {
        rep(j, 5) {
          int y_, x_;
          if (!(i == 2 && j == 2)) {
            y_ = p.first.first - 2 + i;
            x_ = p.first.second - 2 + j;
            if ((abs(i - 2) < 2 && j == 2) || (abs(j - 2) < 2 && i == 2)) {
              temp.second = p.second;
            } else {
              temp.second = p.second + 1;
            }
            temp.first.first = y_;
            temp.first.second = x_;
            if (y_ >= 0 && y_ < h && x_ >= 0 && x_ < w) {
              if (s[y_][x_] == '.' && dist[y_][x_] > temp.second) {
                q.push(temp);
              }
            }
          }
        }
      }
    }
    q.pop();
  }
  if (dist[d_h][d_w] != MAX)
    cout << dist[d_h][d_w] << endl;
  else
    cout << -1 << endl;
}