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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
ll ans = 0;
ll h, w;
const ll INF = 1LL << 60; // MAX 9223372036854775807

bool inarea(ll x, ll y) { return x >= 0 && y >= 0 && x < h && y < w; }
ll dx[4] = {0, 1, 0, -1};
ll dy[4] = {1, 0, -1, 0};
int main() {
  cin >> h >> w;
  vector<vector<bool>> v(h, vector<bool>(w, 0));
  ll sh, sw;
  cin >> sh >> sw;
  ll gh, gw;
  cin >> gh >> gw;
  rep(i, h) rep(j, w) {
    char t;
    cin >> t;
    if (t == '#')
      v[i][j] = 1;
  }
  vector<vector<ll>> d(h, vector<ll>(w, INF));
  d[sh - 1][sw - 1] = 0;
  queue<pair<ll, ll>> q, qq;
  q.push({sh - 1, sw - 1});
  qq.push({sh - 1, sw - 1});

  while (1) {
    while (!q.empty()) {
      auto p = q.front();
      q.pop();
      qq.push(p);
      rep(i, 4) {
        if (inarea(p.first + dx[i], p.second + dy[i]) &&
            !v[p.first + dx[i]][p.second + dy[i]] &&
            d[p.first + dx[i]][p.second + dy[i]] > d[p.first][p.second]) {
          d[p.first + dx[i]][p.second + dy[i]] = d[p.first][p.second];
          q.push({p.first + dx[i], p.second + dy[i]});
          qq.push({p.first + dx[i], p.second + dy[i]});
        }
      }
    }
    while (!qq.empty()) {
      auto p = qq.front();
      qq.pop();
      rep(i, 25) {
        if (inarea(p.first + (i % 5) - 2, p.second + (i / 5) - 2) &&
            !v[p.first + (i % 5) - 2][p.second + (i / 5) - 2] &&
            d[p.first + (i % 5) - 2][p.second + (i / 5) - 2] >
                d[p.first][p.second] + 1) {
          d[p.first + (i % 5) - 2][p.second + (i / 5) - 2] =
              d[p.first][p.second] + 1;
          q.push({p.first + (i % 5) - 2, p.second + (i / 5) - 2});
        }
      }
    }
    if (q.empty())
      break;
  }
  if (d[gh - 1][gw - 1] == INF)
    d[gh - 1][gw - 1] = -1;
  cout << d[gh - 1][gw - 1] << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
ll ans = 0;
ll h, w;
const ll INF = 1LL << 60; // MAX 9223372036854775807

bool inarea(ll x, ll y) { return x >= 0 && y >= 0 && x < h && y < w; }
ll dx[4] = {0, 1, 0, -1};
ll dy[4] = {1, 0, -1, 0};
int main() {
  cin >> h >> w;
  vector<vector<bool>> v(h, vector<bool>(w, 0));
  ll sh, sw;
  cin >> sh >> sw;
  ll gh, gw;
  cin >> gh >> gw;
  rep(i, h) rep(j, w) {
    char t;
    cin >> t;
    if (t == '#')
      v[i][j] = 1;
  }
  vector<vector<ll>> d(h, vector<ll>(w, INF));
  d[sh - 1][sw - 1] = 0;
  queue<pair<ll, ll>> q, qq;
  q.push({sh - 1, sw - 1});
  qq.push({sh - 1, sw - 1});

  while (1) {
    while (!q.empty()) {
      auto p = q.front();
      q.pop();
      rep(i, 4) {
        if (inarea(p.first + dx[i], p.second + dy[i]) &&
            !v[p.first + dx[i]][p.second + dy[i]] &&
            d[p.first + dx[i]][p.second + dy[i]] > d[p.first][p.second]) {
          d[p.first + dx[i]][p.second + dy[i]] = d[p.first][p.second];
          q.push({p.first + dx[i], p.second + dy[i]});
          qq.push({p.first + dx[i], p.second + dy[i]});
        }
      }
    }
    while (!qq.empty()) {
      auto p = qq.front();
      qq.pop();
      rep(i, 25) {
        if (inarea(p.first + (i % 5) - 2, p.second + (i / 5) - 2) &&
            !v[p.first + (i % 5) - 2][p.second + (i / 5) - 2] &&
            d[p.first + (i % 5) - 2][p.second + (i / 5) - 2] >
                d[p.first][p.second] + 1) {
          d[p.first + (i % 5) - 2][p.second + (i / 5) - 2] =
              d[p.first][p.second] + 1;
          q.push({p.first + (i % 5) - 2, p.second + (i / 5) - 2});
          qq.push({p.first + (i % 5) - 2, p.second + (i / 5) - 2});
        }
      }
    }
    if (q.empty())
      break;
  }
  if (d[gh - 1][gw - 1] == INF)
    d[gh - 1][gw - 1] = -1;
  cout << d[gh - 1][gw - 1] << endl;
  return 0;
}