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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i, a, b) for (ll i = (a); i < (b); i++)
#define PER(i, a, b) for (ll i = (a); i >= (b); i--)
#define rep(i, n) REP(i, 0, n)
#define per(i, n) PER(i, n, 0)
#define ALL(a) (a).begin(), (a).end()
const ll INF = 1e18 + 18;
const ll MAX = 200000;
const ll MOD = 1000000007;
#define Yes(n) cout << ((n) ? "Yes" : "No") << endl;
#define YES(n) cout << ((n) ? "YES" : "NO") << endl;

ll MAX_H = 1010;
ll MAX_W = 1010;
vector<vector<ll>> Way(MAX_H, vector<ll>(MAX_W, INF));
vector<vector<char>> S(MAX_H, vector<char>(MAX_W));
ll CH, CW, DH, DW;
ll H, W;
priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> Q;

void init(ll h, ll w) {
  rep(i, h) {
    rep(j, w) { Way[i][j] = INF; }
  }
}

void update(ll h, ll w, ll q) {
  // if(h==1&&w==4)cout<<h<<" "<<w<<" "<<Q<<endl;
  if (S[h][w] != '.')
    return;
  if (Way[h][w] > q) {
    Way[h][w] = q;
    Q.push(make_pair(q, h * W + w));
    // cout<<h<<" "<<w<<" "<<q<<endl;
    return;
  }
  return;
}

void calc(ll h, ll w) {
  if (h > 0) {
    update(h - 1, w, Way[h][w]);
  }
  if (w > 0) {
    update(h, w - 1, Way[h][w]);
  }
  if (h < H - 1) {
    update(h + 1, w, Way[h][w]);
  }
  if (w < W - 1) {
    update(h, w + 1, Way[h][w]);
  }
  for (ll i = -2; i <= 2; i++) {
    for (ll j = -2; j <= 2; j++) {
      ll hh = h + i;
      ll ww = w + j;
      if (hh < 0 || hh >= H || ww < 0 || ww >= W)
        continue;
      update(hh, ww, Way[h][w] + 1);
    }
  }
}

int main() {
  cin >> H >> W;
  cin >> CH >> CW >> DH >> DW;
  CH--;
  CW--;
  DH--;
  DW--;
  rep(i, H) {
    rep(j, W) { cin >> S[i][j]; }
  }
  // init(H, W);
  Q.push(make_pair(0, CH * W + CW));
  Way[CH][CW] = 0;
  vector<ll> Used(H * W + 10, INF);
  while (!(Q.empty())) {
    pair<ll, ll> R = Q.top();
    Q.pop();
    calc(R.second / W, R.second % W);
  }

  if (Way[DH][DW] == INF) {
    cout << -1 << endl;
    return 0;
  }
  /*rep(i, H){
    rep(j, W){
      cout<<Way[i][j]<<" ";
    }
    cout<<endl;
  }*/
  cout << Way[DH][DW] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i, a, b) for (ll i = (a); i < (b); i++)
#define PER(i, a, b) for (ll i = (a); i >= (b); i--)
#define rep(i, n) REP(i, 0, n)
#define per(i, n) PER(i, n, 0)
#define ALL(a) (a).begin(), (a).end()
const ll INF = 1e18 + 18;
const ll MAX = 200000;
const ll MOD = 1000000007;
#define Yes(n) cout << ((n) ? "Yes" : "No") << endl;
#define YES(n) cout << ((n) ? "YES" : "NO") << endl;

ll MAX_H = 1010;
ll MAX_W = 1010;
vector<vector<ll>> Way(MAX_H, vector<ll>(MAX_W, INF));
vector<vector<char>> S(MAX_H, vector<char>(MAX_W));
ll CH, CW, DH, DW;
ll H, W;
void init(ll h, ll w) {
  rep(i, h) {
    rep(j, w) { Way[i][j] = INF; }
  }
}

bool update(ll h, ll w, ll Q) {
  // if(h==1&&w==4)cout<<h<<" "<<w<<" "<<Q<<endl;
  if (S[h][w] != '.')
    return false;
  if (Way[h][w] > Q) {
    Way[h][w] = Q;
    // cout<<h<<" "<<w<<" "<<Q<<endl;
    return true;
  }
  return false;
}

void calc(ll h, ll w) {
  if (h > 0) {
    if (update(h - 1, w, Way[h][w]))
      calc(h - 1, w);
  }
  if (w > 0) {
    if (update(h, w - 1, Way[h][w]))
      calc(h, w - 1);
  }
  if (h < H - 1) {
    if (update(h + 1, w, Way[h][w]))
      calc(h + 1, w);
  }
  if (w < W - 1) {
    if (update(h, w + 1, Way[h][w]))
      calc(h, w + 1);
  }
  for (ll i = -2; i <= 2; i++) {
    for (ll j = -2; j <= 2; j++) {
      if (abs(i) + abs(j) < 2)
        continue;
      ll hh = h + i;
      ll ww = w + j;
      if (hh < 0 || hh >= H || ww < 0 || ww >= W)
        continue;
      if (S[hh][ww] != '.')
        continue;
      if (update(hh, ww, Way[h][w] + 1))
        calc(hh, ww);
    }
  }
}

int main() {
  cin >> H >> W;
  cin >> CH >> CW >> DH >> DW;
  CH--;
  CW--;
  DH--;
  DW--;
  rep(i, H) {
    rep(j, W) { cin >> S[i][j]; }
  }
  // init(H, W);
  Way[CH][CW] = 0;
  calc(CH, CW);
  if (Way[DH][DW] == INF) {
    cout << -1 << endl;
    return 0;
  }
  /*
  rep(i, H){
    rep(j, W){
      cout<<Way[i][j]<<" ";
    }
    cout<<endl;
  }*/
  cout << Way[DH][DW] << endl;
  return 0;
}