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

#include "bits/stdc++.h"
#define rep(i, j) for (int i = 0; i < j; i++)
#define srep(i, j, k) for (int i = j; i < k; i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
using ull = unsigned long long;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

// �G���g�X�e�l�X���
vector<int> estora(vector<int> num) {
  int sel = 0;
  while (sel < num.size()) {
    int d = num[sel];
    for (int i = 0; i < num.size(); i++) {
      if (num[i] % d == 0 && num[i] != d && d != 1) {
        num.erase(num.begin() + i);
        i--;
      }
    }
    sel++;
  }
  return num;
}
/*���C�u�����ő����*/
// ���[�O���b�h�̌ݏ��@
int gcd(int x, int y) {
  int num[3];
  num[0] = (x > y) ? x : y;
  num[1] = (x <= y) ? x : y;
  num[2] = num[0] % num[1];

  while (num[2]) {
    num[0] = num[1];
    num[1] = num[2];
    num[2] = num[0] % num[1];
  }

  return num[1];
}

/*�f��������*/
// ���색�C�u����
// i����ɑf���ɂ��Ă����Ȃ��Ƃ��܂��ł��Ȃ�&&�v�Z�ʃ��o�C
// �G�X�g���l�X��⿂őf����U����Ă����K�v�������肻��
vector<ll> fac(ll n, vector<ll> num) {
  ll kazu = n;
  for (int i = 2; i <= n; i++) {
    while (1) {
      if (kazu % i != 0)
        break;

      kazu /= i;
      num[i]++;
    }
  }
  if (kazu == n && n != 1)
    num[n]++;
  return num;
  int ans = 0;
  if (kazu != 1)
    ans++;
  for (int i = 2; i <= sqrt(n); i++) {
    for (int j = 1; j <= num[i]; j++) {
      num[i] -= j;
      ans++;
    }
  }
  return num;
}
const int INF = 1001001001;

// mint�p�̒萔 (10��9�� + 7)
const int mod = 1000000007;

struct mint {
  ll x; // typedef long long ll;
  mint(ll x = 0) : x((x % mod + mod) % mod) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const { return mint(*this) += a; }
  mint operator-(const mint a) const { return mint(*this) -= a; }
  mint operator*(const mint a) const { return mint(*this) *= a; }
  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(mod - 2); }
  mint &operator/=(const mint a) { return *this *= a.inv(); }
  mint operator/(const mint a) const { return mint(*this) /= a; }
};
istream &operator>>(istream &is, const mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }

int main() {
  int h, w, ch, cw, dh, dw;
  cin >> h >> w >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  vector<string> s(h);
  rep(i, h) cin >> s[i];
  vector<vector<int>> map(h, vector<int>(w, -1));
  deque<P> masu;
  P c;
  c.first = ch;
  c.second = cw;
  map[ch][cw] = 0;
  int wara = 0;
  masu.insert(masu.begin(), c);
  vector<vector<int>> warp(h, vector<int>(w, -1));
  while (!masu.empty()) {
    P current = masu.front();
    masu.pop_front();

    vector<int> vec = {-2, -1, 0, 1, 2};
    for (int j = 0; j < 5; j++) {
      for (int i = 0; i < 5; i++) {
        if (!(current.first + vec[i] >= 0 && current.first + vec[i] < h &&
              current.second + vec[j] >= 0 && current.second + vec[j] < w))
          continue;
        int cost = 0;
        if (abs(vec[i]) + abs(vec[j]) >= 2)
          cost = 1;
        if (map[current.first + vec[i]][current.second + vec[j]] <=
                map[current.first][current.second] + cost &&
            map[current.first + vec[i]][current.second + vec[j]] != -1)
          continue;
        if (s[current.first + vec[i]][current.second + vec[j]] == '#')
          continue;

        map[current.first + vec[i]][current.second + vec[j]] =
            map[current.first][current.second] + cost;

        P kari;
        kari.first = current.first + vec[i];
        kari.second = current.second + vec[j];
        if (cost == 0)
          masu.push_front(kari);
        else
          masu.push_back(kari);
      }
    }
  }

  cout << map[dh][dw];
  return 0;
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
#define rep(i, j) for (int i = 0; i < j; i++)
#define srep(i, j, k) for (int i = j; i < k; i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
using ull = unsigned long long;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

// �G���g�X�e�l�X���
vector<int> estora(vector<int> num) {
  int sel = 0;
  while (sel < num.size()) {
    int d = num[sel];
    for (int i = 0; i < num.size(); i++) {
      if (num[i] % d == 0 && num[i] != d && d != 1) {
        num.erase(num.begin() + i);
        i--;
      }
    }
    sel++;
  }
  return num;
}
/*���C�u�����ő����*/
// ���[�O���b�h�̌ݏ��@
int gcd(int x, int y) {
  int num[3];
  num[0] = (x > y) ? x : y;
  num[1] = (x <= y) ? x : y;
  num[2] = num[0] % num[1];

  while (num[2]) {
    num[0] = num[1];
    num[1] = num[2];
    num[2] = num[0] % num[1];
  }

  return num[1];
}

/*�f��������*/
// ���색�C�u����
// i����ɑf���ɂ��Ă����Ȃ��Ƃ��܂��ł��Ȃ�&&�v�Z�ʃ��o�C
// �G�X�g���l�X��⿂őf����U����Ă����K�v�������肻��
vector<ll> fac(ll n, vector<ll> num) {
  ll kazu = n;
  for (int i = 2; i <= n; i++) {
    while (1) {
      if (kazu % i != 0)
        break;

      kazu /= i;
      num[i]++;
    }
  }
  if (kazu == n && n != 1)
    num[n]++;
  return num;
  int ans = 0;
  if (kazu != 1)
    ans++;
  for (int i = 2; i <= sqrt(n); i++) {
    for (int j = 1; j <= num[i]; j++) {
      num[i] -= j;
      ans++;
    }
  }
  return num;
}
const int INF = 1001001001;

// mint�p�̒萔 (10��9�� + 7)
const int mod = 1000000007;

struct mint {
  ll x; // typedef long long ll;
  mint(ll x = 0) : x((x % mod + mod) % mod) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const { return mint(*this) += a; }
  mint operator-(const mint a) const { return mint(*this) -= a; }
  mint operator*(const mint a) const { return mint(*this) *= a; }
  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(mod - 2); }
  mint &operator/=(const mint a) { return *this *= a.inv(); }
  mint operator/(const mint a) const { return mint(*this) /= a; }
};
istream &operator>>(istream &is, const mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }

int main() {
  int h, w, ch, cw, dh, dw;
  cin >> h >> w >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  vector<string> s(h);
  rep(i, h) cin >> s[i];
  vector<vector<int>> map(h, vector<int>(w, -1));
  vector<P> masu;
  P c;
  c.first = ch;
  c.second = cw;
  map[ch][cw] = 0;
  int wara = 0;
  masu.insert(masu.begin(), c);
  while (1) {
    vector<vector<int>> warp(h, vector<int>(w, -1));
    vector<P> masu2;
    while (!masu.empty()) {
      P current = masu[0];
      masu.erase(masu.begin());

      vector<vector<int>> vec = {{0, 1, 0, -1}, {1, 0, -1, 0}};
      for (int i = 0; i < 4; i++) {
        if (!(current.first + vec[0][i] >= 0 && current.first + vec[0][i] < h &&
              current.second + vec[1][i] >= 0 &&
              current.second + vec[1][i] < w))
          continue;
        if (map[current.first + vec[0][i]][current.second + vec[1][i]] != -1)
          continue;
        if (s[current.first + vec[0][i]][current.second + vec[1][i]] == '#')
          continue;
        map[current.first + vec[0][i]][current.second + vec[1][i]] = wara;
        if (current.first + vec[0][i] == dh &&
            current.second + vec[1][i] == dw) {
          cout << wara << endl;
          return 0;
        }
        P kari;
        kari.first = current.first + vec[0][i];
        kari.second = current.second + vec[1][i];
        masu.emplace_back(kari);
        // if (warp[kari.first][kari.second] != -1) {
        //     masu2.erase(masu2.begin() + warp[kari.first][kari.second] - num);
        //     num++;
        // }
      }
      vector<vector<int>> d_m = {
          {-2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -2, -2, 2, 2, 2, -1, 1, -1, 1},
          {-2, -2, -2, -2, -2, 2,  2,  2, 2, 2,
           1,  0,  -1, 1,  0,  -1, -1, 1, 1, -1}};
      rep(i, d_m[0].size()) {
        if (!(current.first + d_m[0][i] >= 0 && current.first + d_m[0][i] < h &&
              current.second + d_m[1][i] >= 0 &&
              current.second + d_m[1][i] < w))
          continue;
        if (map[current.first + d_m[0][i]][current.second + d_m[1][i]] != -1)
          continue;
        if (warp[current.first + d_m[0][i]][current.second + d_m[1][i]] != -1)
          continue;
        if (s[current.first + d_m[0][i]][current.second + d_m[1][i]] == '#')
          continue;
        warp[current.first + d_m[0][i]][current.second + d_m[1][i]] = 0;
        P kari;
        kari.first = current.first + d_m[0][i];
        kari.second = current.second + d_m[1][i];
        masu2.emplace_back(kari);
      }
    }
    if (masu2.size() == 0) {
      break;
    }
    masu = masu2;
    wara++;
  }
  cout << map[dh][dw];
  return 0;
}