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

#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;

const int MAX = 1e3 + 1;

string s[MAX];
pii p[MAX][MAX];
set<pii> adj[MAX][MAX];
int m[MAX][MAX];
int d[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
int h, w, cx, cy, dx, dy;

void build() {
  cin >> h >> w >> cx >> cy >> dx >> dy;
  for (int i = 0; i < h; i++)
    cin >> s[i];

  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      p[i][j] = {-1, -1};
}

void dfs(int sx, int sy) {
  queue<pair<int, int>> q;
  q.push({sx, sy});
  p[sx][sy] = {sx, sy};
  while (!q.empty()) {
    int nx = q.front().first, ny = q.front().second;
    q.pop();

    for (int i = 0; i < 4; i++) {
      int x = nx + d[i][0], y = ny + d[i][1];
      if (x >= 0 && x < h && y >= 0 && y < w && p[x][y].first == -1 &&
          s[x][y] != '#')
        p[x][y] = {sx, sy}, q.push({x, y});
    }
  }
}

void solve() {
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      if (s[i][j] == '.' && p[i][j].first == -1)
        dfs(i, j);

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (s[i][j] == '#')
        continue;

      for (int ii = -2; ii < 3; ii++) {
        for (int jj = -2; jj < 3; jj++) {
          if (i + ii >= 0 && i + ii < h && j + jj >= 0 && j + jj < w &&
              s[i + ii][j + jj] != '#' && p[i][j] != p[i + ii][j + jj]) {
            adj[p[i][j].first][p[i][j].second].insert(p[i + ii][j + jj]);
            adj[p[i + ii][j + jj].first][p[i + ii][j + jj].second].insert(
                p[i][j]);
          }
        }
      }
    }
  }

  memset(m, -1, sizeof(m));

  queue<pii> q;
  q.push(p[cx - 1][cy - 1]);
  m[p[cx - 1][cy - 1].first][p[cx - 1][cy - 1].second] = 0;

  while (!q.empty()) {
    pii now = q.front();
    q.pop();

    for (pii next : adj[now.first][now.second]) {
      if (m[next.first][next.second] == -1) {
        m[next.first][next.second] = m[now.first][now.second] + 1;
        q.push(next);
      }
    }
  }

  printf("%d", m[p[dx - 1][dy - 1].first][p[dx - 1][dy - 1].second]);
}

int main() {
  cin.sync_with_stdio(0);
  build();
  solve();
  return 0;
}

Error: TLE

Fixed submission:

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

const int MAX = 1e3 + 1;

string s[MAX];
int dp[MAX][MAX];
int h, w, cx, cy, dx, dy;

void build() {
  cin >> h >> w >> cx >> cy >> dx >> dy;
  for (int i = 0; i < h; i++)
    cin >> s[i];
}

void process(int x, int y, int m) {
  if (dp[x][y] != -1 && dp[x][y] <= m)
    return;
  dp[x][y] = m;

  if (x + 1 < h && s[x + 1][y] != '#')
    process(x + 1, y, m);
  if (y + 1 < w && s[x][y + 1] != '#')
    process(x, y + 1, m);
  if (y - 1 >= 0 && s[x][y - 1] != '#')
    process(x, y - 1, m);
  if (x - 1 >= 0 && s[x - 1][y] != '#')
    process(x - 1, y, m);

  for (int i = -2; i < 3; i++)
    for (int j = -2; j < 3; j++)
      if (x + i >= 0 && x + i < h && y + j >= 0 && y + j < w &&
          s[x + i][y + j] != '#')
        process(x + i, y + j, m + 1);
}

void solve() {
  memset(dp, -1, sizeof(dp));
  process(cx - 1, cy - 1, 0);
  printf("%d", dp[dx - 1][dy - 1]);
}

int main() {
  cin.sync_with_stdio(0);
  build();
  solve();
  return 0;
}