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

#include <bits/stdc++.h>
using namespace std;
#define pi 2 * asin(1.0)
#define MAX 10000000
#define MOD 1000000007

vector<long> fac(MAX);
vector<long> finv(MAX);
vector<long> inv(MAX);

// template functions
/////////////////////////
void Cominit() {
  fac.at(0) = fac.at(1) = 1;
  finv.at(0) = finv.at(1) = 1;
  inv.at(1) = 1;
  for (long i = 2; i < MAX; i++) {
    fac.at(i) = (fac.at(i - 1) * i) % MOD;
    inv.at(i) = MOD - inv.at(MOD % i) * (MOD / i) % MOD;
    finv.at(i) = finv.at(i - 1) * inv.at(i) % MOD;
  }
}

long facComb(long N, long M) { // nCm%
  if (N < M)
    return 0;
  if (N < 0 || M < 0)
    return 0;
  return fac.at(N) * (finv.at(M) * finv.at(N - M) % MOD) % MOD;
}

long Comb(long N, long M) { // nCm
  long ans = 1;
  for (long i = N; i > N - M; i--) {
    ans = ans * i;
  }
  for (long i = M; i > 0; i--) {
    ans = ans / i;
  }
  return ans;
}

long Perm(long N, long M) { // nPm
  long ans = 1;
  for (long i = N; i > N - M; i--) {
    ans = ans * i;
  }
  return ans;
}

long GCM(long A, long B) {
  long W, r;
  if (A < B) {

    W = B;
    B = A;
    A = W;
  }
  while (A % B != 0) {
    r = A % B;
    A = B;
    B = r;
  }
  return B;
}
long LCM(long A, long B) { return (A * B) / GCM(A, B); }

long upDiv(long a, long b) { return (a + b - 1) / b; }

/////////////////////////

// Answer

int main() {

  typedef pair<long, long> P;
  deque<P> que;

  long H, W, Ch, Cw, Dh, Dw;
  cin >> H >> W;
  cin >> Ch >> Cw;
  cin >> Dh >> Dw;
  Ch = Ch - 1;
  Cw = Cw - 1;
  Dh = Dh - 1;
  Dw = Dw - 1;
  vector<vector<char>> S(H, vector<char>(W));
  vector<vector<long>> X(H, vector<long>(W, 1000000));
  vector<long> dx;
  vector<long> dy;
  dx = {0, 1, 0, -1};
  dy = {1, 0, -1, 0};
  vector<long> dxx;
  vector<long> dyy;
  dxx = {2, 2, 2, 2, 2, 1, 1, 1, 1, 0, 0, -1, -1, -1, -1, -2, -2, -2, -2, -2};
  dyy = {2, 1, 0, -1, -2, 2, 1, -1, -2, 2, -2, 2, 1, -1, -2, 2, 1, 0, -1, -2};
  for (long i = 0; i < H; i++) {
    for (long j = 0; j < W; j++) {
      cin >> S.at(i).at(j);
    }
  }
  P p = make_pair(Ch, Cw);
  que.push_back(p);
  X.at(Ch).at(Cw) = 0;
  while (que.size() != 0) {
    P pp = que.front();
    que.pop_front();
    if (pp.first == Dh && pp.second == Dw) {
      continue;
    }
    for (int i = 0; i < 4; i++) {
      long nx = pp.first + dx.at(i);
      long ny = pp.second + dy.at(i);
      if (0 <= nx && nx <= H - 1 && 0 <= ny && ny <= W - 1 &&
          S.at(nx).at(ny) != '#') {
        if (X.at(nx).at(ny) > X.at(pp.first).at(pp.second)) {
          X.at(nx).at(ny) = X.at(pp.first).at(pp.second);
          P np = make_pair(nx, ny);
          que.push_front(np);
        }
      }
    }
    for (long i = 0; i < 20; i++) {
      long nx = pp.first + dxx.at(i);
      long ny = pp.second + dyy.at(i);
      if (0 <= nx && nx <= H - 1 && 0 <= ny && ny <= W - 1 &&
          S.at(nx).at(ny) != '#') {
        if (X.at(nx).at(ny) > X.at(pp.first).at(pp.second) + 1) {
          X.at(nx).at(ny) = X.at(pp.first).at(pp.second) + 1;
          P np = make_pair(nx, ny);
          que.push_back(np);
        }
      }
    }
  }
  /*
for(long i=0;i<H;i++){
    for(long j=0;j<W;j++){
        cout<<X.at(i).at(j)<<endl;
    }
}
*/
  if (X.at(Dh).at(Dw) == 1000000) {
    X.at(Dh).at(Dw) = -1;
  }
  cout << X.at(Dh).at(Dw) << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define pi 2 * asin(1.0)
#define MAX 10000000
#define MOD 1000000007

vector<long> fac(MAX);
vector<long> finv(MAX);
vector<long> inv(MAX);

// template functions
/////////////////////////
void Cominit() {
  fac.at(0) = fac.at(1) = 1;
  finv.at(0) = finv.at(1) = 1;
  inv.at(1) = 1;
  for (long i = 2; i < MAX; i++) {
    fac.at(i) = (fac.at(i - 1) * i) % MOD;
    inv.at(i) = MOD - inv.at(MOD % i) * (MOD / i) % MOD;
    finv.at(i) = finv.at(i - 1) * inv.at(i) % MOD;
  }
}

long facComb(long N, long M) { // nCm%
  if (N < M)
    return 0;
  if (N < 0 || M < 0)
    return 0;
  return fac.at(N) * (finv.at(M) * finv.at(N - M) % MOD) % MOD;
}

long Comb(long N, long M) { // nCm
  long ans = 1;
  for (long i = N; i > N - M; i--) {
    ans = ans * i;
  }
  for (long i = M; i > 0; i--) {
    ans = ans / i;
  }
  return ans;
}

long Perm(long N, long M) { // nPm
  long ans = 1;
  for (long i = N; i > N - M; i--) {
    ans = ans * i;
  }
  return ans;
}

long GCM(long A, long B) {
  long W, r;
  if (A < B) {

    W = B;
    B = A;
    A = W;
  }
  while (A % B != 0) {
    r = A % B;
    A = B;
    B = r;
  }
  return B;
}
long LCM(long A, long B) { return (A * B) / GCM(A, B); }

long upDiv(long a, long b) { return (a + b - 1) / b; }

/////////////////////////

// Answer

int main() {

  typedef pair<long, long> P;
  queue<P> que;

  long H, W, Ch, Cw, Dh, Dw;
  cin >> H >> W;
  cin >> Ch >> Cw;
  cin >> Dh >> Dw;
  Ch = Ch - 1;
  Cw = Cw - 1;
  Dh = Dh - 1;
  Dw = Dw - 1;
  vector<vector<char>> S(H, vector<char>(W));
  vector<vector<long>> X(H, vector<long>(W, 1000000));
  vector<long> dx;
  vector<long> dy;
  dx = {0, 1, 0, -1};
  dy = {1, 0, -1, 0};
  vector<long> dxx;
  vector<long> dyy;
  dxx = {2, 2, 2, 2, 2, 1, 1, 1, 1, 0, 0, -1, -1, -1, -1, -2, -2, -2, -2, -2};
  dyy = {2, 1, 0, -1, -2, 2, 1, -1, -2, 2, -2, 2, 1, -1, -2, 2, 1, 0, -1, -2};
  for (long i = 0; i < H; i++) {
    for (long j = 0; j < W; j++) {
      cin >> S.at(i).at(j);
    }
  }
  P p = make_pair(Ch, Cw);
  que.push(p);
  X.at(Ch).at(Cw) = 0;
  while (que.size() != 0) {
    P pp = que.front();
    que.pop();
    if (pp.first == Dh && pp.second == Dw) {
      continue;
    }
    for (int i = 0; i < 4; i++) {
      long nx = pp.first + dx.at(i);
      long ny = pp.second + dy.at(i);
      if (0 <= nx && nx <= H - 1 && 0 <= ny && ny <= W - 1 &&
          S.at(nx).at(ny) != '#') {
        if (X.at(nx).at(ny) > X.at(pp.first).at(pp.second)) {
          X.at(nx).at(ny) = X.at(pp.first).at(pp.second);
          P np = make_pair(nx, ny);
          que.push(np);
        }
      }
    }
    for (long i = 0; i < 20; i++) {
      long nx = pp.first + dxx.at(i);
      long ny = pp.second + dyy.at(i);
      if (0 <= nx && nx <= H - 1 && 0 <= ny && ny <= W - 1 &&
          S.at(nx).at(ny) != '#') {
        if (X.at(nx).at(ny) > X.at(pp.first).at(pp.second) + 1) {
          X.at(nx).at(ny) = X.at(pp.first).at(pp.second) + 1;
          P np = make_pair(nx, ny);
          que.push(np);
        }
      }
    }
  }
  /*
for(long i=0;i<H;i++){
    for(long j=0;j<W;j++){
        cout<<X.at(i).at(j)<<endl;
    }
}
*/
  if (X.at(Dh).at(Dw) == 1000000) {
    X.at(Dh).at(Dw) = -1;
  }
  cout << X.at(Dh).at(Dw) << endl;
}