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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
// memo  fixed  setprecision(20);
using vvll = vector<vector<ll>>;
ll mod = 1e9 + 7;
/*"itob" int to "N"base */ template <typename TypeInt>
string itob(const TypeInt v, int base) {
  static const char table[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  string ret;
  static numeric_limits<TypeInt> t;
  TypeInt n = v;
  if (t.is_signed) {
    if (v < 0)
      n *= -1;
  }
  while (n >= base) {
    ret += table[n % base];
    n /= base;
  }
  ret += table[n];
  if (t.is_signed) {
    if (v < 0 && base == 10)
      ret += '-';
  }
  reverse(ret.begin(), ret.end());
  return ret;
}
/*"chmin" a = MAX*/ template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
/*"chmax" a = MIN*/ template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
/*"ctoi"  char to int*/ int ctoi(char c) { return c - '0'; }
/*"gcd"   MAX Euclidean */ ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
/*"lcm"   MIN*/ ll lcm(ll a, ll b) {
  ll g = gcd(a, b);
  return a / g * b;
}
/*"primecheck"If prime,return true.*/ bool primecheck(ll n) {
  if (n < 2)
    return false;
  else {
    for (int i = 2; i * i <= n; i++) {
      if (n % i == 0)
        return false;
    }
    return true;
  }
}
string reverserange(string s, ll a, ll b) {
  reverse(s.begin() + a - 1, s.begin() + b);
  return s;
}
ll modpow(ll a, ll n, ll mod) {
  ll res = 1;
  while (n > 0) {
    if (n % 2 == 1) {
      res = res * a % mod;
    }
    a = a * a % mod;
    n /= 2;
  }
  return res;
}
int main() {
  ll h, w, ch, cw, dh, dw;
  cin >> h >> w >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  char ma[h][w];
  bool dd[h][w];
  ll ans[h][w];
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> ma[i][j];
      ans[i][j] = 1e17;
      dd[i][j] = false;
    }
  }

  ans[ch][cw] = 0;
  queue<pair<ll, ll>> que, check;
  que.push(make_pair(ch, cw));

  ll dx[4] = {1, -1, 0, 0};
  ll dy[4] = {0, 0, -1, 1};
  while (!que.empty()) {
    ll ue = que.front().first;
    ll hida = que.front().second;
    que.pop();
    check.push(make_pair(ue, hida));
    ll nue, nhida;
    for (int i = 0; i < 4; i++) {
      nue = ue + dy[i], nhida = hida + dx[i];
      if (nue < 0 || nhida < 0 || nue >= h || nhida >= w)
        continue;
      if (ma[nue][nhida] == '#')
        continue;
      if (ans[nue][nhida] > ans[ue][hida]) {
        ans[nue][nhida] = ans[ue][hida];
        que.push(make_pair(nue, nhida));
      }
    }
    if (que.empty()) {
      while (!check.empty()) {
        ue = check.front().first;
        hida = check.front().second;
        check.pop();
        for (int l = 0; l < 5; l++) {
          for (int ww = 0; ww < 5; ww++) {
            nue = ue + l - 2, nhida = hida + ww - 2;
            if (nue < 0 || nhida < 0 || nue >= h || nhida >= w)
              continue;
            if (ma[nue][nhida] == '#')
              continue;
            if (ans[nue][nhida] > ans[ue][hida] + 1) {
              ans[nue][nhida] = ans[ue][hida] + 1;
              que.push(make_pair(nue, nhida));
            }
          }
        }
      }
    }
  }

  if (ans[dh][dw] == 1e17) {
    cout << -1 << endl;
  } else {
    cout << ans[dh][dw] << endl;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
// memo  fixed  setprecision(20);
using vvll = vector<vector<ll>>;
ll mod = 1e9 + 7;
/*"itob" int to "N"base */ template <typename TypeInt>
string itob(const TypeInt v, int base) {
  static const char table[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  string ret;
  static numeric_limits<TypeInt> t;
  TypeInt n = v;
  if (t.is_signed) {
    if (v < 0)
      n *= -1;
  }
  while (n >= base) {
    ret += table[n % base];
    n /= base;
  }
  ret += table[n];
  if (t.is_signed) {
    if (v < 0 && base == 10)
      ret += '-';
  }
  reverse(ret.begin(), ret.end());
  return ret;
}
/*"chmin" a = MAX*/ template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
/*"chmax" a = MIN*/ template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
/*"ctoi"  char to int*/ int ctoi(char c) { return c - '0'; }
/*"gcd"   MAX Euclidean */ ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
/*"lcm"   MIN*/ ll lcm(ll a, ll b) {
  ll g = gcd(a, b);
  return a / g * b;
}
/*"primecheck"If prime,return true.*/ bool primecheck(ll n) {
  if (n < 2)
    return false;
  else {
    for (int i = 2; i * i <= n; i++) {
      if (n % i == 0)
        return false;
    }
    return true;
  }
}
string reverserange(string s, ll a, ll b) {
  reverse(s.begin() + a - 1, s.begin() + b);
  return s;
}
ll modpow(ll a, ll n, ll mod) {
  ll res = 1;
  while (n > 0) {
    if (n % 2 == 1) {
      res = res * a % mod;
    }
    a = a * a % mod;
    n /= 2;
  }
  return res;
}
int main() {
  ll h, w, ch, cw, dh, dw;
  cin >> h >> w >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  char ma[h][w];
  ll ans[h][w];
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> ma[i][j];
      ans[i][j] = 1e17;
    }
  }

  ans[ch][cw] = 0;
  queue<pair<ll, ll>> que, check;
  que.push(make_pair(ch, cw));
  check.push(make_pair(ch, cw));
  ll dx[4] = {1, -1, 0, 0};
  ll dy[4] = {0, 0, -1, 1};
  while (!que.empty()) {
    ll ue = que.front().first;
    ll hida = que.front().second;
    que.pop();
    ll nue, nhida;
    for (int i = 0; i < 4; i++) {
      nue = ue + dy[i], nhida = hida + dx[i];
      if (nue < 0 || nhida < 0 || nue >= h || nhida >= w)
        continue;
      if (ma[nue][nhida] == '#')
        continue;
      if (ans[nue][nhida] > ans[ue][hida]) {
        ans[nue][nhida] = ans[ue][hida];
        que.push(make_pair(nue, nhida));
        check.push(make_pair(nue, nhida));
      }
    }
    if (que.empty()) {
      while (!check.empty()) {
        ue = check.front().first;
        hida = check.front().second;
        check.pop();
        for (int l = 0; l < 5; l++) {
          for (int ww = 0; ww < 5; ww++) {
            nue = ue + l - 2, nhida = hida + ww - 2;
            if (nue < 0 || nhida < 0 || nue >= h || nhida >= w)
              continue;
            if (ma[nue][nhida] == '#')
              continue;
            if (ans[nue][nhida] > ans[ue][hida] + 1) {
              ans[nue][nhida] = ans[ue][hida] + 1;
              que.push(make_pair(nue, nhida));
              check.push(make_pair(nue, nhida));
            }
          }
        }
      }
    }
  }

  if (ans[dh][dw] == 1e17) {
    cout << -1 << endl;
  } else {
    cout << ans[dh][dw] << endl;
  }
}