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

#include <bits/stdc++.h>
#define PREP(i, s, x) for (ll i = (s); i < (x); i++)
#define MREP(i, s, x) for (ll i = (s); i >= (x); i--)
#define REP(i, x) PREP(i, 0, x)
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
template <typename T> inline bool chmax(T &a, T b) {
  return ((a < b) ? (a = b, true) : (false));
}
template <typename T> inline bool chmin(T &a, T b) {
  return ((a > b) ? (a = b, true) : (false));
}

// variable -------------------------------------------

constexpr ll INF = (1ll << 59);
constexpr ll MOD = 1000000007ll;

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

// structure ------------------------------------------

// function -------------------------------------------

// main -----------------------------------------------

int main() {
  cin >> h >> w;
  P c, d;
  cin >> c.first >> c.second >> d.first >> d.second;
  c.first--;
  c.second--;
  d.first--;
  d.second--;
  vector<string> field(h);
  REP(i, h) { cin >> field[i]; }

  vector<vector<ll>> seen(h, vector<ll>(w, INF));
  seen[c.first][c.second] = 0;

  priority_queue<pair<ll, P>, vector<pair<ll, P>>, greater<pair<ll, P>>> que;
  que.emplace(0, c);

  while (!que.empty()) {
    pair<ll, P> hoge = que.top();
    que.pop();
    ll x = hoge.second.first, y = hoge.second.second;
    ll level = hoge.first;

    if (x == d.first && y == d.second) {
      cout << level << endl;
      return 0;
    }
    REP(i, 4) {
      ll nx = x + dx[i], ny = y + dy[i];
      if (nx < 0 || ny < 0 || nx >= h || ny >= w)
        continue;
      if (seen[nx][ny] <= level)
        continue;
      if (field[nx][ny] != '.')
        continue;
      seen[nx][ny] = level;
      que.emplace(level, make_pair(nx, ny));
    }

    for (ll i = x - 2; i <= x + 2; i++) {
      for (ll j = y - 2; j <= y + 2; j++) {
        if (i < 0 || j < 0 || i >= h || j >= w)
          continue;
        if (seen[i][j] <= level + 1)
          continue;
        if (field[i][j] != '.')
          continue;
        seen[i][j] = level + 1;
        que.emplace(level + 1, make_pair(i, j));
      }
    }
  }

  // REP(i, h){
  // 	REP(j, w){
  // 		cout << seen[i][j] << " ";
  // 	}
  // 	cout << endl;
  // }
  // cout << endl;

  cout << -1 << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define PREP(i, s, x) for (ll i = (s); i < (x); i++)
#define MREP(i, s, x) for (ll i = (s); i >= (x); i--)
#define REP(i, x) PREP(i, 0, x)
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
template <typename T> inline bool chmax(T &a, T b) {
  return ((a < b) ? (a = b, true) : (false));
}
template <typename T> inline bool chmin(T &a, T b) {
  return ((a > b) ? (a = b, true) : (false));
}

// variable -------------------------------------------

constexpr ll INF = (1ll << 59);
constexpr ll MOD = 1000000007ll;

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

// structure ------------------------------------------

// function -------------------------------------------

// main -----------------------------------------------

int main() {
  cin >> h >> w;
  P c, d;
  cin >> c.first >> c.second >> d.first >> d.second;
  c.first--;
  c.second--;
  d.first--;
  d.second--;
  vector<string> field(h);
  REP(i, h) { cin >> field[i]; }

  vector<vector<ll>> seen(h, vector<ll>(w, INF));
  seen[c.first][c.second] = 0;

  priority_queue<pair<ll, P>, vector<pair<ll, P>>, greater<pair<ll, P>>> que;
  que.emplace(0, c);

  while (!que.empty()) {
    pair<ll, P> hoge = que.top();
    que.pop();
    ll x = hoge.second.first, y = hoge.second.second;
    ll level = hoge.first;

    seen[x][y] = level;
    if (x == d.first && y == d.second) {
      cout << level << endl;
      return 0;
    }
    REP(i, 4) {
      ll nx = x + dx[i], ny = y + dy[i];
      if (nx < 0 || ny < 0 || nx >= h || ny >= w)
        continue;
      if (seen[nx][ny] <= level)
        continue;
      if (field[nx][ny] != '.')
        continue;
      que.emplace(level, make_pair(nx, ny));
    }

    for (ll i = x - 2; i <= x + 2; i++) {
      for (ll j = y - 2; j <= y + 2; j++) {
        if (i < 0 || j < 0 || i >= h || j >= w)
          continue;
        if (seen[i][j] <= level + 1)
          continue;
        if (field[i][j] != '.')
          continue;
        que.emplace(level + 1, make_pair(i, j));
      }
    }
  }

  // REP(i, h){
  // 	REP(j, w){
  // 		cout << seen[i][j] << " ";
  // 	}
  // 	cout << endl;
  // }
  // cout << endl;

  cout << -1 << endl;
  return 0;
}