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

#include <bits/stdc++.h>
#define ll long long
#define ls id << 1
#define rs id << 1 | 1
#define mem(array, value, size, type)                                          \\
  memset(array, value, ((size) + 5) * sizeof(type))
#define memarray(array, value) memset(array, value, sizeof(array))
#define fillarray(array, value, begin, end)                                    \\
  fill((array) + (begin), (array) + (end) + 1, value)
#define fillvector(v, value) fill((v).begin(), (v).end(), value)
#define pb(x) push_back(x)
#define st(x) (1LL << (x))
#define pii pair<int, int>
#define mp(a, b) make_pair((a), (b))
#define Flush fflush(stdout)
#define vecfirst (*vec.begin())
#define veclast (*vec.rbegin())
#define vecall(v) (v).begin(), (v).end()
#define vecupsort(v) (sort((v).begin(), (v).end()))
#define vecdownsort(v, type) (sort(vecall(v), greater<type>()))
#define veccmpsort(v, cmp) (sort(vecall(v), cmp))
using namespace std;
const int N = 1050;
const int inf = 0x3f3f3f3f;
const ll llinf = 0x3f3f3f3f3f3f3f3f;
const int mod = 998244353;
const int MOD = 1e9 + 7;
const double PI = acos(-1.0);
clock_t TIME__START, TIME__END;
void program_end() {
#ifdef ONLINE
  printf("\\nTime used: %.6lf(s)\\n", ((double)TIME__END - TIME__START) / 1000);
  system("pause");
#endif
}
int h, w;
char s[N][N];
int sx, sy, tx, ty;
int ans = inf;
int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};
int f[N * N];
int Find(int x) { return x == f[x] ? x : f[x] = Find(f[x]); }
int id(int x, int y) { return (x - 1) * w + y; }
vector<int> e[N * N];

void bfs() {
  for (int i = 1; i <= h; ++i)
    for (int j = 1; j <= w; ++j) {
      for (int k = 0; k < 4; ++k) {
        int nx = i + dx[k], ny = j + dy[k];
        if (nx < 1 || ny < 1 || nx > h || ny > w || s[nx][ny] == '#' ||
            s[i][j] == '#')
          continue;
        int fx = Find(id(i, j)), fy = Find(id(nx, ny));
        if (fx != fy)
          f[fy] = fx;
      }
    }
  for (int i = 1; i <= h; ++i)
    for (int j = 1; j <= w; ++j) {
      for (int nx = max(1, i - 2); nx <= min(h, i + 2); ++nx)
        for (int ny = max(1, j - 2); ny <= min(w, j + 2); ++ny) {
          if (nx < 1 || ny < 1 || nx > h || ny > w || s[nx][ny] == '#' ||
              s[i][j] == '#')
            continue;
          int fx = Find(id(i, j)), fy = Find(id(nx, ny));
          if (fx != fy)
            e[fy].push_back(fx), e[fx].push_back(fy);
        }
    }
  for (int i = 1; i <= h * w; ++i)
    e[i].resize(unique(e[i].begin(), e[i].end()) - e[i].begin());
}
int dis[N * N];
bool vis[N * N];
void spfa() {
  memarray(dis, inf);
  dis[Find(id(sx, sy))] = 0;
  queue<int> q;
  q.push(Find(id(sx, sy)));
  vis[Find(id(sx, sy))] = 1;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (auto v : e[u]) {
      if (vis[v])
        continue;
      vis[v] = 1;
      dis[v] = dis[u] + 1;
      q.push(v);
    }
  }
  ans = dis[Find(id(tx, ty))];
  if (ans == inf)
    ans = -1;
}

inline void solve() {
  cin >> h >> w;
  cin >> sx >> sy >> tx >> ty;
  for (int i = 1; i <= h; ++i)
    scanf("%s", s[i] + 1);
  for (int i = 1; i <= h; ++i)
    for (int j = 1; j <= w; ++j)
      f[id(i, j)] = id(i, j);
  bfs();
  spfa();
  cout << ans << '\\n';
}

int main() {
  TIME__START = clock();
  int Test = 1;
  // scanf("%d", &Test);
  while (Test--)
    solve();
  TIME__END = clock();
  program_end();
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define ls id << 1
#define rs id << 1 | 1
#define mem(array, value, size, type)                                          \\
  memset(array, value, ((size) + 5) * sizeof(type))
#define memarray(array, value) memset(array, value, sizeof(array))
#define fillarray(array, value, begin, end)                                    \\
  fill((array) + (begin), (array) + (end) + 1, value)
#define fillvector(v, value) fill((v).begin(), (v).end(), value)
#define pb(x) push_back(x)
#define st(x) (1LL << (x))
#define pii pair<int, int>
#define mp(a, b) make_pair((a), (b))
#define Flush fflush(stdout)
#define vecfirst (*vec.begin())
#define veclast (*vec.rbegin())
#define vecall(v) (v).begin(), (v).end()
#define vecupsort(v) (sort((v).begin(), (v).end()))
#define vecdownsort(v, type) (sort(vecall(v), greater<type>()))
#define veccmpsort(v, cmp) (sort(vecall(v), cmp))
using namespace std;
const int N = 1050;
const int inf = 0x3f3f3f3f;
const ll llinf = 0x3f3f3f3f3f3f3f3f;
const int mod = 998244353;
const int MOD = 1e9 + 7;
const double PI = acos(-1.0);
clock_t TIME__START, TIME__END;
void program_end() {
#ifdef ONLINE
  printf("\\nTime used: %.6lf(s)\\n", ((double)TIME__END - TIME__START) / 1000);
  system("pause");
#endif
}
int h, w;
char s[N][N];
int sx, sy, tx, ty;
int ans = inf;
int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};
struct state {
  int x, y;
  int cost;
  state() {}
  state(int _x, int _y, int _cost) : x(_x), y(_y), cost(_cost) {}
};
queue<state> q;
int dis[N][N];
bool vis[N][N];
void bfs() {
  memarray(dis, inf);
  dis[sx][sy] = 0;
  while (!q.empty()) {
    state u = q.front();
    q.pop();
    vis[u.x][u.y] = 0;
    for (int k = 0; k < 4; ++k) {
      int nx = u.x + dx[k], ny = u.y + dy[k];
      if (nx < 1 || ny < 1 || nx > h || ny > w || s[nx][ny] == '#')
        continue;
      if (dis[nx][ny] > dis[u.x][u.y]) {
        dis[nx][ny] = dis[u.x][u.y];
        if (!vis[nx][ny])
          q.push(state(nx, ny, dis[nx][ny])), vis[nx][ny] = 1;
      }
    }
    for (int i = max(1, u.x - 2); i <= min(h, u.x + 2); ++i)
      for (int j = max(1, u.y - 2); j <= min(w, u.y + 2); ++j) {
        int nx = i, ny = j;
        if (i == u.x && j == u.y)
          continue;
        if (s[i][j] == '#')
          continue;
        if (dis[i][j] > dis[u.x][u.y] + 1) {
          dis[nx][ny] = dis[u.x][u.y] + 1;
          if (!vis[nx][ny])
            q.push(state(nx, ny, dis[nx][ny] + 1)), vis[nx][ny] = 1;
        }
      }
  }
  ans = min(ans, dis[tx][ty]);
  if (ans == inf)
    ans = -1;
}

inline void solve() {
  cin >> h >> w;
  cin >> sx >> sy >> tx >> ty;
  for (int i = 1; i <= h; ++i)
    scanf("%s", s[i] + 1);
  q.push(state(sx, sy, 0));
  bfs();
  cout << ans << '\\n';
}

int main() {
  TIME__START = clock();
  int Test = 1;
  // scanf("%d", &Test);
  while (Test--)
    solve();
  TIME__END = clock();
  program_end();
  return 0;
}