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

#include <bits/stdc++.h>
#define REP(i, e) for (int(i) = 0; (i) < (e); ++(i))
#define FOR(i, b, e) for (int(i) = (b); (i) < (e); ++(i))
#define ALL(c) (c).begin(), (c).end()
#define PRINT(x) cout << (x) << "\\n"
using namespace std;
using ll = long long;
using pint = pair<int, int>;
using pll = pair<ll, ll>;
const long long MOD = 1000000007;

ll H, W, Ch, Cw, Dh, Dw;
bool S[1001][1001];
bool visited[1001][1001];
ll dist[1001][1001];

ll bfs() {
  using item = pair<ll, pll>;
  priority_queue<item, vector<item>, greater<item>> que;
  que.push(item(0, pll(Ch, Cw)));
  while (!que.empty()) {
    ll d = que.top().first;
    ll i = que.top().second.first;
    ll j = que.top().second.second;
    que.pop();
    if (S[i][j] == false)
      continue;
    dist[i][j] = d;
    if (i == Dh && j == Dw) {
      return d;
    }
    if (i > 0 && d < dist[i - 1][j] && S[i - 1][j]) {
      dist[i - 1][j] = d;
      que.push(item(d, pll(i - 1, j)));
    }
    if (i < H - 1 && d < dist[i + 1][j] && S[i + 1][j]) {
      dist[i + 1][j] = d;
      que.push(item(d, pll(i + 1, j)));
    }
    if (j > 0 && d < dist[i][j - 1] && S[i][j - 1]) {
      dist[i][j - 1] = d;
      que.push(item(d, pll(i, j - 1)));
    }
    if (j < W - 1 && d < dist[i][j + 1] && S[i][j + 1]) {
      dist[i][j + 1] = d;
      que.push(item(d, pll(i, j + 1)));
    }
    for (ll k = i - 2; k <= i + 2; k++) {
      if (k < 0 || k >= H)
        continue;
      for (ll l = j - 2; l <= j + 2; l++) {
        if (l < 0 || l >= W)
          continue;
        if (S[k][l] && d + 1 < dist[k][l]) {
          dist[k][l] = d + 1;
          que.push(item(d + 1, pll(k, l)));
        }
      }
    }
  }
  return -1;
}

signed main() {
  cin >> H >> W >> Ch >> Cw >> Dh >> Dw;
  Ch--;
  Cw--;
  Dh--;
  Dw--;
  string str;
  REP(i, H) {
    cin >> str;
    REP(j, W) {
      S[i][j] = (str[j] == '.');
      dist[i][j] = LONG_LONG_MAX / 2;
    }
  }
  PRINT(bfs());
  /*
  REP(i, H){
      REP(j, W) cerr << dist[i][j] << " ";
      cerr << endl;
  }
  */
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, e) for (int(i) = 0; (i) < (e); ++(i))
#define FOR(i, b, e) for (int(i) = (b); (i) < (e); ++(i))
#define ALL(c) (c).begin(), (c).end()
#define PRINT(x) cout << (x) << "\\n"
using namespace std;
using ll = long long;
using pint = pair<int, int>;
using pll = pair<ll, ll>;
const long long MOD = 1000000007;

ll H, W, Ch, Cw, Dh, Dw;
bool S[1001][1001];
bool visited[1001][1001];
ll dist[1001][1001];

ll bfs() {
  using item = pair<ll, pll>;
  priority_queue<item, vector<item>, greater<item>> que;
  que.push(item(0, pll(Ch, Cw)));
  while (!que.empty()) {
    ll d = que.top().first;
    ll i = que.top().second.first;
    ll j = que.top().second.second;
    que.pop();
    if (i < 0 || i >= H || j < 0 || j >= W || S[i][j] == false ||
        dist[i][j] <= d)
      continue;
    dist[i][j] = d;
    if (i == Dh && j == Dw) {
      return d;
    }
    que.push(item(d, pll(i - 1, j)));
    que.push(item(d, pll(i + 1, j)));
    que.push(item(d, pll(i, j - 1)));
    que.push(item(d, pll(i, j + 1)));
    for (ll k = i - 2; k <= i + 2; k++) {
      for (ll l = j - 2; l <= j + 2; l++) {
        que.push(item(d + 1, pll(k, l)));
      }
    }
  }
  return -1;
}

signed main() {
  cin >> H >> W >> Ch >> Cw >> Dh >> Dw;
  Ch--;
  Cw--;
  Dh--;
  Dw--;
  string str;
  REP(i, H) {
    cin >> str;
    REP(j, W) {
      S[i][j] = (str[j] == '.');
      dist[i][j] = LONG_LONG_MAX / 2;
    }
  }
  PRINT(bfs());
  return 0;
}