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

#include <bits/stdc++.h>

#define ntest 0

using namespace std;

int h, w, ch, cw, dh, dw, x, y, r, c;
char s[1005][1005];
int vis[1005][1005];
int b[1005][1005], res[1005][1005];
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};

int valid(int i, int j) {
  if (i > h || i < 1 || j > w || j < 1 || s[i][j] == '#')
    return 0;
  return 1;
}
void solve() {
  cin >> h >> w >> ch >> cw >> dh >> dw;
  for (int i = 1; i <= h; ++i) {
    for (int j = 1; j <= w; ++j) {
      cin >> s[i][j];
      res[i][j] = INT_MAX >> 1;
    }
  }
  deque<pair<int, int>> dq;
  dq.push_front({ch, cw});
  res[ch][cw] = 0;
  while (!dq.empty()) {
    pair<int, int> p = dq.front();
    dq.pop_front();
    x = p.first, y = p.second;
    // cout << "x: " << x << " y: " << y << '\\n';
    vis[x][y] = 1;
    for (int i = 0; i < 4; ++i) {
      r = x + dx[i];
      c = y + dy[i];
      if (valid(r, c) && !vis[r][c]) {
        res[r][c] = min(res[r][c], res[x][y]);
        dq.push_front({r, c});
      }
    }
    if (!b[x][y]) {
      b[x][y] = 1;
      for (int i = x - 2; i <= x + 2; ++i)
        for (int j = y - 2; j <= y + 2; ++j) {
          if (valid(i, j)) {
            res[i][j] = min(res[i][j], res[x][y] + 1);
            dq.push_back({i, j});
          }
        }
    }
  }
  if (!vis[dh][dw])
    cout << -1;
  else
    cout << res[dh][dw];
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  if (ntest) {
    int t;
    cin >> t;
    while (t--)
      solve();
  } else {
    solve();
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define ntest 0

using namespace std;
int h, w, ch, cw, dh, dw;
char s[1005][1005];
int a[1005][1005], m[1005][1005], b[1005][1005];
int change;
void go1(int i, int j) {
  if (i == dh && j == dw) {
    cout << 0;
    exit(0);
  }
  b[i][j] = 1;
  if (j > 1 && s[i][j - 1] == '.' && !b[i][j - 1])
    go1(i, j - 1);
  if (j < w && s[i][j + 1] == '.' && !b[i][j + 1])
    go1(i, j + 1);
  if (i > 1 && s[i - 1][j] == '.' && !b[i - 1][j])
    go1(i - 1, j);
  if (i < h && s[i + 1][j] == '.' && !b[i + 1][j])
    go1(i + 1, j);
}
void go(int i, int j, int magic) {
  if (i < 1 || i > h || j < 1 || j > w)
    return;
  a[i][j] = 1;
  change = 0;
  if (m[i][j] > magic) {
    change = 1;
    m[i][j] = magic;
  }
  if (j > 1 && s[i][j - 1] == '.' && !a[i][j - 1])
    go(i, j - 1, m[i][j]);
  if (j < w && s[i][j + 1] == '.' && !a[i][j + 1])
    go(i, j + 1, m[i][j]);
  if (i > 1 && s[i - 1][j] == '.' && !a[i - 1][j])
    go(i - 1, j, m[i][j]);
  if (i < h && s[i + 1][j] == '.' && !a[i + 1][j])
    go(i + 1, j, m[i][j]);
  if (change) {
    for (int r = max(1, i - 2); r <= min(h, i + 2); ++r)
      for (int c = max(1, j - 2); c <= min(w, j + 2); ++c) {
        if (s[r][c] == '.' && m[i][j] + 1 < m[r][c] &&
            m[i][j] + 1 < m[dh][dw]) {
          go(r, c, m[i][j] + 1);
        }
      }
  }
}
void solve() {
  scanf("%d%d%d%d%d%d", &h, &w, &ch, &cw, &dh, &dw);
  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)
      m[i][j] = INT_MAX;
  go1(ch, cw);
  go(ch, cw, 0);
  if (!a[dh][dw])
    printf("-1");
  else
    printf("%d", m[dh][dw]);
}
int main() { solve(); }