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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
const int maxn = 2e5 + 10;
const int mod = 1e9 + 7;
char s[1005][1005];
int vis[1005][1005];
int cnt[1005][1005];
int h, w;
int sx, sy, ex, ey;
int xx[5] = {58, 0, 0, 1, -1};
int yy[5] = {58, 1, -1, 0, 0};
int min1 = 1e9;

struct ac {
  int x, y, step, num;
};

void bfs() {
  deque<ac> q;
  q.push_front({sx, sy, 0, cnt[sx][sy]});
  s[sx][sy] = '#';
  while (!q.empty()) {
    ac u = q.front();
    q.pop_front();
    if (u.num == cnt[ex][ey]) {
      min1 = u.step;
      return;
    }
    int x = u.x, y = u.y;
    s[x][y] = '#';
    for (int i = x - 2; i <= x + 2; i++) {
      for (int j = y - 2; j <= y + 2; j++) {
        if (i < 1 || j < 1 || i > h || j > w || s[i][j] == '#')
          continue;
        if (u.num == cnt[i][j])
          q.push_front({i, j, u.step, cnt[i][j]});
        else
          q.push_back({i, j, u.step + 1, cnt[i][j]});
      }
    }
  }
}

void dfs222(int x, int y, int num) {
  cnt[x][y] = num;
  vis[x][y] = 1;
  for (int i = 1; i <= 4; i++) {
    int tx = xx[i] + x;
    int ty = yy[i] + y;
    if (tx < 1 || ty < 1 || tx > h || ty > w || vis[tx][ty] || s[tx][ty] == '#')
      continue;
    dfs222(tx, ty, num);
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> h >> w;
  cin >> sx >> sy >> ex >> ey;
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      cin >> s[i][j];
    }
  }
  int tot = 0;
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      if (vis[i][j] || s[i][j] == '#')
        continue;
      dfs222(i, j, ++tot);
    }
  }
  bfs();
  if (min1 == 1e9)
    cout << -1 << '\\n';
  else
    cout << min1 << '\\n';
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
const int maxn = 2e5 + 10;
const int mod = 1e9 + 7;
char s[1005][1005];
int vis[1005][1005];
int cnt[1005][1005];
int vis2[1005][1005];
int h, w;

int sx, sy, ex, ey;
int xx[5] = {58, 0, 0, 1, -1};
int yy[5] = {58, 1, -1, 0, 0};
int min1 = 1e9;
void dfs(int x, int y, int step, int num) {
  vis2[x][y] = 1;
  if (step >= min1)
    return;
  if (num == cnt[ex][ey]) {
    min1 = min(min1, step);
    return;
  }
  int stx = x - 2, sty = y - 2;
  int enx = x + 2, eny = y + 2;
  for (int i = stx; i <= enx; i++) {
    for (int j = sty; j <= eny; j++) {
      if (i < 1 || j < 1 || i > h || j > w || s[i][j] == '#' || vis2[i][j])
        continue;
      if (num == cnt[i][j]) {
        dfs(i, j, step, num);
        vis2[i][j] = 0;
      } else {
        dfs(i, j, step + 1, cnt[i][j]);
        vis2[i][j] = 0;
      }
    }
  }
}

void dfs222(int x, int y, int num) {
  cnt[x][y] = num;
  vis[x][y] = 1;
  for (int i = 1; i <= 4; i++) {
    int tx = xx[i] + x;
    int ty = yy[i] + y;
    if (tx < 1 || ty < 1 || tx > h || ty > w || vis[tx][ty] || s[tx][ty] == '#')
      continue;
    dfs222(tx, ty, num);
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> h >> w;

  cin >> sx >> sy >> ex >> ey;
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      cin >> s[i][j];
    }
  }
  int tot = 0;
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      if (vis[i][j] || s[i][j] == '#')
        continue;
      dfs222(i, j, ++tot);
    }
  }
  dfs(sx, sy, 0, cnt[sx][sy]);

  if (min1 == 1e9)
    cout << -1 << '\\n';
  else
    cout << min1 << '\\n';
}