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

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

#define fi first
#define se second
#define pb push_back
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define FOR(i, a, b) for (int i = a; i <= (b); i++)
#define ROF(i, a, b) for (int i = a; i >= (b); i--)

using pii = pair<int, int>;
using vpii = vector<pii>;
using vi = vector<int>;
using vvi = vector<vi>;
using ll = long long;
using pll = pair<ll, ll>;
using vpll = vector<pll>;
using vll = vector<ll>;
using vvll = vector<vll>;

const int N = 1005, INF = 0x3f3f3f3f;
char c[N][N];
pii ex[4] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};

int main() {
  ios::sync_with_stdio(0), cin.tie(0);
  int h, w;
  cin >> h >> w;
  int ch, cw;
  cin >> ch >> cw;
  int dh, dw;
  cin >> dh >> dw;
  vvi d(h + 1, vi(w + 1, INF));
  FOR(i, 1, h) FOR(j, 1, w) { cin >> c[i][j]; }
  deque<pii> dq;
  dq.push_back({ch, cw});
  d[ch][cw] = 0;
  while (!dq.empty()) {
    pii u = dq.front();
    dq.pop_front();
    int x = u.fi, y = u.se, t = d[x][y];
    if (t != d[x][y])
      continue;
    FOR(i, -2, 2) FOR(j, -2, 2) {
      int tx = x + i, ty = y + j;
      if (tx < 1 || tx > h || ty < 1 || ty > w || c[tx][ty] == '#')
        continue;
      if (abs(i) + abs(j) == 1 && t < d[tx][ty]) {
        d[tx][ty] = t;
        dq.push_front({tx, ty});
      }
      if (t + 1 < d[tx][ty]) {
        d[tx][ty] = t + 1;
        dq.push_back({tx, ty});
      }
    }
  }
  if (d[dh][dw] == INF)
    cout << -1 << endl;
  else
    cout << d[dh][dw] << endl;
}

Error: TLE

Fixed submission:

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

#define fi first
#define se second
#define pb push_back
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define FOR(i, a, b) for (int i = a; i <= (b); i++)
#define ROF(i, a, b) for (int i = a; i >= (b); i--)

using pii = pair<int, int>;
using vpii = vector<pii>;
using vi = vector<int>;
using vvi = vector<vi>;
using ll = long long;
using pll = pair<ll, ll>;
using vpll = vector<pll>;
using vll = vector<ll>;
using vvll = vector<vll>;

const int N = 1005, INF = 0x3f3f3f3f;
char c[N][N];
pii ex[4] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};

int main() {
  ios::sync_with_stdio(0), cin.tie(0);
  int h, w;
  cin >> h >> w;
  int ch, cw;
  cin >> ch >> cw;
  int dh, dw;
  cin >> dh >> dw;
  vvi d(h + 1, vi(w + 1, INF));
  FOR(i, 1, h) FOR(j, 1, w) { cin >> c[i][j]; }
  deque<pii> dq;
  dq.push_back({ch, cw});
  d[ch][cw] = 0;
  while (!dq.empty()) {
    pii u = dq.front();
    dq.pop_front();
    int x = u.fi, y = u.se, t = d[x][y];
    if (t != d[x][y])
      continue;
    FOR(i, 0, 3) {
      int tx = x + ex[i].fi, ty = y + ex[i].se;
      if (tx < 1 || tx > h || ty < 1 || ty > w)
        continue;
      if (c[tx][ty] == '.' && t < d[tx][ty]) {
        d[tx][ty] = t;
        dq.push_back({tx, ty});
      }
    }
    FOR(i, -2, 2) FOR(j, -2, 2) {
      int tx = x + i, ty = y + j;
      if (tx < 1 || tx > h || ty < 1 || ty > w)
        continue;
      if (c[tx][ty] == '.' && t + 1 < d[tx][ty]) {
        d[tx][ty] = t + 1;
        dq.push_back({tx, ty});
      }
    }
  }
  if (d[dh][dw] == INF)
    cout << -1 << endl;
  else
    cout << d[dh][dw] << endl;
}