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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep_range(i, s, e) for (int i = (s); i < (int)(e); i++)
#define rep_rev(i, s, e) for (int i = (s)-1; i >= (int)(e); i--)
#define sort_vec(v) sort((v).begin(), (v).end())
typedef int64_t lint;
// lint mod = 998244353;
// lint mod = 1000000007;
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
  return a < b && (a = b, true);
}
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
  return a > b && (a = b, true);
}

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

lint app() {
  int h, w;
  cin >> h >> w;

  int ch, cw, dh, dw;
  cin >> ch >> cw;
  cin >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;

  vector<vector<bool>> s(h, vector<bool>(w));
  rep(i, h) {
    string str;
    cin >> str;
    rep(j, w) s[i][j] = (str[j] == '.');
  }

  vector<vector<int>> dep(h, vector<int>(w, 2e9));

  using state = pair<int, pair<int, int>>;
  priority_queue<state, vector<state>, greater<state>> que;
  que.emplace(0, make_pair(ch, cw));
  while (!que.empty()) {
    auto u = que.top();
    que.pop();
    auto us = u.second;
    int udep = u.first;
    int ui = us.first;
    int uj = us.second;
    if (udep >= dep[ui][uj])
      continue;
    dep[ui][uj] = udep;
    rep(t, 4) {
      int vi = ui + dx[t];
      int vj = uj + dy[t];
      if (vi < 0 || vi >= h || vj < 0 || vj > w)
        continue;
      if (s[vi][vj])
        if (udep < dep[vi][vj])
          que.emplace(udep, make_pair(vi, vj));
    }
    rep_range(vi, ui - 2, ui + 3) rep_range(vj, uj - 2, uj + 3) {
      if (vi < 0 || vi >= h || vj < 0 || vj > w)
        continue;
      if (s[vi][vj])
        if (udep + 1 < dep[vi][vj])
          que.emplace(udep + 1, make_pair(vi, vj));
    }
  }

  int ans = dep[dh][dw];
  if (ans == 2e9)
    return -1;
  return ans;
}

int main() {
  cout << app() << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep_range(i, s, e) for (int i = (s); i < (int)(e); i++)
#define rep_rev(i, s, e) for (int i = (s)-1; i >= (int)(e); i--)
#define sort_vec(v) sort((v).begin(), (v).end())
typedef int64_t lint;
// lint mod = 998244353;
// lint mod = 1000000007;
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
  return a < b && (a = b, true);
}
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
  return a > b && (a = b, true);
}

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

lint app() {
  int h, w;
  cin >> h >> w;

  int ch, cw, dh, dw;
  cin >> ch >> cw;
  cin >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;

  vector<vector<bool>> s(h, vector<bool>(w));
  rep(i, h) {
    string str;
    cin >> str;
    rep(j, w) s[i][j] = (str[j] == '.');
  }

  vector<vector<int>> dep(h, vector<int>(w, 2e9));

  using state = pair<int, pair<int, int>>;
  queue<state> que;
  que.emplace(0, make_pair(ch, cw));
  while (!que.empty()) {
    auto u = que.front();
    que.pop();
    auto us = u.second;
    int udep = u.first;
    int ui = us.first;
    int uj = us.second;
    if (udep >= dep[ui][uj])
      continue;
    dep[ui][uj] = udep;
    rep(t, 4) {
      int vi = ui + dx[t];
      int vj = uj + dy[t];
      if (vi < 0 || vi >= h || vj < 0 || vj > w)
        continue;
      if (s[vi][vj])
        if (udep < dep[vi][vj])
          que.emplace(udep, make_pair(vi, vj));
    }
    rep_range(vi, ui - 2, ui + 3) rep_range(vj, uj - 2, uj + 3) {
      if (vi < 0 || vi >= h || vj < 0 || vj > w)
        continue;
      if (s[vi][vj])
        if (udep + 1 < dep[vi][vj])
          que.emplace(udep + 1, make_pair(vi, vj));
    }
  }

  int ans = dep[dh][dw];
  if (ans == 2e9)
    return -1;
  return ans;
}

int main() {
  cout << app() << endl;
  return 0;
}