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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

vector<int> dw = {1, 0, -1, 0};
vector<int> dh = {0, 1, 0, -1};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  ll H;
  cin >> H;
  ll W;
  cin >> W;
  ll Ch;
  cin >> Ch;
  Ch--;
  ll Cw;
  cin >> Cw;
  Cw--;
  ll Dh;
  cin >> Dh;
  Dh--;
  ll Dw;
  cin >> Dw;
  Dw--;
  vector<vector<ll>> cells(H, vector<ll>(W, 0));
  vector<vector<ll>> times(H, vector<ll>(W, 1e15));
  for (ll i = 0; i < H; i++) {
    string S;
    cin >> S;
    for (ll j = 0; j < W; j++) {
      if (S[j] == '#')
        cells[i][j] = 1;
    }
  }

  deque<pair<ll, ll>> q;
  q.push_back({Ch, Cw});
  times[Ch][Cw] = 0;

  while (!q.empty()) {
    pair<ll, ll> p = q.front();
    q.pop_front();
    ll ch = p.first, cw = p.second;
    for (ll i = 0; i < 4; i++) {
      ll nh = ch + dh[i];
      ll nw = cw + dw[i];
      if (nh < 0 || nw < 0 || nh >= H || nw >= W)
        continue;
      if (cells[nh][nw] > 0)
        continue;
      if (times[nh][nw] <= times[ch][cw])
        continue;
      times[nh][nw] = times[ch][cw];
      q.push_front({nh, nw});
    }

    for (ll i = -2; i <= 2; i++) {
      for (ll j = -2; j <= 2; j++) {
        ll nh = ch + i;
        ll nw = cw + j;
        if (nh < 0 || nw < 0 || nh >= H || nw >= W)
          continue;
        if (cells[nh][nw] > 0)
          continue;
        if (times[nh][nw] <= times[ch][cw] + 1)
          continue;
        times[nh][nw] = times[ch][cw] + 1;
        q.push_back({nh, nw});
      }
    }
  }

  ll ans = times[Dh][Dw];
  if (ans >= 1e15)
    ans = -1;
  cout << ans << '\\n';
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

vector<int> dw = {1, 0, -1, 0};
vector<int> dh = {0, 1, 0, -1};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  ll H;
  cin >> H;
  ll W;
  cin >> W;
  ll Ch;
  cin >> Ch;
  Ch--;
  ll Cw;
  cin >> Cw;
  Cw--;
  ll Dh;
  cin >> Dh;
  Dh--;
  ll Dw;
  cin >> Dw;
  Dw--;
  vector<vector<ll>> cells(H, vector<ll>(W, 0));
  vector<vector<ll>> times(H, vector<ll>(W, 1e15));
  for (ll i = 0; i < H; i++) {
    string S;
    cin >> S;
    for (ll j = 0; j < W; j++) {
      if (S[j] == '#')
        cells[i][j] = 1;
    }
  }

  queue<pair<ll, ll>> q;
  q.push(make_pair(Ch, Cw));
  times[Ch][Cw] = 0;
  ll m = 0;

  while (!q.empty()) {
    pair<ll, ll> p = q.front();
    q.pop();
    queue<pair<ll, ll>> q2 = q;
    q2.push(p);

    set<pair<ll, ll>> s;

    while (!q2.empty()) {
      pair<ll, ll> p = q2.front();
      q2.pop();
      ll ch = p.first, cw = p.second;

      for (ll k = -2; k < 3; k++) {
        for (ll j = -2; j < 3; j++) {
          ll nnh = ch + k;
          ll nnw = cw + j;
          if (nnh < 0 || nnw < 0 || nnh >= H || nnw >= W)
            continue;
          if (times[nnh][nnw] <= m)
            continue;
          if (cells[nnh][nnw] > 0)
            continue;
          s.insert(make_pair(nnh, nnw));
        }
      }

      for (ll i = 0; i < 4; i++) {
        ll nh = ch + dh[i];
        ll nw = cw + dw[i];
        if (nh < 0 || nw < 0 || nh >= H || nw >= W)
          continue;
        if (times[nh][nw] <= times[ch][cw])
          continue;
        if (cells[nh][nw] > 0)
          continue;
        times[nh][nw] = times[ch][cw];
        q2.push(make_pair(nh, nw));

        for (ll k = -2; k < 3; k++) {
          for (ll j = -2; j < 3; j++) {
            if (k == 0 && j == 0)
              continue;
            ll nnh = ch + k;
            ll nnw = cw + j;
            if (nnh < 0 || nnw < 0 || nnh >= H || nnw >= W)
              continue;
            if (times[nnh][nnw] <= m)
              continue;
            if (cells[nnh][nnw] > 0)
              continue;
            s.insert(make_pair(nnh, nnw));
          }
        }
      }
    }

    for (auto p : s) {
      ll nh = p.first, nw = p.second;
      if (times[nh][nw] > m + 1) {
        times[nh][nw] = m + 1;
        q.push(make_pair(nh, nw));
      }
    }
    m++;
  }

  ll ans = times[Dh][Dw];
  if (ans >= 1e15)
    ans = -1;
  cout << ans << '\\n';
  return 0;
}