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

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define bug(x) cout << "zdongdebug1: " << x << endl;
#define bug2(x, y) cout << "zdongdebug2: " << x << " " << y << endl;
#define bug3(x, y, z)                                                          \\
  cout << "zdongdebug3: " << x << " " << y << " " << z << endl;
using namespace std;
typedef long long ll;
const int maxn = 1005;
const int mod = 1000000007;

int n, m;
string s[maxn];
int c[maxn][maxn];
int dir[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};

void dfs(int x, int y, int cc) {
  c[x][y] = cc;
  for (int i = 0; i < 4; i++) {
    int nx = x + dir[i][0];
    int ny = y + dir[i][1];
    if (nx < 0 || ny < 0 || nx >= n || ny >= m || s[nx][ny] == '#' ||
        c[nx][ny] != -1)
      continue;
    dfs(nx, ny, cc);
  }
}

unordered_set<int> st[maxn * maxn];
int d[maxn * maxn];
int main() {
#ifdef suiyuan2009
  freopen("/Users/suiyuan2009/CLionProjects/icpc/input.txt", "r", stdin);
  // freopen("/Users/suiyuan2009/CLionProjects/icpc/output.txt", "w", stdout);
#endif

  cin >> n >> m;
  int sx, sy, tx, ty;
  cin >> sx >> sy >> tx >> ty;
  sx--, sy--, tx--, ty--;
  for (int i = 0; i < n; i++)
    cin >> s[i];
  memset(c, -1, sizeof(c));
  int cur = 0;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) {
      if (s[i][j] == '#' || c[i][j] != -1)
        continue;
      dfs(i, j, cur++);
    }
  if (c[sx][sy] == c[tx][ty]) {
    cout << 0 << endl;
    return 0;
  }
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) {
      if (s[i][j] == '#')
        continue;
      for (int p = -2; p <= 2; p++)
        for (int q = -2; q <= 2; q++) {
          int x = i + p;
          int y = j + q;
          if (x < 0 || x >= n || y < 0 || y >= m || s[x][y] == '#' ||
              c[i][j] == c[x][y])
            continue;
          st[c[i][j]].insert(c[x][y]);
          st[c[x][y]].insert(c[i][j]);
        }
    }
  memset(d, -1, sizeof(d));
  d[c[sx][sy]] = 0;
  queue<int> q;
  q.push(c[sx][sy]);
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    for (auto y : st[x]) {
      if (d[y] == -1 || d[y] > d[x] + 1) {
        d[y] = d[x] + 1;
        q.push(y);
      }
    }
  }
  cout << d[c[tx][ty]] << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define bug(x) cout << "zdongdebug1: " << x << endl;
#define bug2(x, y) cout << "zdongdebug2: " << x << " " << y << endl;
#define bug3(x, y, z)                                                          \\
  cout << "zdongdebug3: " << x << " " << y << " " << z << endl;
using namespace std;
typedef long long ll;
const int maxn = 1005;
const int mod = 1000000007;

string s[maxn];
bool in[maxn][maxn];
int f[maxn][maxn];

int d[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
int main() {
#ifdef suiyuan2009
  freopen("/Users/suiyuan2009/CLionProjects/icpc/input.txt", "r", stdin);
  // freopen("/Users/suiyuan2009/CLionProjects/icpc/output.txt", "w", stdout);
#endif
  int n, m;
  cin >> n >> m;
  int sx, sy, tx, ty;
  cin >> sx >> sy >> tx >> ty;
  sx--, sy--, tx--, ty--;
  for (int i = 0; i < n; i++)
    cin >> s[i];
  memset(f, -1, sizeof(f));
  queue<int> qx, qy;
  qx.push(sx);
  qy.push(sy);
  in[sx][sy] = 1;
  f[sx][sy] = 0;
  while (!qx.empty()) {
    int x = qx.front();
    int y = qy.front();
    qx.pop();
    qy.pop();
    in[x][y] = 0;
    for (int i = 0; i < 4; i++) {
      int nx = x + d[i][0];
      int ny = y + d[i][1];
      if (nx < 0 || ny < 0 || nx >= n || ny >= m || s[nx][ny] == '#')
        continue;
      int add = 0;
      int tt = f[nx][ny];
      if (tt == -1 || tt > f[x][y] + add) {
        f[nx][ny] = f[x][y] + add;
        if (!in[nx][ny]) {
          qx.push(nx);
          qy.push(ny);
          in[nx][ny] = 1;
        }
      }
    }
    for (int i = -2; i <= 2; i++)
      for (int j = -2; j <= 2; j++) {
        if (i == 0 && j == 0)
          continue;
        if (abs(i) + abs(j) == 1)
          continue;
        int nx = x + i;
        int ny = y + j;
        if (nx < 0 || ny < 0 || nx >= n || ny >= m || s[nx][ny] == '#')
          continue;
        int add = 1;
        int tt = f[nx][ny];
        if (tt == -1 || tt > f[x][y] + add) {
          f[nx][ny] = f[x][y] + add;
          if (!in[nx][ny]) {
            qx.push(nx);
            qy.push(ny);
            in[nx][ny] = 1;
          }
        }
      }
  }
  cout << f[tx][ty] << endl;
  return 0;
}