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

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ld long double
#define ll long long
#define P pair<int, int>
#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(v) v.begin(), v.end()
#define pb push_back
#define eb emplace_back
template <class T> void chmax(T &a, T b) {
  if (a < b)
    a = b;
}
template <class T> void chmin(T &a, T b) {
  if (a > b)
    a = b;
}
constexpr int INF = 1000000000000000000;
constexpr int mod = 1000000007;
constexpr double eps = 1e-8;
const double pi = acos(-1);
int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
int Random(int mi, int ma) {
  random_device rnd;
  mt19937 mt(rnd()); // 32bit
  //[mi,ma]
  uniform_int_distribution<int> engine(mi, ma);
  return engine(mt);
}
int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
int lcm(int a, int b) { return a / gcd(a, b) * b; }
bool prime(int a) {
  if (a == 1)
    return false;
  for (int i = 2; i * i <= a; i++) {
    if (a % i == 0)
      return false;
  }
  return true;
}
int modpow(int a, int b) {
  if (b == 0)
    return 1;
  if (b % 2)
    return modpow(a, b - 1) * a % mod;
  int memo = modpow(a, b / 2);
  return memo * memo % mod;
}
vector<int> kaijo, invkaijo;
void init_fact(int n) {
  kaijo.resize(n + 1);
  invkaijo.resize(n + 1);
  kaijo[0] = 1;
  for (int i = 1; i <= n; i++) {
    kaijo[i] = kaijo[i - 1] * i;
    kaijo[i] %= mod;
  }
  rep(i, n + 1) invkaijo[i] = modpow(kaijo[i], mod - 2);
}
int comb(int a, int b) {
  if (a < b)
    return 0;
  if (a < 0 || b < 0)
    return 0;
  return kaijo[a] * invkaijo[a - b] % mod * invkaijo[b] % mod;
}
int inv(int x) {
  x = modpow(x, mod - 2);
  return x;
}

int h, w;
int ch, cw, dh, dw;
char s[3010][3010];
int d[3010][3010];
void bfs() {
  rep(i, h) rep(j, w) d[i][j] = INF;
  d[ch][cw] = 0;
  vector<P> v;
  v.pb({ch, cw});
  rep(dist, INF) {
    vector<P> u;
    queue<P> q;
    for (P p : v)
      q.push(p);
    while (!q.empty()) {
      P p = q.front();
      q.pop();
      u.pb(p);
      int x = p.fi, y = p.se;
      rep(i, 4) {
        int X = p.fi + dx[i], Y = p.se + dy[i];
        if (0 <= X && X < h && 0 <= Y && y < w && s[X][Y] == '.') {
          if (d[X][Y] > d[x][y]) {
            d[X][Y] = d[x][y];
            q.push({X, Y});
          }
        }
      }
    }
    v.clear();
    for (P p : u) {
      int x = p.fi, y = p.se;
      for (int i = -2; i <= 2; i++) {
        for (int j = -2; j <= 2; j++) {
          int X = x + i, Y = y + j;
          if (0 <= X && X < h && 0 <= Y && y < w && s[X][Y] == '.') {
            if (d[X][Y] > d[x][y] + 1) {
              d[X][Y] = d[x][y] + 1;
              v.pb({X, Y});
            }
          }
        }
      }
    }
    if (v.empty())
      break;
  }
}
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> h >> w >> ch >> cw >> dh >> dw;
  ch--, cw--, dh--, dw--;
  rep(i, h) {
    rep(j, w) { cin >> s[i][j]; }
  }
  bfs();
  int ans = d[dh][dw];
  if (ans == INF)
    cout << -1 << endl;
  else
    cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ld long double
#define ll long long
#define P pair<int, int>
#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(v) v.begin(), v.end()
#define pb push_back
#define eb emplace_back
template <class T> void chmax(T &a, T b) {
  if (a < b)
    a = b;
}
template <class T> void chmin(T &a, T b) {
  if (a > b)
    a = b;
}
constexpr int INF = 1000000000000000000;
constexpr int mod = 1000000007;
constexpr double eps = 1e-8;
const double pi = acos(-1);
int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
int Random(int mi, int ma) {
  random_device rnd;
  mt19937 mt(rnd()); // 32bit
  //[mi,ma]
  uniform_int_distribution<int> engine(mi, ma);
  return engine(mt);
}
int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
int lcm(int a, int b) { return a / gcd(a, b) * b; }
bool prime(int a) {
  if (a == 1)
    return false;
  for (int i = 2; i * i <= a; i++) {
    if (a % i == 0)
      return false;
  }
  return true;
}
int modpow(int a, int b) {
  if (b == 0)
    return 1;
  if (b % 2)
    return modpow(a, b - 1) * a % mod;
  int memo = modpow(a, b / 2);
  return memo * memo % mod;
}
vector<int> kaijo, invkaijo;
void init_fact(int n) {
  kaijo.resize(n + 1);
  invkaijo.resize(n + 1);
  kaijo[0] = 1;
  for (int i = 1; i <= n; i++) {
    kaijo[i] = kaijo[i - 1] * i;
    kaijo[i] %= mod;
  }
  rep(i, n + 1) invkaijo[i] = modpow(kaijo[i], mod - 2);
}
int comb(int a, int b) {
  if (a < b)
    return 0;
  if (a < 0 || b < 0)
    return 0;
  return kaijo[a] * invkaijo[a - b] % mod * invkaijo[b] % mod;
}
int inv(int x) {
  x = modpow(x, mod - 2);
  return x;
}

int h, w;
int ch, cw, dh, dw;
char s[3010][3010];
int d[3010][3010];
void bfs() {
  rep(i, h) rep(j, w) d[i][j] = INF;
  d[ch][cw] = 0;
  queue<P> q;
  q.push({ch, cw});
  while (!q.empty()) {
    P p = q.front();
    q.pop();
    int x = p.fi, y = p.se;
    rep(i, 4) {
      int X = p.fi + dx[i], Y = p.se + dy[i];
      if (0 <= X && X < h && 0 <= Y && y < w && s[X][Y] == '.') {
        if (d[X][Y] > d[x][y]) {
          d[X][Y] = d[x][y];
          q.push({X, Y});
        }
      }
    }
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        int X = x + i, Y = y + j;
        if (0 <= X && X < h && 0 <= Y && y < w && s[X][Y] == '.') {
          if (d[X][Y] > d[x][y] + 1) {
            d[X][Y] = d[x][y] + 1;
            q.push({X, Y});
          }
        }
      }
    }
  }
}
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> h >> w >> ch >> cw >> dh >> dw;
  ch--, cw--, dh--, dw--;
  rep(i, h) {
    rep(j, w) { cin >> s[i][j]; }
  }
  bfs();
  int ans = d[dh][dw];
  if (ans == INF)
    cout << -1 << endl;
  else
    cout << ans << endl;
  return 0;
}