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

// #Sazaの1日1AC
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000000 + 7;
const long long INF = 9e18;
const double PI = 3.14159265358979323846;

// マクロ
// 型エイリアス
using ll = long long;
using P = pair<int, int>;
using vl = vector<long long>;
using vvl = vector<vector<long long>>;
using vP = vector<pair<long long, long long>>;
// ショートカット
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep2(i, k, n) for (long long i = k; i < n; i++) // 半開区間
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define pb push_back
#define mkp make_pair
// 入力
#define vin(v, N)                                                              \\
  for (long long i = 0; i < N; i++)                                            \\
  cin >> v.at(i)
#define lin(n)                                                                 \\
  long long n;                                                                 \\
  cin >> n
#define chin(x)                                                                \\
  char x;                                                                      \\
  cin >> x;
#define sin(s)                                                                 \\
  string s;                                                                    \\
  cin >> s;
#define vlin(v, N)                                                             \\
  vector<long long> v(N);                                                      \\
  for (long long i = 0; i < N; i++)                                            \\
  cin >> v.at(i)

#define max(a, b) max((ll)a, (ll)b)
#define min(a, b) min((ll)a, (ll)b)

// 関数
// 最大公約数
long long gcd(long long m, long long n) {
  long long a = max(m, n);
  long long b = min(m, n);
  long long r = a % b;
  while (r != 0) {
    a = b;
    b = r;
    r = a % b;
  }
  return b;
}
// 最小公倍数
long long lcd(long long m, long long n) { return m * n / gcd(m, n); }
// xのn乗
long long power(long long x, long long N) {
  long long ret = 1;
  for (long long i = 0; i < N; i++)
    ret *= x;
  return ret;
}
// 繰り返し二乗法
long long repeat_squaring(long long x, long long n) {
  if (n == 0)
    return 1;
  else if (n % 2 == 0) {
    long long t = repeat_squaring(x, n / 2);
    return t * t % mod;
  } else
    return x * repeat_squaring(x, n - 1);
}
// 素因数分解(mapで返す)
map<long long, long long> factor(long long p) {
  ll p1 = p;
  map<long long, long long> ret;
  for (long long i = 2; i * i <= p1; i++) {
    while (p % i == 0) {
      ret[i]++;
      p /= i;
    }
  }
  if (p != 1)
    ret[p]++;
  return ret;
}
// 素数判定
bool is_prime(long long N) {
  for (long long i = 2; i * i <= N; i++) {
    if (N % i == 0)
      return false;
  }
  return true;
}
// 最大値更新
void chmax(long long &a, long long b) { a = max(a, b); }
// 最小値更新
void chmin(long long &a, long long b) { a = min(a, b); }

// 構造体

// Union-Find木
struct UnionFind {
  // メンバ変数
  vector<long long> par; // par[i]:=頂点iの親
  vector<long long> s; // s[i]:=頂点iが属する集合の個数(iが根のとき)

  // コンストラクタ
  UnionFind(long long N) : par(N), s(N) {
    for (long long i = 0; i < N; i++) {
      par[i] = i; // 最初はすべてが根として初期化
      s[i] = 1;
    }
  }
  // メンバ関数
  // root(i):=頂点iの根
  long long root(long long i) {
    if (par[i] == i)
      return i;
    par[i] = root(par[i]); // 経路圧縮
    return root(par[i]);   // 再帰
  }
  // size[i]:=iが属する集合の個数
  long long size(long long i) { return s[root(i)]; }
  // same(x,y) xとyが同じ根を持つか(同じ集合に含まれるか)
  bool same(long long x, long long y) { return root(x) == root(y); }
  // unite(x,y) xの根をyの根に繋げる(集合を合併)
  void unite(long long x, long long y) {
    if (!same(x, y)) {
      long long rx = root(x);
      long long ry = root(y);
      par[rx] = par[ry];
      s[ry] = s[rx] + s[ry];
    }
  }
};

/////////////////⊂('ω'⊂ )))Σ≡GO!/////////////////
int dh[4] = {1, 0, -1, 0};
int dw[4] = {0, 1, 0, -1};

// メイン関数
int main() {
  // doubleの桁数

  int H, W, sh, sw, th, tw;
  cin >> H >> W >> sh >> sw >> th >> tw;
  sh--;
  th--;
  sw--;
  tw--;

  vector<vector<char>> maze(H, vector<char>(W));
  rep(i, H) rep(j, W) cin >> maze[i][j];

  deque<P> Q;

  vector<vector<int>> d(H, vector<int>(W, mod));
  Q.push_front(mkp(sh, sw));
  d[sh][sw] = 0;

  while (!Q.empty()) {
    int h, w;
    tie(h, w) = Q.front();
    Q.pop_front();
    // cout << Q.size() << endl;

    rep(i, 4) {
      int nh = h + dh[i], nw = w + dw[i];
      if (nh < 0 || H <= nh || nw < 0 || W <= nw)
        continue;
      if (maze[nh][nw] == '#')
        continue;
      if (d[nh][nw] <= d[h][w])
        continue;
      d[nh][nw] = d[h][w];
      Q.emplace_front(mkp(nh, nw));
    }
    for (int eh = -2; eh <= 2; eh++) {
      for (int ew = -2; ew <= 2; ew++) {
        int nh = h + eh, nw = w + ew;
        if (nh < 0 || H <= nh || nw < 0 || W <= nw)
          continue;
        if (maze[nh][nw] == '#')
          continue;
        if (d[nh][nw] <= d[h][w] + 1)
          continue;

        d[nh][nw] = d[h][w] + 1;
        Q.emplace_back(mkp(nh, nw));
      }
    }
  }

  if (d[th][tw] == mod)
    cout << -1 << endl;
  else
    cout << d[th][tw] << endl;
}

Error: TLE

Fixed submission:

// #Sazaの1日1AC
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000000 + 7;
const long long INF = 9e15;
const double PI = 3.14159265358979323846;

// マクロ
// 型エイリアス
using ll = long long;
using P = pair<long long, long long>;
using vl = vector<long long>;
using vvl = vector<vector<long long>>;
using vP = vector<pair<long long, long long>>;
// ショートカット
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep2(i, k, n) for (long long i = k; i < n; i++) // 半開区間
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define pb push_back
#define mkp make_pair
// 入力
#define vin(v, N)                                                              \\
  for (long long i = 0; i < N; i++)                                            \\
  cin >> v.at(i)
#define lin(n)                                                                 \\
  long long n;                                                                 \\
  cin >> n
#define chin(x)                                                                \\
  char x;                                                                      \\
  cin >> x;
#define sin(s)                                                                 \\
  string s;                                                                    \\
  cin >> s;
#define vlin(v, N)                                                             \\
  vector<long long> v(N);                                                      \\
  for (long long i = 0; i < N; i++)                                            \\
  cin >> v.at(i)

#define max(a, b) max((ll)a, (ll)b)
#define min(a, b) min((ll)a, (ll)b)

// 関数
// 最大公約数
long long gcd(long long m, long long n) {
  long long a = max(m, n);
  long long b = min(m, n);
  long long r = a % b;
  while (r != 0) {
    a = b;
    b = r;
    r = a % b;
  }
  return b;
}
// 最小公倍数
long long lcd(long long m, long long n) { return m * n / gcd(m, n); }
// xのn乗
long long power(long long x, long long N) {
  long long ret = 1;
  for (long long i = 0; i < N; i++)
    ret *= x;
  return ret;
}
// 繰り返し二乗法
long long repeat_squaring(long long x, long long n) {
  if (n == 0)
    return 1;
  else if (n % 2 == 0) {
    long long t = repeat_squaring(x, n / 2);
    return t * t % mod;
  } else
    return x * repeat_squaring(x, n - 1);
}
// 素因数分解(mapで返す)
map<long long, long long> factor(long long p) {
  ll p1 = p;
  map<long long, long long> ret;
  for (long long i = 2; i * i <= p1; i++) {
    while (p % i == 0) {
      ret[i]++;
      p /= i;
    }
  }
  if (p != 1)
    ret[p]++;
  return ret;
}
// 素数判定
bool is_prime(long long N) {
  for (long long i = 2; i * i <= N; i++) {
    if (N % i == 0)
      return false;
  }
  return true;
}
// 最大値更新
void chmax(long long &a, long long b) { a = max(a, b); }
// 最小値更新
void chmin(long long &a, long long b) { a = min(a, b); }

// 構造体

// Union-Find木
struct UnionFind {
  // メンバ変数
  vector<long long> par; // par[i]:=頂点iの親
  vector<long long> s; // s[i]:=頂点iが属する集合の個数(iが根のとき)

  // コンストラクタ
  UnionFind(long long N) : par(N), s(N) {
    for (long long i = 0; i < N; i++) {
      par[i] = i; // 最初はすべてが根として初期化
      s[i] = 1;
    }
  }
  // メンバ関数
  // root(i):=頂点iの根
  long long root(long long i) {
    if (par[i] == i)
      return i;
    par[i] = root(par[i]); // 経路圧縮
    return root(par[i]);   // 再帰
  }
  // size[i]:=iが属する集合の個数
  long long size(long long i) { return s[root(i)]; }
  // same(x,y) xとyが同じ根を持つか(同じ集合に含まれるか)
  bool same(long long x, long long y) { return root(x) == root(y); }
  // unite(x,y) xの根をyの根に繋げる(集合を合併)
  void unite(long long x, long long y) {
    if (!same(x, y)) {
      long long rx = root(x);
      long long ry = root(y);
      par[rx] = par[ry];
      s[ry] = s[rx] + s[ry];
    }
  }
};

/////////////////⊂('ω'⊂ )))Σ≡GO!/////////////////
ll dh[] = {1, 0, -1, 0, 2, -2};
ll dw[] = {0, 1, 0, -1, 2, -2};

void bfs(ll H, ll W, ll sh, ll sw, int &cnt, vector<vector<int>> &c) {
  if (c[sh][sw] != 0)
    return;
  cnt = cnt + 1;
  queue<pair<ll, ll>> Q;
  Q.push(mkp(sh, sw));
  while (Q.size() > 0) {
    ll h, w;
    tie(h, w) = Q.front();
    Q.pop();
    c[h][w] = cnt;
    rep(i, 4) {
      ll nh = h + dh[i];
      ll nw = w + dw[i];
      if (0 <= nh && nh < H && 0 <= nw && nw < W && c[nh][nw] == 0) {
        Q.push(mkp(nh, nw));
      }
    }
  }
}

// メイン関数
int main() {
  // doubleの桁数
  cout << fixed << setprecision(10);

  lin(H);
  lin(W);
  lin(sh);
  lin(sw);
  lin(th);
  lin(tw);
  sh--;
  sw--;
  th--;
  tw--;
  vector<vector<int>> c(H, vector<int>(W, 0));
  rep(i, H) {
    rep(j, W) {
      chin(C);
      if (C == '#')
        c[i][j] = -1;
    }
  }

  int cnt = 0;
  rep(i, H) {
    rep(j, W) { bfs(H, W, i, j, cnt, c); }
  }
  rep(i, H) rep(j, W) c[i][j]--;

  vector<vector<ll>> G(cnt);

  set<pair<ll, ll>> S;

  rep(i, H) {
    rep(j, W) {
      if (c[i][j] < 0)
        continue;
      rep(p, 6) {
        rep(q, 6) {
          ll nh = i + dh[p];
          ll nw = j + dw[q];
          if (0 <= nh && nh < H && 0 <= nw && nw < W && c[nh][nw] >= 0 &&
              c[nh][nw] != c[i][j] && !S.count(mkp(c[i][j], c[nh][nw]))) {
            G[c[i][j]].pb(c[nh][nw]);
            G[c[nh][nw]].pb(c[i][j]);
            S.insert(mkp(c[i][j], c[nh][nw]));
          }
        }
      }
    }
  }

  /*
  rep(i, H){
    rep(j, W){
      cout << c[i][j] << " ";
    }
    cout << endl;
  }
    cout << G[0][0]<< endl;
  */

  ll s = c[sh][sw];
  ll t = c[th][tw];

  vector<int> d(cnt, -1);
  d[s] = 0;

  queue<ll> Q;
  Q.push(s);
  while (Q.size() > 0) {
    ll u = Q.front();
    Q.pop();
    rep(i, G[u].size()) {
      ll v = G[u][i];
      if (d[v] == -1) {
        d[v] = d[u] + 1;
        Q.push(v);
      }
    }
  }

  if (d[t] != INF)
    cout << d[t] << endl;
  else
    cout << -1 << endl;
}