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

#include <bits/stdc++.h>
using namespace std;
// #define int long long
#define M 1000000007
#define INF ((1LL << 62LL) - 1)

const int N = 1005;
int n, m, sx, sy, ex, ey;
int conn = 0;
int total;

char a[N][N];
bool vis[N][N];
int number[N][N];

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

void dfs(int x, int y) {
  vis[x][y] = true;
  number[x][y] = conn;

  for (int i = 0; i < 4; i++) {
    int nx = x + dx[i];
    int ny = y + dy[i];
    if (!vis[nx][ny] && nx >= 1 && nx <= n && ny >= 1 && ny <= m &&
        a[nx][ny] == '.') {
      dfs(nx, ny);
    }
  }
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  cin >> n >> m >> sx >> sy >> ex >> ey;

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> a[i][j];
    }
  }

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (!vis[i][j] && a[i][j] == '.') {
        conn++;
        dfs(i, j);
      }
    }
  }

  total = conn;

  set<int> g[total + 1];

  if (number[sx][sy] == number[ex][ey]) {
    cout << "0\\n";
    return 0;
  } else {
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= m; j++) {
        if (a[i][j] == '.') {
          for (int ii = -2; ii <= 2; ii++) {
            for (int jj = -2; jj <= 2; jj++) {
              int nx = i + ii;
              int ny = j + jj;
              if (nx >= 1 && nx <= n && ny >= 1 && ny <= m &&
                  a[nx][ny] == '.' && number[nx][ny] != number[i][j]) {
                g[number[i][j]].insert(number[nx][ny]);
                g[number[nx][ny]].insert(number[i][j]);
              }
            }
          }
        }
      }
    }
  }

  int start = number[sx][sy];
  int end = number[ex][ey];

  queue<pair<int, int>> q;
  vector<bool> visi(total + 1, false);

  q.push({start, 0});
  visi[start] = true;

  while (!q.empty()) {
    pair<int, int> p = q.front();
    q.pop();

    for (int x : g[p.first]) {
      if (!visi[x]) {
        if (x == end) {
          cout << p.second + 1 << "\\n";
          return 0;
        }
        q.push({x, p.second + 1});
        visi[x] = true;
      }
    }
  }

  cout << "-1\\n";
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define M 1000000007
#define INF ((1LL << 62LL) - 1)

const int N = 1005;
int n, m, sx, sy, ex, ey;
int conn = 0;
int total;

char a[N][N];
bool vis[N][N];
int number[N][N];
set<int> g[N * N + 5];

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

void dfs(int x, int y) {
  vis[x][y] = true;
  number[x][y] = conn;

  for (int i = 0; i < 4; i++) {
    int nx = x + dx[i];
    int ny = y + dy[i];
    if (!vis[nx][ny] && nx >= 1 && nx <= n && ny >= 1 && ny <= m &&
        a[nx][ny] == '.') {
      dfs(nx, ny);
    }
  }
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  cin >> n >> m >> sx >> sy >> ex >> ey;

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> a[i][j];
    }
  }

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (!vis[i][j] && a[i][j] == '.') {
        conn++;
        dfs(i, j);
      }
    }
  }

  total = conn;

  if (number[sx][sy] == number[ex][ey]) {
    cout << "0\\n";
    return 0;
  } else {
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= m; j++) {
        if (a[i][j] == '.') {
          for (int ii = -2; ii <= 2; ii++) {
            for (int jj = -2; jj <= 2; jj++) {
              int nx = i + ii;
              int ny = j + jj;
              if (nx >= 1 && nx <= n && ny >= 1 && ny <= m &&
                  a[nx][ny] == '.' && number[nx][ny] != number[i][j]) {
                g[number[i][j]].insert(number[nx][ny]);
                g[number[nx][ny]].insert(number[i][j]);
              }
            }
          }
        }
      }
    }
  }

  int start = number[sx][sy];
  int end = number[ex][ey];

  queue<pair<int, int>> q;
  vector<bool> visi(N * N + 5, false);

  q.push({start, 0});
  visi[start] = true;

  while (!q.empty()) {
    pair<int, int> p = q.front();
    q.pop();

    for (int x : g[p.first]) {
      if (!visi[x]) {
        if (x == end) {
          cout << p.second + 1 << "\\n";
          return 0;
        }
        q.push({x, p.second + 1});
      }
    }
  }

  cout << "-1\\n";
}