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

#include <bits/stdc++.h>
#define INF 100000007
#define rep(i, N) for (ll i = 0; i < N; i++)
#define rep2(i, j, k) for (ll i = j; i < k; i++)
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define chmax(x, y) x = max(x, y)
#define print(x) cout << x << "\\n"
#define printv(vec) rep(i, vec.size()) cout << vec[i] << " "
#define ALL(v) v.begin(), v.end()
#define SUM(v) accumulate(ALL(v), 0)
#define MAX(v) *max_element(ALL(v))
#define MIN(v) *min_element(ALL(v))
#define SORT(v) sort(ALL(v))
#define REV(v) reverse(ALL(v))
typedef long long ll;
using namespace std;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using vs = vector<string>;
using vvs = vector<vector<string>>;

void solve() {

  ll H, W, ch, cw, dh, dw;
  cin >> H >> W;
  cin >> ch >> cw;
  cin >> dh >> dw;
  vs S(H);
  rep(i, H) cin >> S[i];
  ll dir[] = {1, 0, -1, 0, 1};

  deque<pair<ll, ll>> q;
  vector<vector<ll>> dp(H, vector<ll>(W, INF));
  dp[ch - 1][cw - 1] = 0;
  q.push_back({ch - 1, cw - 1});
  ll x, y, nx, ny;

  while (!q.empty()) {
    tie(x, y) = q.front();
    q.pop_front();
    rep(i, 4) {
      ny = y + dir[i];
      nx = x + dir[i + 1];
      if (ny < 0 || nx < 0 || nx >= H || ny >= W)
        continue;
      if (S[nx][ny] == '#')
        continue;
      if (dp[nx][ny] > dp[x][y]) {
        dp[nx][ny] = dp[x][y];
        q.push_front({nx, ny});
      }
    }
    rep2(i, -2, 3) {
      rep2(j, -2, 3) {
        nx = x + i;
        ny = y + j;
        if (ny < 0 || nx < 0 || nx >= H || ny >= W)
          continue;
        if (S[nx][ny] == '#')
          continue;
        if (dp[nx][ny] > dp[x][y] + 1) {
          dp[nx][ny] = dp[x][y] + 1;
          q.push_back({nx, ny});
        }
      }
    }
  }

  ll ans = dp[dh - 1][dw - 1];
  if (ans == INF)
    print(-1);
  else
    print(ans);
}

int main() {

  fastio;
  solve();

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define INF 100000007
#define rep(i, N) for (ll i = 0; i < N; i++)
#define rep2(i, j, k) for (ll i = j; i < k; i++)
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define chmax(x, y) x = max(x, y)
#define print(x) cout << x << "\\n"
#define printv(vec) rep(i, vec.size()) cout << vec[i] << " "
#define ALL(v) v.begin(), v.end()
#define SUM(v) accumulate(ALL(v), 0)
#define MAX(v) *max_element(ALL(v))
#define MIN(v) *min_element(ALL(v))
#define SORT(v) sort(ALL(v))
#define REV(v) reverse(ALL(v))
typedef long long ll;
using namespace std;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using vs = vector<string>;
using vvs = vector<vector<string>>;

void solve() {

  ll H, W, ch, cw, dh, dw;
  cin >> H >> W;
  cin >> ch >> cw;
  cin >> dh >> dw;
  vs S(H);
  rep(i, H) cin >> S[i];
  ll dir[] = {1, 0, -1, 0, 1};

  queue<pair<ll, ll>> q;
  vector<vector<ll>> dp(H, vector<ll>(W, INF));
  dp[ch - 1][cw - 1] = 0;
  q.push(make_pair(ch - 1, cw - 1));
  ll x, y, nx, ny;

  while (!q.empty()) {
    x = q.front().first;
    y = q.front().second;
    q.pop();
    rep2(i, -2, 3) {
      rep2(j, -2, 3) {
        nx = x + i;
        ny = y + j;
        if (ny < 0 || nx < 0 || nx >= H || ny >= W)
          continue;
        if (S[nx][ny] == '#')
          continue;
        if (dp[nx][ny] > dp[x][y] + 1) {
          dp[nx][ny] = dp[x][y] + 1;
          q.push(make_pair(nx, ny));
        }
      }
    }
    rep(i, 4) {
      ny = y + dir[i];
      nx = x + dir[i + 1];
      if (ny < 0 || nx < 0 || nx >= H || ny >= W)
        continue;
      if (S[nx][ny] == '#')
        continue;
      if (dp[nx][ny] > dp[x][y]) {
        dp[nx][ny] = dp[x][y];
        q.push(make_pair(nx, ny));
      }
    }
  }

  ll ans = dp[dh - 1][dw - 1];
  if (ans == INF)
    print(-1);
  else
    print(ans);
}

int main() {

  fastio;
  solve();

  return 0;
}