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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
const double PI = 4 * atan(1);
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
const double EPS = 1e-9;
const string YES = "Possible";
const string NO = "Impossible";
#define pb push_back
#define mp make_pair
#define eb emplace_back

const int MAX_N = 1002;
int N, M;
int S, T, X, Y;
string G[MAX_N];
const int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1};
int vis[MAX_N][MAX_N];
vector<pair<int, int>> prv, nxt;
int a;
map<int, vector<pair<int, int>>> order;

void dfs(int x, int y) {
  if (vis[X][Y] != -1)
    return;
  vis[x][y] = a;
  order[a].eb(x, y);
  for (int i = 0; i < 4; ++i) {
    const int nx = x + dx[i];
    const int ny = y + dy[i];
    if (nx >= 0 && nx < N && ny >= 0 && ny < M && vis[nx][ny] == -1 &&
        G[nx][ny] != '#') {
      dfs(nx, ny);
    }
  }
}

void solve() {
  S--, T--, X--, Y--;
  a = 0;
  memset(vis, -1, sizeof(vis));
  dfs(S, T);
  if (vis[X][Y] == 0) {
    cout << 0;
    return;
  }
  for (;;) {
    a++;
    bool update = false;
    for (const auto &p : order[a - 1]) {
      const int x = p.first, y = p.second;
      for (int r = -2; r <= 2; ++r)
        for (int c = -2; c <= 2; ++c) {
          const int nx = x + r, ny = y + c;
          if (nx >= 0 && nx < N && ny >= 0 && ny < M && vis[nx][ny] == -1 &&
              G[nx][ny] != '#') {
            update = true;
            dfs(nx, ny);
            if (vis[X][Y] != -1)
              goto out;
          }
        }
    }
    if (!update || vis[X][Y] != -1)
      break;
  }
out:
  if (vis[X][Y] == -1)
    cout << -1;
  else
    cout << a;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> N >> M;
  cin >> S >> T;
  cin >> X >> Y;
  for (int i = 0; i < N; ++i)
    cin >> G[i];
  solve();
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
const double PI = 4 * atan(1);
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
const double EPS = 1e-9;
const string YES = "Possible";
const string NO = "Impossible";
#define pb push_back
#define mp make_pair
#define eb emplace_back

const int MAX_N = 1002;
int N, M;
int S, T, X, Y;
string G[MAX_N];
const int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1};
int vis[MAX_N][MAX_N];
vector<pair<int, int>> prv, nxt;
int a;

void dfs(int x, int y) {
  if (vis[X][Y] != -1)
    return;
  vis[x][y] = a;
  for (int i = 0; i < 4; ++i) {
    const int nx = x + dx[i];
    const int ny = y + dy[i];
    if (nx >= 0 && nx < N && ny >= 0 && ny < M && vis[nx][ny] == -1 &&
        G[nx][ny] != '#') {
      dfs(nx, ny);
    }
  }
}

void solve() {
  S--, T--, X--, Y--;
  a = 0;
  memset(vis, -1, sizeof(vis));
  dfs(S, T);
  if (vis[X][Y] == 0) {
    cout << 0;
    return;
  }
  for (;;) {
    a++;
    bool update = false;
    for (int x = 0; x < N; ++x)
      for (int y = 0; y < M; ++y)
        if (vis[x][y] == a - 1) {
          for (int r = -2; r <= 2; ++r)
            for (int c = -2; c <= 2; ++c) {
              const int nx = x + r, ny = y + c;
              if (nx >= 0 && nx < N && ny >= 0 && ny < M && vis[nx][ny] == -1 &&
                  G[nx][ny] != '#') {
                update = true;
                dfs(nx, ny);
                if (vis[X][Y] != -1)
                  goto out;
              }
            }
        }
    if (!update || vis[X][Y] != -1)
      break;
  }
out:
  if (vis[X][Y] == -1)
    cout << -1;
  else
    cout << a;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> N >> M;
  cin >> S >> T;
  cin >> X >> Y;
  for (int i = 0; i < N; ++i)
    cin >> G[i];
  solve();
  return 0;
}