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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define Rep(i, a, b) for (ll i = a; i <= b; i++)
#define repr(i, a, b) for (ll i = b - 1; i >= a; i--)
#define _GLIBCXX_DEBUG
#define Vl vector<ll>
#define Vs vector<string>
#define Vp vector<pair<ll, ll>>
using ll = long long;
#define ALL(v) (v).begin(), (v).end()
#define endl "\\n"
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define co(x) cout << x << endl
#define coel cout << endl
#define pb push_back
#define sz(v) ((ll)(v).size())
const double pi = acos(-1.0);
const ll MOD = 1e9 + 7;
// const ll INF = 1LL << 60;
const ll INF = 1000000000;
#define pp pair<ll, pair<ll, ll>>
#define fi first
#define se second

////////////////////////////////////////////////////////

ll dy[] = {0, 1, 0, -1};
ll dx[] = {1, 0, -1, 0};

ll H, W;
string s[1010];
ll F[1010][1010];
ll sy, sx, gy, gx;

void bfs() {
  rep(i, 0, 1010) rep(j, 0, 1010) F[i][j] = INF;

  deque<pair<ll, ll>> qu;
  qu.push_front({sy, sx});
  F[sy][sx] = 0;
  while (!qu.empty()) {
    ll y, x;
    y = qu.front().first, x = qu.front().second;
    qu.pop_front();
    rep(i, 0, 4) {
      ll ny, nx;
      ny = y + dy[i], nx = x + dx[i];
      if (ny < 0 || ny >= H || nx < 0 || nx >= W)
        continue;
      if (s[ny][nx] == '#')
        continue;
      if (F[ny][nx] <= F[y][x])
        continue;
      chmin(F[ny][nx], F[y][x]);
      qu.push_front({ny, nx});
    }
    Rep(i, -2, 2) Rep(j, -2, 2) {
      ll ny, nx;
      ny = y + i, nx = x + j;
      if (ny < 0 || ny >= H || nx < 0 || nx >= W)
        continue;
      if (s[ny][nx] == '#')
        continue;
      if (F[ny][nx] <= F[y][x] + 1)
        continue;
      chmin(F[ny][nx], F[y][x] + 1);
      qu.push_back({ny, nx});
    }
  }
}

int main() {
  cin >> H >> W;
  cin >> sy >> sx >> gy >> gx;
  sy--, sx--, gy--, gx--;
  rep(i, 0, H) cin >> s[i];

  bfs();

  co((F[gy][gx] == INF ? -1 : F[gy][gx]));

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define Rep(i, a, b) for (ll i = a; i <= b; i++)
#define repr(i, a, b) for (ll i = b - 1; i >= a; i--)
#define _GLIBCXX_DEBUG
#define Vl vector<ll>
#define Vs vector<string>
#define Vp vector<pair<ll, ll>>
using ll = long long;
#define ALL(v) (v).begin(), (v).end()
#define endl "\\n"
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define co(x) cout << x << endl
#define coel cout << endl
#define pb push_back
#define sz(v) ((ll)(v).size())
const double pi = acos(-1.0);
const ll MOD = 1e9 + 7;
const ll INF = 1LL << 60;
#define pp pair<ll, pair<ll, ll>>
#define fi first
#define se second

////////////////////////////////////////////////////////
ll H, W;
ll field[1010][1010];
string s[1010];
ll dy[] = {0, 1, 0, -1};
ll dx[] = {1, 0, -1, 0};
ll t = 0;
queue<pair<pair<ll, ll>, ll>> qu;

void dfs(ll h, ll w, ll v) {
  // cout << "h: " << h << ", w: " << w << ", v: " << v << endl;
  // t++;
  // if(t > 100) return;
  chmin(field[h][w], v);
  Rep(i, -2, 2) Rep(j, -2, 2) {
    ll nh, nw;
    nh = h + i, nw = w + j;
    if (nh < 0 || nh >= H || nw < 0 || nw >= W)
      continue;
    if (s[nh][nw] == '#')
      continue;
    if (field[nh][nw] <= v)
      continue;
    chmin(field[nh][nw], v + 1);
    qu.push({{nh, nw}, v + 1});
  }
  rep(i, 0, 4) {
    ll nh, nw;
    nh = h + dy[i], nw = w + dx[i];
    if (nh < 0 || nh >= H || nw < 0 || nw >= W)
      continue;
    if (field[nh][nw] <= v)
      continue;
    if (s[nh][nw] == '#')
      continue;
    qu.push({{nh, nw}, v});
  }
}

int main() {
  rep(i, 0, 1010) rep(j, 0, 1010) field[i][j] = INF;

  cin >> H >> W;
  ll sx, sy, gx, gy;
  cin >> sy >> sx >> gy >> gx;
  sy--, sx--, gy--, gx--;
  rep(i, 0, H) cin >> s[i];

  field[sy][sx] = 0;

  dfs(sy, sx, 0);
  ll now = 1;

  while (!qu.empty()) {
    ll h = qu.front().first.first, w = qu.front().first.second,
       v = qu.front().second;
    qu.pop();
    dfs(h, w, v);
  }

  // rep(i, 0, H) rep(j, 0, W) cout << (field[i][j]==INF ? -1 : field[i][j]) <<
  // " \\n"[j==W-1];

  if (field[gy][gx] == INF)
    co(-1);
  else
    co(field[gy][gx]);

  return 0;
}