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

#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using ll = int;
const ll MOD = 1000000007;
// const ll MOD=998244353;
const long long INF = 1 << 30;
const double pi = acos(-1.0);

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

ll H, W;
vector<string> grid;

bool movable(ll gx, ll gy) {
  if (gx < 0 || gx >= W || gy < 0 || gy >= H)
    return false;
  if (grid[gy][gx] == '#')
    return false;

  return true;
}

int main() {
  cin >> H >> W;
  ll sy, sx;
  cin >> sy >> sx;
  sx--;
  sy--;
  ll gy, gx;
  cin >> gy >> gx;
  gx--;
  gy--;
  grid = vector<string>(H);
  rep(i, H) cin >> grid[i];

  // bfs
  deque<pair<ll, ll>> que;
  vector<vector<ll>> dp(H, vector<ll>(W, INF));

  que.push_front({sx, sy});
  dp[sy][sx] = 0;

  vector<ll> dx = {-1, 0, 1, 0};
  vector<ll> dy = {0, -1, 0, 1};
  while (!que.empty()) {
    auto v = que.front();
    que.pop_front();
    ll vx, vy;
    tie(vx, vy) = v;
    rep(i, 4) {
      if (movable(vx + dx[i], vy + dy[i]) &&
          chmin(dp[vy + dy[i]][vx + dx[i]], dp[vy][vx])) {
        que.push_front({vx + dx[i], vy + dy[i]});
      }
    }
    if (movable(vx + dx[0], vy + dy[0]) && movable(vx + dx[1], vy + dy[1]) &&
        movable(vx + dx[2], vy + dy[2]) && movable(vx + dx[3], vy + dy[3]))
      continue;
    rep(i, 3) {
      rep(j, 3) {
        if (abs(i) + abs(j) <= 1)
          continue;
        if (movable(vx + i, vy + j) &&
            chmin(dp[vy + j][vx + i], dp[vy][vx] + 1))
          que.push_back({vx + i, vy + j});
        if (movable(vx - i, vy + j) &&
            chmin(dp[vy + j][vx - i], dp[vy][vx] + 1))
          que.push_back({vx - i, vy + j});
        if (movable(vx + i, vy - j) &&
            chmin(dp[vy - j][vx + i], dp[vy][vx] + 1))
          que.push_back({vx + i, vy - j});
        if (movable(vx - i, vy - j) &&
            chmin(dp[vy - j][vx - i], dp[vy][vx] + 1))
          que.push_back({vx - i, vy - j});
      }
    }
  }

  if (dp[gy][gx] == INF)
    cout << -1 << endl;
  else
    cout << dp[gy][gx] << endl;
  return 0;
}


Error: TLE

Fixed submission:

#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using ll = int;
const ll MOD = 1000000007;
// const ll MOD=998244353;
const long long INF = 1 << 30;
const double pi = acos(-1.0);

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

ll H, W;
vector<string> grid;

bool movable(ll gx, ll gy) {
  if (gx < 0 || gx >= W || gy < 0 || gy >= H)
    return false;
  if (grid[gy][gx] == '#')
    return false;

  return true;
}

int main() {
  cin >> H >> W;
  ll sy, sx;
  cin >> sy >> sx;
  sx--;
  sy--;
  ll gy, gx;
  cin >> gy >> gx;
  gx--;
  gy--;
  grid = vector<string>(H);
  rep(i, H) cin >> grid[i];

  // bfs
  queue<pair<ll, ll>> que;
  vector<vector<ll>> dp(H, vector<ll>(W, INF));

  que.push({sx, sy});
  dp[sy][sx] = 0;

  vector<ll> dx = {-1, 0, 1, 0};
  vector<ll> dy = {0, -1, 0, 1};
  while (!que.empty()) {
    auto v = que.front();
    que.pop();
    ll vx, vy;
    tie(vx, vy) = v;
    rep(i, 4) {
      if (movable(vx + dx[i], vy + dy[i]) &&
          chmin(dp[vy + dy[i]][vx + dx[i]], dp[vy][vx])) {
        que.push({vx + dx[i], vy + dy[i]});
      }
    }
    if (movable(vx + dx[0], vy + dy[0]) && movable(vx + dx[1], vy + dy[1]) &&
        movable(vx + dx[2], vy + dy[2]) && movable(vx + dx[3], vy + dy[3]))
      continue;
    rep(i, 3) {
      rep(j, 3) {
        if (abs(i) + abs(j) <= 1)
          continue;
        if (movable(vx + i, vy + j) &&
            chmin(dp[vy + j][vx + i], dp[vy][vx] + 1))
          que.push({vx + i, vy + j});
        if (movable(vx - i, vy + j) &&
            chmin(dp[vy + j][vx - i], dp[vy][vx] + 1))
          que.push({vx - i, vy + j});
        if (movable(vx + i, vy - j) &&
            chmin(dp[vy - j][vx + i], dp[vy][vx] + 1))
          que.push({vx + i, vy - j});
        if (movable(vx - i, vy - j) &&
            chmin(dp[vy - j][vx - i], dp[vy][vx] + 1))
          que.push({vx - i, vy - j});
      }
    }
  }

  if (dp[gy][gx] == INF)
    cout << -1 << endl;
  else
    cout << dp[gy][gx] << endl;
  return 0;
}