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

#include "bits/stdc++.h"
using namespace std;
const int MAX = 700000;
const int MOD = 1000000007;

long long fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

// 二項係数計算
long long COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
/*第二引数で第一引数を割ったときの切り上げの計算*/
long long int maxtime(long long int x, long long int y) {
  return (x + y - 1) / y;
}
/*最大公約数*/
long long int lcm(long long int number1, long long int number2) {
  long long int m = number1;
  long long int n = number2;

  if (number2 > number1) {
    m = number2;
    n = number1;
  }
  long long int s = -1;
  while (s != 0) {
    s = m % n;
    m = n;
    n = s;
  }
  return m;
}
/*最大公倍数*/
long long int gcd(long long int number1, long long int number2) {
  long long int m = number1;
  long long int n = number2;
  return m / lcm(m, n) * n;
}
/*逆元計算*/
long long int modinv(long long a, long long m) {
  long long int b = m, u = 1, v = 0;
  while (b) {
    long long int t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= m;
  if (u < 0)
    u += m;
  return u;
}
// index が条件を満たすかどうか
vector<long long int> meguru;
bool isOK(int index, int key) {
  if (meguru[index] >= key)
    return true;
  else
    return false;
}
// 汎用的な二分探索のテンプレ
int binary_search(int key) {
  int left = -1; // 「index = 0」が条件を満たすこともあるので、初期値は -1
  int right = (int)meguru.size(); // 「index =
                                  // a.size()-1」が条件を満たさないこともあるので、初期値は
                                  // a.size()
  /* どんな二分探索でもここの書き方を変えずにできる！ */
  while (right - left > 1) {
    int mid = left + (right - left) / 2;

    if (isOK(mid, key))
      right = mid;
    else
      left = mid;
  }
  /* left は条件を満たさない最大の値、right は条件を満たす最小の値になっている
   */
  return right;
}
long long modpow(long long a, long long n, long long mod) {
  long long res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}
char masu[1030][1030] = {};
int sinko[1030][1030] = {};
int o[1030][1030] = {};
queue<pair<int, int>> q;
queue<pair<int, int>> s;
int main() {
  int h, w;
  cin >> h >> w;
  int ch, cw, dh, dw;
  cin >> ch >> cw >> dh >> dw;
  int x[4] = {1, -1, 0, 0};
  int y[4] = {0, 0, 1, -1};
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      char a;
      cin >> a;
      masu[i][j] = a;
    }
  }
  q.push(make_pair(cw - 1, ch - 1));
  sinko[ch - 1][cw - 1] = 1;
  o[ch - 1][cw - 1] = 1;
  int b = 1;
  while (b == 1) {
    b = 0;
    while (q.size()) {
      int x1, y1;
      x1 = q.front().first;
      y1 = q.front().second;
      s.push(make_pair(x1, y1));
      q.pop();
      for (int i = 0; i < 4; i++) {
        if (x1 + x[i] >= 0 && x1 + x[i] < w && y1 + y[i] >= 0 &&
            y1 + y[i] < h) {
          if (masu[y1 + y[i]][x1 + x[i]] != '#') {
            if (sinko[y1 + y[i]][x1 + x[i]] == 0) {
              q.push(make_pair(x1 + x[i], y1 + y[i]));
              s.push(make_pair(x1 + x[i], y1 + y[i]));
              sinko[y1 + y[i]][x1 + x[i]] = 1;
              o[y1 + y[i]][x1 + x[i]] = o[y1][x1];
            }
          }
        }
      }
    }
    while (s.size()) {
      int i = s.front().second;
      int j = s.front().first;
      s.pop();
      for (int k = -2; k <= 2; k++) {
        for (int l = -2; l <= 2; l++) {
          if (i + k >= 0 && i + k < h && j + l >= 0 && j + l < w &&
              sinko[i + k][j + l] == 0 && masu[i + k][j + l] != '#') {
            q.push(make_pair(j + l, i + k));
            o[i + k][j + l] = o[i][j] + 1;
            sinko[i + k][j + l] = 1;
            b = 1;
          }
        }
      }
    }
  }

  if (o[dh - 1][dw - 1] == 0) {
    cout << -1;
  } else {
    cout << o[dh - 1][dw - 1] - 1;
  }
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
const int MAX = 700000;
const int MOD = 1000000007;

long long fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

// 二項係数計算
long long COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
/*第二引数で第一引数を割ったときの切り上げの計算*/
long long int maxtime(long long int x, long long int y) {
  return (x + y - 1) / y;
}
/*最大公約数*/
long long int lcm(long long int number1, long long int number2) {
  long long int m = number1;
  long long int n = number2;

  if (number2 > number1) {
    m = number2;
    n = number1;
  }
  long long int s = -1;
  while (s != 0) {
    s = m % n;
    m = n;
    n = s;
  }
  return m;
}
/*最大公倍数*/
long long int gcd(long long int number1, long long int number2) {
  long long int m = number1;
  long long int n = number2;
  return m / lcm(m, n) * n;
}
/*逆元計算*/
long long int modinv(long long a, long long m) {
  long long int b = m, u = 1, v = 0;
  while (b) {
    long long int t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= m;
  if (u < 0)
    u += m;
  return u;
}
// index が条件を満たすかどうか
vector<long long int> meguru;
bool isOK(int index, int key) {
  if (meguru[index] >= key)
    return true;
  else
    return false;
}
// 汎用的な二分探索のテンプレ
int binary_search(int key) {
  int left = -1; // 「index = 0」が条件を満たすこともあるので、初期値は -1
  int right = (int)meguru.size(); // 「index =
                                  // a.size()-1」が条件を満たさないこともあるので、初期値は
                                  // a.size()
  /* どんな二分探索でもここの書き方を変えずにできる！ */
  while (right - left > 1) {
    int mid = left + (right - left) / 2;

    if (isOK(mid, key))
      right = mid;
    else
      left = mid;
  }
  /* left は条件を満たさない最大の値、right は条件を満たす最小の値になっている
   */
  return right;
}
long long modpow(long long a, long long n, long long mod) {
  long long res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}
char masu[1030][1030] = {};
int sinko[1030][1030] = {};
int o[1030][1030] = {};
int main() {
  int h, w;
  cin >> h >> w;
  int ch, cw, dh, dw;
  cin >> ch >> cw >> dh >> dw;
  int x[4] = {1, -1, 0, 0};
  int y[4] = {0, 0, 1, -1};
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      char a;
      cin >> a;
      masu[i][j] = a;
    }
  }
  queue<pair<int, int>> q;
  q.push(make_pair(cw - 1, ch - 1));
  sinko[ch - 1][cw - 1] = 1;
  o[ch - 1][cw - 1] = 1;
  int sum = 1;
  int b = 1;
  while (b == 1) {
    b = 0;
    while (q.size()) {
      int x1, y1;
      x1 = q.front().first;
      y1 = q.front().second;
      q.pop();
      for (int i = 0; i < 4; i++) {
        if (x1 + x[i] >= 0 && x1 + x[i] < w && y1 + y[i] >= 0 &&
            y1 + y[i] < h) {
          if (masu[y1 + y[i]][x1 + x[i]] != '#') {
            if (sinko[y1 + y[i]][x1 + x[i]] == 0) {
              q.push(make_pair(x1 + x[i], y1 + y[i]));
              sinko[y1 + y[i]][x1 + x[i]] = sum;
              o[y1 + y[i]][x1 + x[i]] = o[y1][x1];
            }
          }
        }
      }
    }

    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (sinko[i][j] == sum) {
          for (int k = -2; k <= 2; k++) {
            for (int l = -2; l <= 2; l++) {
              if (i + k >= 0 && i + k < h && j + l >= 0 && j + l < w &&
                  sinko[i + k][j + l] == 0 && masu[i + k][j + l] != '#') {
                q.push(make_pair(j + l, i + k));
                o[i + k][j + l] = o[i][j] + 1;
                sinko[i + k][j + l] = sum + 1;
                b = 1;
              }
            }
          }
        }
      }
    }
    sum++;
  }

  if (o[dh - 1][dw - 1] == 0) {
    cout << -1;
  } else {
    cout << o[dh - 1][dw - 1] - 1;
  }
}