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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define uu unsigned
#define io                                                                     \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define MOD 1000000007
#define pi pair<int, int>
#define pl pair<ll, ll>
#define mkp make_pair
#define ss second
#define ff first
#define all(x) x.begin(), x.end()
#define vi vector<int>
#define vl vector<ll>
#define vb vector<bool>
#define pb push_back
#define pf push_front
#define printcas cout << "Case " << cas << ": "
#define _ continue;
#define endl '\\n'
#define __                                                                     \\
  cout << endl;                                                                \\
  continue;
#define vc4d(nam, type, a, b, c, d, fix)                                       \\
  vector<vector<vector<vector<type>>>> nam(                                    \\
      a, vector<vector<vector<type>>>(                                         \\
             b, vector<vector<type>>(c, vector<type>(d, fix))))
const ll oo = (1ll << 60);
ll h, w, ch, cw, dh, dw;
ll X[] = {-1, 1, 0, 0}, Y[] = {0, 0, 1, -1},
   xxx[] = {-1, -1, 1, 1, -2, -2, -2, -2, -2, -1,
            -1, 0,  0, 1, 1,  2,  2,  2,  2,  2},
   yyy[] = {1, -1, -1, 1,  -2, -1, 0,  1, 2, -2,
            2, -2, 2,  -2, 2,  -2, -1, 0, 1, 2};
bool safe(ll x, ll y) {
  if (x > -1 and y > -1 and x < h and y < w)
    return true;
  return false;
}
int main() {
  io;
  int T = 1;
  // T=gg();
  // cin>>T;
  // for(int cas=1;cas<=T;cas++)
  ll i, j, k, l, cnt = 0;
  cin >> h >> w >> ch >> cw >> dh >> dw;
  vector<string> v(h);
  for (auto &x : v)
    cin >> x;
  ch--, cw--, dh--, dw--;
  queue<pl> qq({{ch, cw}}), pp;
  v[ch][cw] = '#';
  while (true) {
    if (!qq.empty()) {
      while (!qq.empty()) {
        pl u = qq.front();
        if (u.ff == dh and u.ss == dw) {
          cout << cnt;
          return 0;
        }
        pp.push(u);
        qq.pop();
        for (i = 0; i < 4; i++) {
          ll x = u.ff + X[i], y = u.ss + Y[i];
          if (safe(x, y) and v[x][y] == '.') {
            v[x][y] = '#';
            qq.push({x, y});
          }
        }
      }
    } else if (!pp.empty()) {
      while (!pp.empty()) {
        pl u = pp.front();
        pp.pop();
        for (i = 0; i < 20; i++) {
          ll x = u.ff + xxx[i], y = u.ss + yyy[i];
          if (safe(x, y) and v[x][y] == '.') {
            v[x][y] = '#';
            qq.push({x, y});
          }
        }
      }
      cnt++;
    }
    if (pp.empty() && qq.empty()) {
      cout << -1;
      return 0;
    }
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define uu unsigned
#define io                                                                     \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define MOD 1000000007
#define pi pair<int, int>
#define pl pair<ll, ll>
#define mkp make_pair
#define ss second
#define ff first
#define all(x) x.begin(), x.end()
#define vi vector<int>
#define vl vector<ll>
#define vb vector<bool>
#define pb push_back
#define pf push_front
#define printcas cout << "Case " << cas << ": "
#define _ continue;
#define endl '\\n'
#define __                                                                     \\
  cout << endl;                                                                \\
  continue;
#define vc4d(nam, type, a, b, c, d, fix)                                       \\
  vector<vector<vector<vector<type>>>> nam(                                    \\
      a, vector<vector<vector<type>>>(                                         \\
             b, vector<vector<type>>(c, vector<type>(d, fix))))
const ll oo = (1ll << 60);

ll h, w, ch, cw, dh, dw;
ll X[] = {-1, 1, 0, 0}, Y[] = {0, 0, 1, -1},
   xxx[] = {-1, -1, 1, 1, -2, -2, -2, -2, -2, -1,
            -1, 0,  0, 1, 1,  2,  2,  2,  2,  2},
   yyy[] = {1, -1, -1, 1,  -2, -1, 0,  1, 2, -2,
            2, -2, 2,  -2, 2,  -2, -1, 0, 1, 2};
bool safe(ll x, ll y) {
  if (x > -1 and y > -1 and x < h and y < w)
    return true;
  return false;
}
int main() {
  io;
  ll i, j, k, l, cnt = 0;
  cin >> h >> w >> ch >> cw >> dh >> dw;
  vector<string> v(h);
  for (auto &x : v)
    cin >> x;
  ch--, cw--, dh--, dw--;
  queue<pl> qq({{ch, cw}}), pp;
  v[ch][cw] = '#';
  while (true) {
    while (!qq.empty()) {
      pl u = qq.front();
      if (u.ff == dh and u.ss == dw) {
        cout << cnt;
        return 0;
      }
      pp.push(u);
      qq.pop();
      for (i = 0; i < 4; i++) {
        ll x = u.ff + X[i], y = u.ss + Y[i];
        if (safe(x, y) and v[x][y] == '.') {
          v[x][y] = '#';
          qq.push({x, y});
        }
      }
    }
    while (!pp.empty()) {
      pl u = pp.front();
      pp.pop();
      for (i = 0; i < 20; i++) {
        ll x = u.ff + xxx[i], y = u.ss + yyy[i];
        if (safe(x, y) and v[x][y] == '.') {
          v[x][y] = '#';
          qq.push({x, y});
        }
      }
    }
    if (pp.empty() && !qq.empty())
      cnt++;
    if (!pp.empty() && !qq.empty()) {
      cout << -1 << endl;
      return 0;
    }
  }

  return 0;
}