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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

const int di[] = {-1, 0, 1, 0};
const int dj[] = {0, -1, 0, 1};

int main() {
  int h, w;
  cin >> h >> w;
  int si, sj;
  cin >> si >> sj;
  int ti, tj;
  cin >> ti >> tj;
  --si;
  --sj;
  --ti;
  --tj;
  vector<string> s(h);
  rep(i, h) cin >> s[i];
  const int INF = 1e9;
  vector<vector<int>> dist(h, vector<int>(w, INF));
  deque<P> q;
  dist[si][sj] = 0;
  q.emplace_back(si, sj);
  while (!q.empty()) {
    int i = q.front().first;
    int j = q.front().second;
    int d = dist[i][j];
    q.pop_front();
    if (dist[i][j] != d)
      continue;
    rep(v, 4) {
      int ni = i + di[v], nj = j + dj[v];
      if (ni < 0 || ni >= h || nj < 0 || nj >= w)
        continue;
      if (s[ni][nj] == '#')
        continue;
      if (dist[ni][nj] <= d)
        continue;
      dist[ni][nj] = d;
      q.emplace_front(ni, nj);
    }
    for (int ei = -2; ei <= 2; ++ei) {
      for (int ej = -2; ej <= 2; ++ej) {
        int ni = i + ei, nj = j + ej;
        if (ni < 0 || ni >= h || nj < 0 || nj >= w)
          continue;
        if (s[ni][nj] == '#')
          continue;
        if (dist[ni][nj] <= d + 1)
          continue;
        dist[ni][nj] = d + 1;
        q.emplace_back(ni, nj);
      }
    }
  }
  int ans = dist[ti][tj];
  if (ans == INF)
    ans = -1;
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define endl '\\n'
#define all(x) (x).begin(), (x).end()
const int INF = 1000000000 + 5;
const int N = 2e5 + 5;
const ll oo = 1e18 + 5;
const ll mod = 1e9 + 7;

int n, m;

const int dx[] = {-1, 1, 0, 0};
const int dy[] = {0, 0, -1, 1};

int main() {
  //~ ios::sync_with_stdio(0); cin.tie(0);
  int n, m;
  cin >> n >> m;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  --x1, --y1, --x2, --y2;
  vector<string> s(n);
  for (int i = 0; i < n; ++i)
    cin >> s[i];
  vector<vector<int>> score(n, vector<int>(m, INF));
  deque<pair<int, int>> dq;
  score[x1][y1] = 0;
  dq.emplace_back(x1, y1);
  while (!dq.empty()) {
    int x = dq.front().first;
    int y = dq.front().second;
    int dist = score[x][y];
    dq.pop_front();
    if (score[x][y] != dist)
      continue;
    for (int dir = 0; dir < 4; ++dir) {
      int nx = x + dx[dir];
      int ny = y + dy[dir];
      if (nx < 0 || nx >= n || ny < 0 || ny >= m)
        continue;
      if (s[nx][ny] == '#')
        continue;
      if (score[nx][ny] <= dist)
        continue;
      score[nx][ny] = dist;
      dq.emplace_back(nx, ny);
    }
    for (int i = -2; i <= 2; ++i) {
      for (int j = -2; j <= 2; ++j) {
        int nx = x + i;
        int ny = y + j;
        if (nx < 0 || nx >= n || ny < 0 || ny >= m)
          continue;
        if (s[nx][ny] == '#')
          continue;
        if (score[nx][ny] <= dist + 1)
          continue;
        score[nx][ny] = dist + 1;
        dq.emplace_back(nx, ny);
      }
    }
  }
  int ans = score[x2][y2];
  if (ans == INF)
    cout << -1;
  else
    cout << ans;
  return 0;
}