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

#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = s; i < (int)(n); i++)
#define Clear(a) a = decltype(a)()
#define all(a) a.begin(), a.end()
#define vec vector
typedef long long ll;
typedef pair<ll, ll> P;
// const ll big=998244353;
const ll big = 1000000007LL;
const ll INF = 1e18;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
ll max(ll x, ll y) {
  if (x > y)
    return x;
  else
    return y;
}
ll min(ll x, ll y) {
  if (x < y)
    return x;
  else
    return y;
}
ll expm(ll x, ll y) {
  if (y == 0)
    return 1; // 0^0=1
  if (x == 1 || x == 0)
    return x;
  if (y % 2 == 1)
    return (expm(x, y - 1) * x) % big;
  ll t = expm(x, y / 2);
  return (t * t) % big;
}
ll exp(ll x, ll y) {
  if (y == 0)
    return 1; // 0^0=1
  if (x == 1 || y == 0)
    return x;
  if (y % 2 == 1)
    return exp(x, y - 1) * x;
  ll t = exp(x, y / 2);
  return t * t;
}
ll inv(ll x) { return expm(x, big - 2); }

struct edge {
  ll to, cost;
  constexpr bool operator<(const edge &a) const noexcept {
    if ((*this).cost < a.cost)
      return true;
    else
      return false;
  }
  constexpr bool operator>(const edge &a) const noexcept {
    if ((*this).cost > a.cost)
      return true;
    else
      return false;
  }
};

struct graph {
  ll v;
  vec<vec<edge>> g;
  vec<ll> d;

  graph(ll n) : v(n), g(n), d(n, INF) {}

  void addedge(const ll s, const ll t, const ll cost) {
    edge e;
    e.to = t;
    e.cost = cost;
    g[s].emplace_back(e);
  }
  // E+VlogV
  void dijkstra(ll s) { // sからstartのdijkstra
    /*一度dijkstraをした時同じグラフでdijkstraしたいならこれも入れる
    rep(i,v)d[i]=INF;
    */
    d[s] = 0;
    priority_queue<P, vec<P>, greater<P>> que;
    que.push(P(0, s));
    while (!que.empty()) {
      P pa = que.top();
      que.pop();
      ll k = pa.second;
      if (d[k] < pa.first)
        continue;
      for (auto e : g[k]) {
        if (d[e.to] > d[k] + e.cost) {
          d[e.to] = d[k] + e.cost;
          que.push(P(d[e.to], e.to));
        }
      }
    }
  }
};

int main() {
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  cout << fixed << setprecision(10);
  /*--------------------------------*/

  int h, w, sx, sy, gx, gy;
  cin >> h >> w >> sx >> sy >> gx >> gy;
  vec<vec<char>> s(h, vec<char>(w));
  rep(i, h) rep(j, w) cin >> s[i][j];
  sx--;
  sy--;
  gx--;
  gy--;
  graph g(h * w);

  // 点(i,j)は点(w*i+j)に
  // 点(i,j)から右下に引けるところへ辺を張る
  rep(i, h) rep(j, w) {
    if (s[i][j] == '#')
      continue;
    rep(k, 4) {
      int nx = i + dx[k], ny = j + dy[k];
      if (nx < 0 || ny < 0 || nx >= h || ny >= w)
        continue;
      if (s[nx][ny] == '#')
        continue;
      g.addedge(w * i + j, w * nx + ny, 0);
    }
    rep2(k, -2, 3) rep2(l, -2, 3) {
      if (k * k + l * l == 1)
        continue;
      if (i + k >= h || j + l >= w || i + k < 0 || j + l < 0)
        continue;
      if (s[i + k][j + l] == '#')
        continue;
      g.addedge(w * i + j, w * (i + k) + j + l, 1);
    }
  }
  g.dijkstra(w * sx + sy);
  // rep(i,h)rep(j,w)cout << g.d[w*i+j] << endl;
  if (g.d[w * gx + gy] == INF) {
    cout << -1 << endl;
    return 0;
  }
  cout << g.d[w * gx + gy] << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = s; i < (int)(n); i++)
#define Clear(a) a = decltype(a)()
#define all(a) a.begin(), a.end()
#define vec vector
typedef long long ll;
typedef pair<ll, ll> P;
// const ll big=998244353;
const ll big = 1000000007LL;
const ll INF = 1e18;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
ll max(ll x, ll y) {
  if (x > y)
    return x;
  else
    return y;
}
ll min(ll x, ll y) {
  if (x < y)
    return x;
  else
    return y;
}
ll expm(ll x, ll y) {
  if (y == 0)
    return 1; // 0^0=1
  if (x == 1 || x == 0)
    return x;
  if (y % 2 == 1)
    return (expm(x, y - 1) * x) % big;
  ll t = expm(x, y / 2);
  return (t * t) % big;
}
ll exp(ll x, ll y) {
  if (y == 0)
    return 1; // 0^0=1
  if (x == 1 || y == 0)
    return x;
  if (y % 2 == 1)
    return exp(x, y - 1) * x;
  ll t = exp(x, y / 2);
  return t * t;
}
ll inv(ll x) { return expm(x, big - 2); }

int main() {
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  cout << fixed << setprecision(10);
  /*--------------------------------*/

  int h, w;
  cin >> h >> w;
  int sx, sy, gx, gy;
  cin >> sx >> sy >> gx >> gy;
  sx--;
  sy--;
  gx--;
  gy--;
  vec<vec<char>> s(h, vec<char>(w));
  rep(i, h) rep(j, w) cin >> s[i][j];

  vec<vec<ll>> ans(h, vec<ll>(w, -1));
  ans[sx][sy] = 0;
  function<ll(int, int)> res = [&](int x, int y) {
    bool move = false;
    rep(i, 4) {
      int nx = dx[i] + x, ny = dy[i] + y;
      if (nx < 0 || nx >= h || ny < 0 || ny >= w)
        continue;
      if (s[nx][ny] == '#') {
        move = true;
        continue;
      }
      if (ans[nx][ny] == -1 || ans[nx][ny] > ans[x][y]) {
        ans[nx][ny] = ans[x][y];
        res(nx, ny);
      }
    }
    if (move) {
      rep2(i, -2 + x, x + 3) rep2(j, -2 + y, y + 3) {
        if (i < 0 || i >= h || j < 0 || j >= w)
          continue;
        if (s[i][j] == '#')
          continue;
        if (ans[i][j] > ans[x][y] + 1 || ans[i][j] == -1) {
          ans[i][j] = ans[x][y] + 1;
          res(i, j);
        }
      }
    }
    return 0LL;
  };
  res(sx, sy);
  cout << ans[gx][gy] << endl;
}