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

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

int _;

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

int n, m, sx, sy, gx, gy, iidx[maxn * maxn], cnt, sum[maxn * maxn];
bool vis[1000050];
vector<int> idx[maxn * maxn];
string s[maxn];

void dfs(int x, int y) {
  if (x < 0 || y < 0 || x >= n || y >= m || s[x][y] != '.' ||
      iidx[x * maxn + y])
    return;
  idx[cnt].push_back(x * maxn + y);
  iidx[x * maxn + y] = cnt;
  for (int i = 0; i < 4; i++)
    dfs(x + dx[i], y + dy[i]);
}

inline bool bfs() {
  queue<int> q;
  sum[sx * maxn + sy] = 1;
  for (auto v : idx[iidx[sx * maxn + sy]]) {
    sum[v] = sum[sx * maxn + sy];
    q.push(v);
  }
  vis[iidx[sx * maxn + sy]] = true;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    if (u == gx * maxn + gy)
      return true;
    for (int i = -2; i <= 2; i++)
      for (int j = -2; j <= 2; j++)
        if (i || j) {
          int x = u / maxn + i, y = u % maxn + j;
          if (x >= 0 && y >= 0 && x < n && y < m && s[x][y] == '.' &&
              !sum[x * maxn + y] && !vis[iidx[x * maxn + y]]) {
            vis[iidx[x * maxn + y]] = true;
            for (auto v : idx[iidx[x * maxn + y]]) {
              sum[v] = sum[u] + 1;
              q.push(v);
            }
          }
        }
  }
  return false;
}

int main() {
  // for(scanf("%d",&_);_;_--)
  scanf("%d%d%d%d%d%d", &n, &m, &sx, &sy, &gx, &gy);
  sx--;
  sy--;
  gx--;
  gy--;
  for (int i = 0; i < n; i++)
    cin >> s[i];
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (!iidx[i * maxn + j] && s[i][j] == '.') {
        cnt++;
        dfs(i, j);
      }
  if (bfs())
    printf("%d\\n", sum[gx * maxn + gy] - 1);
  else
    printf("-1\\n");
  return 0;
}


Error: TLE

Fixed submission:

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

int _;

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

int n, m, sx, sy, gx, gy, idx[maxn * maxn], cnt, sum[maxn * maxn];
bool vis[1000050];
string s[maxn];

void dfs(int x, int y) {
  if (x < 0 || y < 0 || x >= n || y >= m || s[x][y] != '.' || idx[x * maxn + y])
    return;
  idx[x * maxn + y] = cnt;
  for (int i = 0; i < 4; i++)
    dfs(x + dx[i], y + dy[i]);
}

inline bool bfs() {
  queue<int> q;
  sum[sx * maxn + sy] = 1;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (idx[i * maxn + j] == idx[sx * maxn + sy]) {
        sum[i * maxn + j] = sum[sx * maxn + sy];
        q.push(i * maxn + j);
      }
  vis[idx[sx * maxn + sy]] = true;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    if (u == gx * maxn + gy)
      return true;
    for (int i = -2; i <= 2; i++)
      for (int j = -2; j <= 2; j++)
        if (i || j) {
          int x = u / maxn + i, y = u % maxn + j;
          if (x >= 0 && y >= 0 && x < n && y < m && s[x][y] == '.' &&
              !sum[x * maxn + y] && !vis[idx[x * maxn + y]]) {
            vis[idx[x * maxn + y]] = true;
            for (int ii = 0; ii < n; ii++)
              for (int jj = 0; jj < m; jj++)
                if (idx[ii * maxn + jj] == idx[x * maxn + y]) {
                  sum[ii * maxn + jj] = sum[u] + 1;
                  q.push(ii * maxn + jj);
                }
          }
        }
  }
  return false;
}

int main() {
  // for(scanf("%d",&_);_;_--)
  scanf("%d%d%d%d%d%d", &n, &m, &sx, &sy, &gx, &gy);
  sx--;
  sy--;
  gx--;
  gy--;
  for (int i = 0; i < n; i++)
    cin >> s[i];
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (!idx[i * maxn + j] && s[i][j] == '.') {
        cnt++;
        dfs(i, j);
      }
  if (bfs())
    printf("%d\\n", sum[gx * maxn + gy] - 1);
  else
    printf("-1\\n");
  return 0;
}