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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0, i##_len = (n); i < i##_len; i++)
#define reps(i, s, n) for (ll i = (s), i##_len = (n); i < i##_len; i++)
#define rrep(i, n) for (ll i = (n)-1; i >= 0; i--)
#define rreps(i, e, n) for (ll i = (n)-1; i >= (e); i--)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) ((ll)(x).size())
#define len(x) ((ll)(x).length())
#define endl "\\n"
template <class T> void chmax(T &a, const T b) { a = max(a, b); }
template <class T> void chmin(T &a, const T b) { a = min(a, b); }

template <class T> struct Dijkstra {
  const T inf = numeric_limits<T>::max() / 2 - 1;
  vector<T> dist;
  vector<int> path;

  Dijkstra() {}
  Dijkstra(int n) {
    graph.resize(n);
    dist.resize(n);
    prev.resize(n);
  }

  void add_edge(int from, int to, T cost) {
    graph[from].emplace_back(to, cost);
  }

  void get_distance(int from) {
    priority_queue<pair<T, int>, vector<pair<T, int>>, greater<pair<T, int>>>
        pq;
    fill(dist.begin(), dist.end(), inf);
    fill(prev.begin(), prev.end(), -1);
    dist[from] = 0;
    pq.push(make_pair(0, from));
    while (!pq.empty()) {
      pair<T, int> p = pq.top();
      pq.pop();
      if (dist[p.second] < p.first)
        continue;
      for (auto e : graph[p.second]) {
        if (dist[e.first] > (dist[p.second] + e.second)) {
          dist[e.first] = dist[p.second] + e.second;
          prev[e.first] = p.second;
          pq.push(make_pair(dist[e.first], e.first));
        }
      }
    }
  }

  void get_path(int to) {
    path = vector<int>(0);
    for (; to != -1; to = prev[to]) {
      path.push_back(to);
    }
    reverse(path.begin(), path.end());
  }

private:
  vector<vector<pair<int, T>>> graph;
  vector<T> prev;
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  // ifstream in("input.txt");
  // cin.rdbuf(in.rdbuf());
  const ll dir[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
  const ll inf = LONG_LONG_MAX / 2 - 1;
  ll h, w;
  cin >> h >> w;
  ll ch, cw, dh, dw;
  cin >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  vector<string> s(h);
  rep(i, h) cin >> s[i];
  Dijkstra<ll> di(h * w);
  rep(y, h) {
    rep(x, w) {
      rep(i, 4) {
        ll nx = x + dir[i][0];
        ll ny = y + dir[i][1];
        if ((nx < 0) || (nx >= w) || (ny < 0) || (ny >= h))
          continue;
        if (s[ny][nx] != '.')
          continue;
        di.add_edge(y * w + x, ny * w + nx, 0);
      }
      reps(dy, -2, 3) {
        reps(dx, -2, 3) {
          if ((dy == 0) && (dx == 0))
            continue;
          ll nx = x + dx;
          ll ny = y + dy;
          if ((nx < 0) || (nx >= w) || (ny < 0) || (ny >= h))
            continue;
          if (s[ny][nx] != '.')
            continue;
          di.add_edge(y * w + x, ny * w + nx, 1);
        }
      }
    }
  }
  di.get_distance(ch * w + cw);
  if (di.dist[dh * w + dw] == inf) {
    cout << -1 << endl;
  } else {
    cout << di.dist[dh * w + dw] << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0, i##_len = (n); i < i##_len; i++)
#define reps(i, s, n) for (ll i = (s), i##_len = (n); i < i##_len; i++)
#define rrep(i, n) for (ll i = (n)-1; i >= 0; i--)
#define rreps(i, e, n) for (ll i = (n)-1; i >= (e); i--)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) ((ll)(x).size())
#define len(x) ((ll)(x).length())
#define endl "\\n"
template <class T> void chmax(T &a, const T b) { a = max(a, b); }
template <class T> void chmin(T &a, const T b) { a = min(a, b); }

struct S {
  ll y, x, d;
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  // ifstream in("input.txt");
  // cin.rdbuf(in.rdbuf());
  const ll dir[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
  const ll inf = LONG_LONG_MAX / 2 - 1;
  ll h, w;
  cin >> h >> w;
  ll ch, cw, dh, dw;
  cin >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  vector<string> s(h);
  rep(i, h) cin >> s[i];
  vector<vector<ll>> dist(h, vector<ll>(w, inf));
  dist[ch][cw] = 0;
  queue<S> q;
  q.push((S){ch, cw, 0});
  while (!q.empty()) {
    S tmp = q.front();
    q.pop();
    if (tmp.d != dist[tmp.y][tmp.x])
      continue;
    rep(i, 4) {
      ll nx = tmp.x + dir[i][0];
      ll ny = tmp.y + dir[i][1];
      ll nd = tmp.d;
      if ((nx < 0) || (nx >= w) || (ny < 0) || (ny >= h))
        continue;
      if (dist[ny][nx] <= nd)
        continue;
      if (s[ny][nx] != '.')
        continue;
      dist[ny][nx] = nd;
      q.push((S){ny, nx, nd});
    }
    reps(i, -2, 3) {
      reps(j, -2, 3) {
        if ((i == 0) && (j == 0))
          continue;
        ll nx = tmp.x + i;
        ll ny = tmp.y + j;
        ll nd = tmp.d + 1;
        if ((nx < 0) || (nx >= w) || (ny < 0) || (ny >= h))
          continue;
        if (dist[ny][nx] <= nd)
          continue;
        if (s[ny][nx] != '.')
          continue;
        dist[ny][nx] = nd;
        q.push((S){ny, nx, nd});
      }
    }
  }
  if (dist[dh][dw] == inf) {
    cout << -1 << endl;
  } else {
    cout << dist[dh][dw] << endl;
  }
  return 0;
}