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

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

#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define lch (o << 1)
#define rch (o << 1 | 1)

typedef double db;
typedef long long ll;
typedef unsigned int ui;
typedef pair<int, int> pint;
typedef tuple<int, int, int> tint;

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

const int N = 1000 + 5;
const int INF = 0x3f3f3f3f;
const ll INF_LL = 0x3f3f3f3f3f3f3f3f;

char g[N][N];
int n, m;
pint st, ed;

bool OK(int x, int y) {
  if (x <= 0 || x > n || y <= 0 || y > m)
    return 0;
  else
    return 1;
}

int tms, idx[N][N];

void DFS(int x, int y, int c) {
  idx[x][y] = c;
  for (int d = 0; d < 4; d++) {
    int nx = x + dx[d];
    int ny = y + dy[d];
    if (OK(nx, ny) && g[nx][ny] == '.' && !idx[nx][ny])
      DFS(nx, ny, c);
  }
}

vector<int> a[N * N];
int dis[N * N];
bool inQ[N * N];

int main() {
  ios::sync_with_stdio(0);

  cin >> n >> m;
  cin >> st.fi >> st.se;
  cin >> ed.fi >> ed.se;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      cin >> g[i][j];

  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (!idx[i][j] && g[i][j] == '.')
        DFS(i, j, ++tms);

  for (int x = 1; x <= n; x++)
    for (int y = 1; y <= m; y++) {
      if (g[x][y] != '.')
        continue;
      for (int dx = -2; dx <= 2; dx++)
        for (int dy = -2; dy <= 2; dy++) {
          int nx = x + dx;
          int ny = y + dy;
          if (OK(nx, ny) && g[nx][ny] == '.' && idx[nx][ny] != idx[x][y]) {
            a[idx[nx][ny]].push_back(idx[x][y]);
            a[idx[x][y]].push_back(idx[nx][ny]);
          }
        }
    }

  queue<int> q;
  memset(dis, 0x3f, sizeof(dis));
  dis[idx[st.fi][st.se]] = 0;
  inQ[idx[st.fi][st.se]] = 1;
  q.push(idx[st.fi][st.se]);

  while (!q.empty()) {
    auto u = q.front();
    q.pop();
    for (auto v : a[u]) {
      if (dis[v] > dis[u] + 1) {
        dis[v] = dis[u] + 1;
        if (!inQ[v]) {
          inQ[v] = 1;
          q.push(v);
        }
      }
    }
    inQ[u] = 0;
  }

  if (dis[idx[ed.fi][ed.se]] == INF)
    cout << -1 << endl;
  else
    cout << dis[idx[ed.fi][ed.se]] << endl;

  return 0;
}


Error: TLE

Fixed submission:

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

#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define lch (o << 1)
#define rch (o << 1 | 1)

typedef double db;
typedef long long ll;
typedef unsigned int ui;
typedef pair<int, int> pint;
typedef tuple<int, int, int> tint;

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

const int N = 1000 + 5;
const int INF = 0x3f3f3f3f;
const ll INF_LL = 0x3f3f3f3f3f3f3f3f;

char g[N][N];
int n, m;
pint st, ed;

bool OK(int x, int y) {
  if (x <= 0 || x > n || y <= 0 || y > m)
    return 0;
  else
    return 1;
}

int dis[N][N];
bool inQ[N][N];
int tms, idx[N][N];

void DFS(int x, int y, int c) {
  idx[x][y] = c;
  for (int d = 0; d < 4; d++) {
    int nx = x + dx[d];
    int ny = y + dy[d];
    if (OK(nx, ny) && g[nx][ny] == '.' && !idx[nx][ny])
      DFS(nx, ny, c);
  }
}

int main() {
  ios::sync_with_stdio(0);

  cin >> n >> m;
  cin >> st.fi >> st.se;
  cin >> ed.fi >> ed.se;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      cin >> g[i][j];

  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (!idx[i][j])
        DFS(i, j, ++tms);

  queue<pint> q;
  memset(dis, 0x3f, sizeof(dis));
  dis[st.fi][st.se] = 0;
  inQ[st.fi][st.se] = 1;
  q.push(st);

  while (!q.empty()) {
    auto u = q.front();
    q.pop();
    int x = u.fi, y = u.se;

    for (int d = 0; d < 4; d++) {
      int nx = x + dx[d];
      int ny = y + dy[d];
      if (OK(nx, ny) && g[nx][ny] == '.') {
        if (dis[nx][ny] > dis[x][y]) {
          dis[nx][ny] = dis[x][y];
          if (!inQ[nx][ny]) {
            inQ[nx][ny] = 1;
            q.push(make_pair(nx, ny));
          }
        }
      }
    }

    for (int dx = -2; dx <= 2; dx++)
      for (int dy = -2; dy <= 2; dy++) {
        int nx = x + dx;
        int ny = y + dy;
        if (OK(nx, ny) && g[nx][ny] == '.' && idx[nx][ny] != idx[x][y]) {
          if (dis[nx][ny] > dis[x][y] + 1) {
            dis[nx][ny] = dis[x][y] + 1;
            if (!inQ[nx][ny]) {
              inQ[nx][ny] = 1;
              q.push(make_pair(nx, ny));
            }
          }
        }
      }

    inQ[x][y] = 0;
  }

  if (dis[ed.fi][ed.se] == INF)
    cout << -1 << endl;
  else
    cout << dis[ed.fi][ed.se] << endl;

  return 0;
}