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

#include <bits/stdc++.h>
#pragma GCC optimize("O3")

#define fi first
#define se second
#define mp make_pair
#define pi pair<int, int>
#define vi vector<int>
#define vpi vector<pair<int, int>>
#define vll vector<ll>
#define vpll vector<pair<ll, ll>>
#define endl '\\n'

#define si(v) int((v).size())
#define all(v) (v).begin(), (v).end()
#define rep(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define rep2(i, n, m) for (ll i = n; i <= (ll)(m); ++i)
#define rep3(i, n, m) for (ll i = n; i >= (ll)(m); --i)

template <class T, class U> bool chmax(T &a, U b) {
  if (a < b)
    a = b;
  return true;
}
template <class T, class U> bool chmin(T &a, U b) {
  if (a > b)
    a = b;
  return true;
}

using ll = long long;
using ld = long double;
using namespace std;

constexpr ll MOD = 1000000007, INF = 1000000000;

int h, w;
int sx, sy, gx, gy;
char maze[1001][1001];
int dist[1001][1001];

int dijkstra() {
  priority_queue<pair<int, pi>, vector<pair<int, pi>>, greater<pair<int, pi>>>
      que;
  que.emplace(0, mp(sx, sy));

  rep(i, 1001) rep(j, 1001) dist[i][j] = INF;
  dist[sy][sx] = 0;

  while (!que.empty()) {
    auto p = que.top();
    que.pop();
    int x = p.se.fi, y = p.se.se, d = p.fi;

    if (dist[y][x] < d)
      continue;

    rep2(i, -2, 2) rep2(j, -2, 2) {
      int nx = x + i, ny = y + j;

      if (nx < 0 || ny < 0 || nx >= w || ny >= h || maze[ny][nx] == '#')
        continue;

      int nd = d + (abs(i) + abs(j) > 1);
      if (dist[ny][nx] > nd) {
        dist[ny][nx] = nd;
        que.emplace(nd, mp(nx, ny));
      }
    }
  }

  return (dist[gy][gx] == INF ? -1 : dist[gy][gx]);
}

signed main() {
  cout << fixed << setprecision(20);
  ios::sync_with_stdio(false);
  cin.tie(0);

  cin >> h >> w;
  cin >> sy >> sx;
  cin >> gy >> gx;

  --sy, --sx;
  --gy, --gx;

  rep(i, h) rep(j, w) cin >> maze[i][j];

  cout << dijkstra() << endl;

  return (0);
}


Error: TLE

Fixed submission:


#include <bits/stdc++.h>
#pragma GCC optimize("O3")

#define fi first
#define se second
#define vi vector<int>
#define vpi vector<pair<int, int>>
#define vll vector<ll>
#define vpll vector<pair<ll, ll>>
#define endl '\\n'

#define si(v) int((v).size())
#define all(v) (v).begin(), (v).end()
#define rep(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define rep2(i, n, m) for (ll i = n; i <= (ll)(m); ++i)
#define rep3(i, n, m) for (ll i = n; i >= (ll)(m); --i)

template <class T, class U> bool chmax(T &a, const U &b) {
  if (a < b) {
    a = b;
    return true;
  } else
    return false;
}
template <class T, class U> bool chmin(T &a, const U &b) {
  if (b < a) {
    a = b;
    return true;
  } else
    return false;
}

using ll = long long;
using ld = long double;
using namespace std;

constexpr ll MOD = 1000000007;

int h, w;
int sx, sy, gx, gy;
char maze[1001][1001];
int vs[1001][1001];
int dp[1001][1001];

signed main() {
  cout << fixed << setprecision(20);
  ios::sync_with_stdio(false);
  cin.tie(0);

  cin >> h >> w;
  cin >> sy >> sx;
  cin >> gy >> gx;

  --sx, --sy;
  --gx, --gy;

  rep(i, h) rep(j, w) cin >> maze[i][j];

  queue<pair<int, int>> que;

  rep(i, h) rep(j, w) dp[i][j] = MOD;
  dp[sy][sx] = 0;
  que.emplace(sx, sy);

  while (!que.empty()) {
    auto p = que.front();
    que.pop();
    int x = p.fi, y = p.se;
    vs[sy][sx] = true;

    rep2(i, -2, 2) rep2(j, -2, 2) {
      int nx = x + i, ny = y + j;

      if (nx < 0 || ny < 0 || nx >= w || ny >= h || maze[ny][nx] == '#' ||
          vs[ny][nx])
        continue;

      if (abs(i) + abs(j) < 2 && chmin(dp[ny][nx], dp[y][x]))
        que.emplace(nx, ny);
      else if (chmin(dp[ny][nx], dp[y][x] + 1))
        que.emplace(nx, ny);
    }
  }

  cout << (dp[gy][gx] == MOD ? -1 : dp[gy][gx]) << endl;

  return (0);
}