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

#include <bits/stdc++.h>
#define f first
#define s second
#define fore(i, a, b) for (int i = (a), ThxMK = (b); i < ThxMK; ++i)
#define pb push_back
#define all(s) begin(s), end(s)
#define _                                                                      \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define sz(s) int(s.size())
#define ENDL '\\n'
using namespace std;
typedef long double ld;
typedef long long lli;
typedef pair<lli, lli> ii;
typedef vector<int> vi;
typedef vector<lli> vl;
typedef vector<ii> vii;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
#define deb(x) cout << #x ": " << (x) << endl;

const int N = 1e3;
const lli INF = 1e18;
const lli MOD = 1e9 + 7;

vi m;
vi primes(1, 2);
vector<vector<ld>> nk;
void sieve() {
  m = vi(N + 1, 0);
  for (int i = 4; i < N; i += 2)
    m[i] = 1;
  for (int i = 3; i * i <= N; i += 2)
    if (!m[i])
      for (int j = i * i; j < N; j += i)
        m[j] = 1;
  for (int i = 3; i < N; i += 2)
    if (!m[i])
      primes.pb(i);
}

void pascal() {
  nk = vector<vector<ld>>(N, vector<ld>(N, 0.0));
  fore(i, 0, N) nk[i][0] = nk[i][i] = 1.0;
  fore(i, 1, N) fore(j, 1, i) nk[i][j] = nk[i - 1][j - 1] + nk[i - 1][j];
}

lli gcd(lli a, lli b) { return (b ? gcd(b, a % b) : a); }

lli lcm(lli a, lli b) {
  if (a < b)
    swap(a, b);
  lli c = gcd(a, b);
  a /= c;
  return a * b;
}

int popcount(lli x) { return __builtin_popcountll(x); }

lli poww(lli a, lli b) {
  lli res = 1;
  while (b) {
    if (b & 1)
      res = res * a;
    a = a * a;
    b /= 2;
  }
  return res;
}

lli powm(lli a, lli b) {
  lli res = 1;
  while (b) {
    if (b & 1)
      res = (res * a) % MOD;
    a = (a * a) % MOD;
    b /= 2;
  }
  return res;
}

bool isPrime(lli x) {
  if (x == 1)
    return 0;
  for (auto i : primes) {
    if (i * i > x)
      return 1;
    if (x % i == 0)
      return 0;
  }
  return 1;
}

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

// ---- コーディングはここから！ ('-')7

void solve() {
  int h, w;
  cin >> h >> w;
  int x, y;
  cin >> x >> y;
  int tx, ty;
  cin >> tx >> ty;
  x--, y--, tx--, ty--;
  vector<string> s(h);
  fore(i, 0, h) cin >> s[i];
  queue<ii> q;

  vvl vis(h, vl(w, 0)), dfs(h, vl(w, 0));
  auto valid = [&](int a, int b) {
    return a >= 0 and a < h and b >= 0 and b < w and s[a][b] == '.' and
           !vis[a][b];
  };
  if (valid(x, y)) {
    q.push({x, y});
    vis[x][y] = 1;
  }

  auto f = [&](auto f, int a, int b, int d) -> void {
    vis[a][b] = d;
    dfs[a][b] = 1;
    bool mark = 0;
    fore(i, 0, 4) {
      int aa = a + dx[i], bb = b + dy[i];
      if (valid(aa, bb))
        f(f, aa, bb, d);
      else
        mark = 1;
    }
    if (mark)
      q.push({a, b});
  };
  while (!q.empty()) {
    auto [a, b] = q.front();
    q.pop();
    auto d = vis[a][b];
    if (!dfs[a][b])
      f(f, a, b, d);
    fore(i, -2, 3) fore(j, -2, 3) {
      int aa = a + i, bb = b + j;
      if (valid(aa, bb))
        f(f, aa, bb, d + 1);
    }
  }
  cout << vis[tx][ty] - 1 << ENDL;
}

int main() {
  _
  // int t; cin>>t; while(t--)
  solve();
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define f first
#define s second
#define fore(i, a, b) for (int i = (a), ThxMK = (b); i < ThxMK; ++i)
#define pb push_back
#define all(s) begin(s), end(s)
#define _                                                                      \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define sz(s) int(s.size())
#define ENDL '\\n'
using namespace std;
typedef long double ld;
typedef long long lli;
typedef pair<lli, lli> ii;
typedef vector<int> vi;
typedef vector<lli> vl;
typedef vector<ii> vii;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
#define deb(x) cout << #x ": " << (x) << endl;

const int N = 1e3;
const lli INF = 1e18;
const lli MOD = 1e9 + 7;

vi m;
vi primes(1, 2);
vector<vector<ld>> nk;
void sieve() {
  m = vi(N + 1, 0);
  for (int i = 4; i < N; i += 2)
    m[i] = 1;
  for (int i = 3; i * i <= N; i += 2)
    if (!m[i])
      for (int j = i * i; j < N; j += i)
        m[j] = 1;
  for (int i = 3; i < N; i += 2)
    if (!m[i])
      primes.pb(i);
}

void pascal() {
  nk = vector<vector<ld>>(N, vector<ld>(N, 0.0));
  fore(i, 0, N) nk[i][0] = nk[i][i] = 1.0;
  fore(i, 1, N) fore(j, 1, i) nk[i][j] = nk[i - 1][j - 1] + nk[i - 1][j];
}

lli gcd(lli a, lli b) { return (b ? gcd(b, a % b) : a); }

lli lcm(lli a, lli b) {
  if (a < b)
    swap(a, b);
  lli c = gcd(a, b);
  a /= c;
  return a * b;
}

int popcount(lli x) { return __builtin_popcountll(x); }

lli poww(lli a, lli b) {
  lli res = 1;
  while (b) {
    if (b & 1)
      res = res * a;
    a = a * a;
    b /= 2;
  }
  return res;
}

lli powm(lli a, lli b) {
  lli res = 1;
  while (b) {
    if (b & 1)
      res = (res * a) % MOD;
    a = (a * a) % MOD;
    b /= 2;
  }
  return res;
}

bool isPrime(lli x) {
  if (x == 1)
    return 0;
  for (auto i : primes) {
    if (i * i > x)
      return 1;
    if (x % i == 0)
      return 0;
  }
  return 1;
}

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

// ---- コーディングはここから！ ('-')7

void solve() {
  int h, w;
  cin >> h >> w;
  int x, y;
  cin >> x >> y;
  int tx, ty;
  cin >> tx >> ty;
  x--, y--, tx--, ty--;
  vector<string> s(h);
  fore(i, 0, h) cin >> s[i];
  priority_queue<pair<lli, ii>> q;

  vvl vis(h, vl(w, INF)), dfs(h, vl(w, 0));
  auto valid = [&](int a, int b) {
    return a >= 0 and a < h and b >= 0 and b < w and s[a][b] == '.';
  };
  if (valid(x, y)) {
    q.push({0LL, ii{x, y}});
    vis[x][y] = 0;
  }

  auto f = [&](auto f, int a, int b, int d) -> void {
    dfs[a][b] = 1;
    bool mark = 0;
    fore(i, 0, 4) {
      int aa = a + dx[i], bb = b + dy[i];
      if (valid(aa, bb) and vis[aa][bb] > d) {
        vis[aa][bb] = d;
        f(f, aa, bb, d);
      } else
        mark = 1;
    }
    if (mark)
      q.push({d, ii{a, b}});
  };
  while (!q.empty()) {
    auto [d, c] = q.top();
    q.pop();
    auto [a, b] = c;
    if (vis[a][b] != d)
      continue;
    if (!dfs[a][b])
      f(f, a, b, d);
    fore(i, -2, 3) fore(j, -2, 3) {
      int aa = a + i, bb = b + j;
      if (valid(aa, bb) and vis[aa][bb] > d + 1) {
        q.push({d + 1, ii{aa, bb}});
        vis[aa][bb] = d + 1;
      }
    }
  }
  cout << (vis[tx][ty] == INF ? -1 : vis[tx][ty]) << ENDL;
}

int main() {
  _
  // int t; cin>>t; while(t--)
  solve();
}