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() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  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
  priority_queue<tuple<ll, ll, ll>> que;
  vector<vector<ll>> dp(H, vector<ll>(W, INF));
  vector<vector<int>> checked(H, vector<int>(W, 0));

  que.push({0, 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.top();
    que.pop();
    ll cost, vx, vy;
    tie(cost, vx, vy) = v;
    if (checked[vy][vx])
      continue;
    if (vx == gx && vy == gy)
      break;
    checked[vy][vx] = 1;
    cost *= -1;
    rep(i, 4) {
      if (movable(vx + dx[i], vy + dy[i]) &&
          chmin(dp[vy + dy[i]][vx + dx[i]], cost)) {
        que.push({-cost, 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], cost + 1))
          que.push({-cost - 1, vx + i, vy + j});
        if (movable(vx - i, vy + j) && chmin(dp[vy + j][vx - i], cost + 1))
          que.push({-cost - 1, vx - i, vy + j});
        if (movable(vx + i, vy - j) && chmin(dp[vy - j][vx + i], cost + 1))
          que.push({-cost - 1, vx + i, vy - j});
        if (movable(vx - i, vy - j) && chmin(dp[vy - j][vx - i], cost + 1))
          que.push({-cost - 1, 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() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  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
  priority_queue<tuple<ll, ll, ll>> que;
  vector<vector<ll>> dp(H, vector<ll>(W, INF));
  vector<vector<int>> checked(H, vector<int>(W, 0));

  que.push({0, 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.top();
    que.pop();
    ll cost, vx, vy;
    tie(cost, vx, vy) = v;
    if (checked[vy][vx])
      continue;
    if (vx == gx && vy == gy)
      break;
    checked[vy][vx] = 1;
    cost *= -1;
    rep(i, 4) {
      if (movable(vx + dx[i], vy + dy[i]) &&
          chmin(dp[vy + dy[i]][vx + dx[i]], cost)) {
        que.push({-cost, 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], cost + 1))
          que.push({-cost - 1, vx + i, vy + j});
        if (movable(vx - i, vy + j) && chmin(dp[vy + j][vx - i], cost + 1))
          que.push({-cost - 1, vx - i, vy + j});
        if (movable(vx + i, vy - j) && chmin(dp[vy - j][vx + i], cost + 1))
          que.push({-cost - 1, vx + i, vy - j});
        if (movable(vx - i, vy - j) && chmin(dp[vy - j][vx - i], cost + 1))
          que.push({-cost - 1, vx - i, vy - j});
      }
    }
  }

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