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

#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) FOR(i, 0, (n))
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define LAR(a, b) ((a) = max((a), (b)))
#define SML(a, b) ((a) = min((a), (b)))
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vl = vector<ll>;
using pii = pair<int, int>;
using vpii = vector<pair<int, int>>;
template <typename T> using pque = priority_queue<T, vector<T>, greater<T>>;
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define ALL(a) (a).begin(), (a).end()
#ifdef LOCAL_DEBUG
#define DEBUG(...) printf(__VA_ARGS__)
#else
#define DEBUG(...)
#endif

#define N 1024
char s[N][N] = {};
vpii v;
queue<tuple<int, int, int>> que;
int dy[4] = {0, 1, 0, -1};
int dx[4] = {1, 0, -1, 0};

void dfs(int y, int x) {
  if (s[y][x] == '#')
    return;
  s[y][x] = '#';
  v.EB(y, x);
  REP(k, 4) dfs(y + dy[k], x + dx[k]);
}

int main() {
  int h, w;
  scanf("%d%d", &h, &w);
  int sy, sx, ty, tx;
  scanf("%d%d%d%d", &sy, &sx, &ty, &tx);
  sy++;
  sx++;
  ty++;
  tx++;
  REP(i, h) scanf("%s", s[i + 2] + 2);
  REP(i, N) {
    REP(j, N) {
      if (i < 2 || 2 + h <= i || j < 2 || 2 + w <= j) {
        s[i][j] = '#';
      }
    }
  }
  que.emplace(0, sy, sx);

  while (que.size()) {
    auto [ans, y, x] = que.front();
    que.pop();
    v.clear();
    dfs(y, x);
    for (auto [i, j] : v) {
      if (i == ty && j == tx) {
        printf("%d\\n", ans);
        return 0;
      }
      FOR(k, i - 2, i + 3) {
        FOR(l, j - 2, j + 3) {
          if (s[k][l] == '.') {
            que.emplace(ans + 1, k, l);
          }
        }
      }
    }
  }
  printf("-1\\n");
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) FOR(i, 0, (n))
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define LAR(a, b) ((a) = max((a), (b)))
#define SML(a, b) ((a) = min((a), (b)))
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vl = vector<ll>;
using pii = pair<int, int>;
using vpii = vector<pair<int, int>>;
template <typename T> using pque = priority_queue<T, vector<T>, greater<T>>;
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define ALL(a) (a).begin(), (a).end()
#ifdef LOCAL_DEBUG
#define DEBUG(...) printf(__VA_ARGS__)
#else
#define DEBUG(...)
#endif

#define N 1024
char s[N][N] = {};
vpii v;
queue<tuple<int, int, int>> que;
int dy[4] = {0, 1, 0, -1};
int dx[4] = {1, 0, -1, 0};

void dfs(int y, int x) {
  if (s[y][x] == '#')
    return;
  s[y][x] = '#';
  v.EB(y, x);
  REP(k, 4) dfs(y + dy[k], x + dx[k]);
}

int main() {
  int h, w;
  scanf("%d%d", &h, &w);
  int sy, sx, ty, tx;
  scanf("%d%d%d%d", &sy, &sx, &ty, &tx);
  sy++;
  sx++;
  ty++;
  tx++;
  REP(i, h) scanf("%s", s[i + 2] + 2);
  REP(i, N) {
    REP(j, N) {
      if (i < 2 || 2 + h <= i || j < 2 || 2 + w <= j) {
        s[i][j] = '#';
      }
    }
  }
  que.emplace(0, sy, sx);

  while (que.size()) {
    auto [ans, y, x] = que.front();
    que.pop();
    dfs(y, x);
    for (auto [i, j] : v) {
      if (i == ty && j == tx) {
        printf("%d\\n", ans);
        return 0;
      }
      FOR(k, i - 2, i + 3) {
        FOR(l, j - 2, j + 3) {
          if (s[k][l] == '.') {
            que.emplace(ans + 1, k, l);
          }
        }
      }
    }
  }
  printf("-1\\n");
}