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

#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define rep2(i, s, n) for (ll i = s; i < (ll)(n); i++)
#define repr(i, n) for (ll i = n; i >= 0; i--)
#define pb push_back
#define COUT(x) cout << (x) << "\\n"
#define COUTF(x) cout << setprecision(15) << (x) << "\\n"
#define ENDL cout << "\\n"
#define DF(x) x.erase(x.begin())
#define ALL(x) x.begin(), x.end()
#define SORT(x) sort(ALL(x))
#define REVERSE(x) reverse(ALL(x))
#define ANS cout << ans << "\\n"
#define init()                                                                 \\
  cin.tie(0);                                                                  \\
  ios::sync_with_stdio(false)
#define LINE cerr << "[debug] line: " << __LINE__ << "\\n";
#define debug(x) cerr << "[debug] " << #x << ": " << x << "\\n";
#define debugV(v)                                                              \\
  cerr << "[debugV] " << #v << ":";                                            \\
  rep(i, v.size()) cerr << " " << v[i];                                        \\
  cerr << "\\n";
using namespace std;
using ll = long long;
using ld = long double;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using mll = map<ll, ll>;
using qll = queue<ll>;
using P = pair<ll, ll>;
using vp = vector<P>;
using vs = vector<string>;

template <typename T> inline istream &operator>>(istream &i, vector<T> &v) {
  rep(j, v.size()) i >> v[j];
  return i;
}

template <typename T1, typename T2>
inline istream &operator>>(istream &i, pair<T1, T2> &v) {
  return i >> v.first >> v.second;
}

constexpr ll INF = 0x3f3f3f3f3f3f3f3f;
constexpr ld PI = 3.141592653589793238462643383279;
ll get_digit(ll x) { return to_string(x).size(); }

ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }

ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}

template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

vector<P> factorize(ll n) {
  vector<P> result;
  for (ll i = 2; i * i <= n; ++i) {
    if (n % i == 0) {
      result.pb({i, 0});
      while (n % i == 0) {
        n /= i;
        result.back().second++;
      }
    }
  }
  if (n != 1) {
    result.pb({n, 1});
  }
  return result;
}

vll divisor(ll n) {
  vll ret;
  for (ll i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      ret.push_back(i);
      if (i * i != n)
        ret.push_back(n / i);
    }
  }
  SORT(ret);
  return (ret);
}

const int mod = 1000000007;
struct mint {
  ll x;
  mint(ll x = 0) : x((x % mod + mod) % mod) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const { return mint(*this) += a; }
  mint operator-(const mint a) const { return mint(*this) -= a; }
  mint operator*(const mint a) const { return mint(*this) *= a; }
  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(mod - 2); }
  mint &operator/=(const mint a) { return *this *= a.inv(); }
  mint operator/(const mint a) const { return mint(*this) /= a; }
};
istream &operator>>(istream &is, const mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }

struct combination {
  vector<mint> fact, ifact;
  combination(ll n) : fact(n + 1), ifact(n + 1) {
    assert(n < mod);
    fact[0] = 1;
    for (ll i = 1; i <= n; ++i) {
      fact[i] = fact[i - 1] * i;
    }
    ifact[n] = fact[n].inv();
    for (ll i = n; i >= 1; --i) {
      ifact[i - 1] = ifact[i] * i;
    }
  }
  mint operator()(ll n, ll k) {
    if (k < 0 || k > n)
      return 0;
    return fact[n] * ifact[k] * ifact[n - k];
  }
};

const int di[] = {-1, 0, 1, 0};
const int dj[] = {0, -1, 0, 1};

signed main() {
  init();
  ll H, W, K, si, sj, ti, tj;
  cin >> H >> W >> K >> si >> sj >> ti >> tj;
  si--;
  sj--;
  ti--;
  tj--;
  vs S(H);
  cin >> S;

  auto toId = [&](ll i, ll j, ll v) { return (i * W + j) * 4 + v; };
  auto modceil = [&](ll x) { return (x + K - 1) / K * K; };
  vector<ll> dist(H * W * 4, INF);

  priority_queue<P, vector<P>, greater<P>> q;
  auto push = [&](ll i, ll j, ll v, ll x) {
    ll id = toId(i, j, v);
    if (dist[id] <= x)
      return;
    dist[id] = x;
    q.emplace(x, id);
  };

  rep(v, 4) push(si, sj, v, 0);

  while (!q.empty()) {
    ll d = q.top().first;
    ll id = q.top().second;
    q.pop();
    if (dist[id] != d)
      continue;
    ll i = id / W / 4;
    ll j = id / 4 % W;
    ll v = id % 4;
    rep(nv, 4) push(i, j, nv, modceil(d));
    {
      ll ni = i + di[v], nj = j + dj[v];
      if (0 <= ni and ni < H and 0 <= nj and nj < W) {
        if (S[ni][nj] == '.') {
          push(ni, nj, v, d + 1);
        }
      }
    }
  }

  ll ans = INF;
  rep(v, 4) chmin(ans, dist[toId(ti, tj, v)]);
  if (ans == INF) {
    COUT(-1);
    return 0;
  }

  ans = modceil(ans) / K;
  ANS;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define rep2(i, s, n) for (ll i = s; i < (ll)(n); i++)
#define repr(i, n) for (ll i = n; i >= 0; i--)
#define pb push_back
#define COUT(x) cout << (x) << "\\n"
#define COUTF(x) cout << setprecision(15) << (x) << "\\n"
#define ENDL cout << "\\n"
#define DF(x) x.erase(x.begin())
#define ALL(x) x.begin(), x.end()
#define SORT(x) sort(ALL(x))
#define REVERSE(x) reverse(ALL(x))
#define ANS cout << ans << "\\n"
#define init()                                                                 \\
  cin.tie(0);                                                                  \\
  ios::sync_with_stdio(false)
#define LINE cerr << "[debug] line: " << __LINE__ << "\\n";
#define debug(x) cerr << "[debug] " << #x << ": " << x << "\\n";
#define debugV(v)                                                              \\
  cerr << "[debugV] " << #v << ":";                                            \\
  rep(i, v.size()) cerr << " " << v[i];                                        \\
  cerr << "\\n";
using namespace std;
using ll = long long;
using ld = long double;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using mll = map<ll, ll>;
using qll = queue<ll>;
using P = pair<ll, ll>;
using vp = vector<P>;
using vs = vector<string>;

template <typename T> inline istream &operator>>(istream &i, vector<T> &v) {
  rep(j, v.size()) i >> v[j];
  return i;
}

template <typename T1, typename T2>
inline istream &operator>>(istream &i, pair<T1, T2> &v) {
  return i >> v.first >> v.second;
}

constexpr ll INF = 0x3f3f3f3f3f3f3f3f;
constexpr ld PI = 3.141592653589793238462643383279;
ll get_digit(ll x) { return to_string(x).size(); }

ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }

ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}

template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

vector<P> factorize(ll n) {
  vector<P> result;
  for (ll i = 2; i * i <= n; ++i) {
    if (n % i == 0) {
      result.pb({i, 0});
      while (n % i == 0) {
        n /= i;
        result.back().second++;
      }
    }
  }
  if (n != 1) {
    result.pb({n, 1});
  }
  return result;
}

vll divisor(ll n) {
  vll ret;
  for (ll i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      ret.push_back(i);
      if (i * i != n)
        ret.push_back(n / i);
    }
  }
  SORT(ret);
  return (ret);
}

const int mod = 1000000007;
struct mint {
  ll x;
  mint(ll x = 0) : x((x % mod + mod) % mod) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const { return mint(*this) += a; }
  mint operator-(const mint a) const { return mint(*this) -= a; }
  mint operator*(const mint a) const { return mint(*this) *= a; }
  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(mod - 2); }
  mint &operator/=(const mint a) { return *this *= a.inv(); }
  mint operator/(const mint a) const { return mint(*this) /= a; }
};
istream &operator>>(istream &is, const mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }

struct combination {
  vector<mint> fact, ifact;
  combination(ll n) : fact(n + 1), ifact(n + 1) {
    assert(n < mod);
    fact[0] = 1;
    for (ll i = 1; i <= n; ++i) {
      fact[i] = fact[i - 1] * i;
    }
    ifact[n] = fact[n].inv();
    for (ll i = n; i >= 1; --i) {
      ifact[i - 1] = ifact[i] * i;
    }
  }
  mint operator()(ll n, ll k) {
    if (k < 0 || k > n)
      return 0;
    return fact[n] * ifact[k] * ifact[n - k];
  }
};

signed main() {
  init();
  ll H, W, K, x1, y1, x2, y2;
  cin >> H >> W >> K >> x1 >> y1 >> x2 >> y2;
  x1--;
  y1--;
  x2--;
  y2--;
  vs S(H);
  cin >> S;

  vvll done(H, vll(W, -1));
  queue<pair<P, ll>> q;
  q.push({{x1, y1}, 0});
  done[x1][y1] = 0;

  while (!q.empty() && done[x2][y2] == -1) {
    pair<P, ll> a = q.front();
    P c = a.first;
    q.pop();
    bool up_flag = true;
    bool down_flag = true;
    bool left_flag = true;
    bool right_flag = true;
    rep2(i, 1, K + 1) {
      if (!up_flag and !down_flag and !left_flag and !right_flag)
        break;
      if (up_flag) {
        ll up = c.first - i;
        if (up >= 0) {
          if (S[up][c.second] == '@')
            up_flag = false;
          if (done[up][c.second] == -1 && up_flag) {
            done[up][c.second] = a.second + 1;
            q.push({{up, c.second}, a.second + 1});
          }
        } else {
          up_flag = false;
        }
      }

      if (down_flag) {
        ll down = c.first + i;
        if (down <= H - 1) {
          if (S[down][c.second] == '@')
            down_flag = false;
          if (done[down][c.second] == -1 && down_flag) {
            done[down][c.second] = a.second + 1;
            q.push({{down, c.second}, a.second + 1});
          }
        } else {
          down_flag = false;
        }
      }

      if (left_flag) {
        ll left = c.second - i;
        if (left >= 0) {
          if (S[c.first][left] == '@')
            left_flag = false;
          if (done[c.first][left] == -1 && left_flag) {
            done[c.first][left] = a.second + 1;
            q.push({{c.first, left}, a.second + 1});
          }
        } else {
          left_flag = false;
        }
      }

      if (right_flag) {
        ll right = c.second + i;
        if (right <= W - 1) {
          if (S[c.first][right] == '@')
            right_flag = false;
          if (done[c.first][right] == -1 && right_flag) {
            done[c.first][right] = a.second + 1;
            q.push({{c.first, right}, a.second + 1});
          }
        } else {
          right_flag = false;
        }
      }
    }
  }

  // rep(i, H) {
  //   rep(j, W) {
  //     cout << done[i][j] << " ";
  //   }
  //   ENDL;
  // }

  COUT(done[x2][y2]);
  return 0;
}