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

#include <bits/stdc++.h>
#define INF 2000000000000000000
#define ll long long
#define pb push_back
using namespace std;

ll H, W;
bool check(ll h, ll w) {
  if (h < 0 || h >= H || w < 0 || w >= W) {
    return false;
  }
  return true;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> H >> W;
  ll ch, cw;
  cin >> ch >> cw;
  ch -= 1;
  cw -= 1;
  ll dh, dw;
  cin >> dh >> dw;
  dh -= 1;
  dw -= 1;
  vector<vector<ll>> move = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}};
  vector<vector<char>> maze(H, vector<char>(W));
  for (ll i = 0; i < H; ++i) {
    for (ll j = 0; j < W; ++j) {
      cin >> maze.at(i).at(j);
    }
  }
  vector<vector<ll>> dp(H, vector<ll>(W, INF));
  dp.at(ch).at(cw) = 0;
  set<vector<ll>> q;
  q.insert({0, ch, cw});
  while (!q.empty()) {
    vector<ll> now = *q.begin();
    ll h = now.at(1), w = now.at(2);
    q.erase(q.begin());
    if (now.at(0) > dp.at(h).at(w)) {
      continue;
    }
    for (ll i = 0; i < 4; ++i) {
      ll nh = h + move.at(i).at(0), nw = w + move.at(i).at(1);
      if (!check(nh, nw)) {
        continue;
      }
      if (maze.at(nh).at(nw) == '#') {
        continue;
      }
      if (dp.at(nh).at(nw) > dp.at(h).at(w)) {
        q.insert({dp.at(h).at(w), nh, nw});
        dp.at(nh).at(nw) = dp.at(h).at(w);
      }
    }
    for (ll i = -2; i <= 2; ++i) {
      for (ll j = -2; j <= 2; ++j) {
        ll nh = h + i, nw = w + j;
        if (!check(nh, nw)) {
          continue;
        }
        if (maze.at(nh).at(nw) == '#') {
          continue;
        }
        if (dp.at(nh).at(nw) > dp.at(h).at(w) + 1) {
          q.insert({dp.at(h).at(w) + 1, nh, nw});
          dp.at(nh).at(nw) = dp.at(h).at(w) + 1;
        }
      }
    }
  }
  // for (ll i = 0; i < H; ++i) {
  //   for (ll j = 0; j < W; ++j) {
  //     cout << dp.at(i).at(j) << " ";
  //   }
  //   cout << "\\n";
  // }
  if (dp.at(dh).at(dw) == INF) {
    cout << -1 << "\\n";
  } else {
    cout << dp.at(dh).at(dw) << "\\n";
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define INF 2000000000000000000
#define ll long long
#define pb push_back
using namespace std;

ll H, W;
bool check(ll h, ll w) {
  if (h < 0 || h >= H || w < 0 || w >= W) {
    return false;
  }
  return true;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> H >> W;
  ll ch, cw;
  cin >> ch >> cw;
  ch -= 1;
  cw -= 1;
  ll dh, dw;
  cin >> dh >> dw;
  dh -= 1;
  dw -= 1;
  vector<vector<char>> maze(H, vector<char>(W));
  for (ll i = 0; i < H; ++i) {
    for (ll j = 0; j < W; ++j) {
      cin >> maze.at(i).at(j);
    }
  }
  vector<vector<ll>> dp(H, vector<ll>(W, INF));
  dp.at(ch).at(cw) = 0;
  queue<vector<ll>> q;
  q.push({ch, cw});
  vector<vector<ll>> move = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}};
  while (!q.empty()) {
    vector<ll> now = q.front();
    ll h = now.at(0), w = now.at(1);
    q.pop();
    for (ll i = 0; i < 4; ++i) {
      ll nh = h + move.at(i).at(0), nw = w + move.at(i).at(1);
      if (!check(nh, nw)) {
        continue;
      }
      if (maze.at(nh).at(nw) == '#') {
        continue;
      }
      if (dp.at(nh).at(nw) > dp.at(h).at(w)) {
        q.push({nh, nw});
        dp.at(nh).at(nw) = dp.at(h).at(w);
      }
    }
    for (ll i = -2; i <= 2; ++i) {
      for (ll j = -2; j <= 2; ++j) {
        ll nh = h + i, nw = w + j;
        if (!check(nh, nw)) {
          continue;
        }
        if (maze.at(nh).at(nw) == '#') {
          continue;
        }
        if (dp.at(nh).at(nw) > dp.at(h).at(w) + 1) {
          q.push({nh, nw});
          dp.at(nh).at(nw) = dp.at(h).at(w) + 1;
        }
      }
    }
  }
  // for (ll i = 0; i < H; ++i) {
  //   for (ll j = 0; j < W; ++j) {
  //     cout << dp.at(i).at(j) << " ";
  //   }
  //   cout << "\\n";
  // }
  if (dp.at(dh).at(dw) == INF) {
    cout << -1 << "\\n";
  } else {
    cout << dp.at(dh).at(dw) << "\\n";
  }
}