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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const ll MOD = 1000000007;
const ld PI = acos(-1);
const ld EPS = 0.0000000001;
const ll LINF = 1001002003004005006ll;
const int INF = 1001001001;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repd(i, n) for (ll i = n - 1; 0 <= i; i--)
#define FOR(i, a, b) for (ll i = a; i < (ll)(b); i++)
#define FORD(i, a, b) for (ll i = b - 1; (ll)(a) <= i; i--)
#define ALL(x) x.begin(), x.end()
#define MAX(x) *max_element(ALL(x))
#define MIN(x) *min_element(ALL(x))
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

ll c[1010][1010];

int main() {
  ll h, w;
  cin >> h >> w;
  ll sx, sy, gx, gy;
  cin >> sx >> sy >> gx >> gy;
  sx--, sy--, gx--, gy--;
  vector<vector<char>> g(h, vector<char>(w));
  rep(i, h) rep(j, w) cin >> g[i][j];
  rep(i, h) rep(j, w) c[i][j] = LINF;
  c[sx][sy] = 0;
  priority_queue<tuple<ll, ll, ll>, vector<tuple<ll, ll, ll>>,
                 greater<tuple<ll, ll, ll>>>
      que;
  que.emplace(0, sx, sy);
  while (!que.empty()) {
    ll cost = get<0>(que.top()), x = get<1>(que.top()), y = get<2>(que.top());
    que.pop();
    rep(i, 4) {
      ll nx = x + dx[i], ny = y + dy[i];
      if (nx < 0 || h <= nx || ny < 0 || w <= ny)
        continue;
      if (g[nx][ny] == '#')
        continue;
      if (cost < c[nx][ny]) {
        c[nx][ny] = c[x][y];
        que.emplace(c[nx][ny], nx, ny);
      }
    }
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        ll nx = x + i, ny = y + j;
        if (nx < 0 || h <= nx || ny < 0 || w <= ny)
          continue;
        if (g[nx][ny] == '#')
          continue;
        if (cost + 1 < c[nx][ny]) {
          c[nx][ny] = c[x][y] + 1;
          que.emplace(c[nx][ny], nx, ny);
        }
      }
    }
  }

  if (c[gx][gy] == LINF)
    c[gx][gy] = -1;

  cout << c[gx][gy] << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const ll MOD = 1000000007;
const ld PI = acos(-1);
const ld EPS = 0.0000000001;
const ll LINF = 1001002003004005006ll;
const int INF = 1001001001;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repd(i, n) for (ll i = n - 1; 0 <= i; i--)
#define FOR(i, a, b) for (ll i = a; i < (ll)(b); i++)
#define FORD(i, a, b) for (ll i = b - 1; (ll)(a) <= i; i--)
#define ALL(x) x.begin(), x.end()
#define MAX(x) *max_element(ALL(x))
#define MIN(x) *min_element(ALL(x))
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

ll c[1010][1010];
ll h, w;

void dfs(ll x, ll y, ll cost = 0) {
  c[x][y] = cost;
  rep(i, 4) {
    ll nx = x + dx[i], ny = y + dy[i];
    if (nx < 0 || h <= nx || ny < 0 || w <= ny || 0 <= c[nx][ny])
      continue;
    dfs(nx, ny, cost);
  }
}
int main() {
  cin >> h >> w;
  ll sx, sy, gx, gy;
  cin >> sx >> sy >> gx >> gy;
  sx--, sy--, gx--, gy--;
  vector<vector<char>> g(h, vector<char>(w));
  rep(i, h) rep(j, w) cin >> g[i][j];
  rep(i, h) rep(j, w) {
    if (g[i][j] == '#')
      c[i][j] = LINF;
    else
      c[i][j] = -1;
  }

  dfs(sx, sy);
  bool flag = true;
  ll nc = 0;
  while (flag) {
    queue<pair<ll, ll>> que;
    flag = false;
    nc++;
    rep(i, h) rep(j, w) {
      if (c[i][j] < 0) {
        bool chk = false;
        for (int x = -2; x <= 2; x++) {
          for (int y = -2; y <= 2; y++) {
            ll nx = x + i, ny = y + j;
            if (nx < 0 || h <= nx || ny < 0 || w <= ny || c[nx][ny] < 0)
              continue;
            if ('#' == g[nx][ny])
              continue;
            que.emplace(i, j);
            chk = true;
            if (chk)
              break;
          }
          if (chk)
            break;
        }
      }
    }

    while (!que.empty()) {
      ll x = que.front().first, y = que.front().second;
      que.pop();
      if (0 <= c[x][y])
        continue;
      flag = true;
      dfs(x, y, nc);
      // cout << nc << endl;
    }
    if (0 <= c[gx][gy])
      flag = false;
  }
  // rep(i, h){
  //     rep(j, w) cout << c[i][j] << ' ';
  //     cout << endl;
  // }
  cout << c[gx][gy] << endl;
}