Problem p02644 (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 rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define rep2(i, start, end) for (int i = (int)start; i <= (int)end; i++)
#define vrep(i, n) for (int i = (int)n - 1; i >= 0; i--)
#define vrep1(i, n) for (int i = (int)n; i > 0; i--)
typedef long long ll;
typedef pair<int, int> pint;
typedef pair<long, long> plong;
typedef vector<int> vint;
typedef vector<long> vlong;

const double pi = M_PI;
const int INF10 = 1000000001;
const long long INF18 = 1e18 + 1;
const int mod = 1000000007;
// const int mod = 998244353;

struct edge {
  edge(int i, long c = 1) {
    to = i;
    cost = c;
  }
  int to;
  long cost;
};

// bを何回足せばaを超えるか(O(a/b))
// a+b-1/bとすればよし

// 2進数表示したときの最高桁(O(log n))
int bi_max(long n) {
  int m = 0;
  for (m; (1 << m) <= n; m++)
    ;
  m = m - 1;
  return m;
}
// bi_eに二進数表示したやつを代入(O(log^2 n))
// bitset<N>
// a(n)でnの二進数表示が得られて、a[i]=0or1でi番目のfragが立ってるかわかる x^n
// mod m (nが負の時は０)(O(log n))
long myPow(long x, long n, long m = mod) {
  if (n < 0)
    return 0;
  if (n == 0)
    return 1;
  if (n % 2 == 0)
    return myPow(x * x % m, n / 2, m);
  else
    return x * myPow(x, n - 1, m) % m;
}
// auto mod int
// https://youtu.be/L8grWxBlIZ4?t=9858
// https://youtu.be/ERZuLAxZffQ?t=4807 : optimize
// https://youtu.be/8uowVvQ_-Mo?t=1329 : division
struct mint {
  ll x; // typedef long long ll;
  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; }
  bool operator!=(const int a) { return this->x != a; }
  bool operator==(const int a) { return this->x == a; }
};
istream &operator>>(istream &is, const mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }
// combination mod prime
// https://www.youtube.com/watch?v=8uowVvQ_-Mo&feature=youtu.be&t=1619
struct combination {
  vector<mint> fact, ifact;
  combination(int n) : fact(n + 1), ifact(n + 1) {
    assert(n < mod);
    fact[0] = 1;
    for (int i = 1; i <= n; ++i)
      fact[i] = fact[i - 1] * i;
    ifact[n] = fact[n].inv();
    for (int i = n; i >= 1; --i)
      ifact[i - 1] = ifact[i] * i;
  }
  mint operator()(int n, int k) {
    if (k < 0 || k > n)
      return 0;
    return fact[n] * ifact[k] * ifact[n - k];
  }
};
template <class T> void maxin(T &a, T b) { a = max(a, b); }
template <class T> void minin(T &a, T b) { a = min(a, b); }
template <class M, class N> constexpr common_type_t<M, N> gcd(M a, N b) {
  a = abs(a);
  b = abs(b);
  if (a < b)
    return gcd(b, a);
  M r;
  while ((r = a % b)) {
    a = b;
    b = r;
  }
  return b;
}
template <class M, class N> constexpr common_type_t<M, N> lcm(M a, N b) {
  return a * b / gcd(a, b);
}

const int N_MAX = 1000005;
ll h, w, k;
int sx, sy, ex, ey;
vector<char> s[N_MAX];

ll bfs() {
  vector<vector<ll>> d(h + 1, vector<ll>(w + 1, INF18));
  queue<pair<int, int>> que;
  int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
  d[sx - 1][sy - 1] = 0;
  que.push({sx - 1, sy - 1});
  while (que.size()) {
    pint now = que.front();
    que.pop();
    if (now.first == ex - 1 && now.second == ey - 1)
      break;

    rep(i, 4) {
      rep1(j, k) {
        int nx = now.first + dx[i] * j, ny = now.second + dy[i] * j;
        if (nx >= 0 && nx < h && ny >= 0 && ny < w && s[nx][ny] != '@') {
          if (d[nx][ny] == INF18) {
            d[nx][ny] = d[now.first][now.second] + 1;
            que.push({nx, ny});
          } else if (d[nx][ny] <= d[now.first][now.second])
            break;
        } else
          break;
      }
    }
  }
  return d[ex - 1][ey - 1];
}

int main() {
  int x, y;
  // 入力
  cin >> h >> w >> k;
  cin >> sx >> sy >> ex >> ey;
  char a;
  rep(i, h) {
    rep(j, w) {
      cin >> a;
      s[i].push_back(a);
    }
  }
  // 処理
  auto ans = bfs();
  // 出力
  if (ans == INF18)
    cout << -1 << endl;
  else
    cout << ans << endl;
}

Error: TLE

Fixed submission:

//
#include <bits/stdc++.h>

using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define rep2(i, start, end) for (int i = (int)start; i <= (int)end; i++)
#define vrep(i, n) for (int i = (int)n - 1; i >= 0; i--)
#define vrep1(i, n) for (int i = (int)n; i > 0; i--)
typedef long long ll;
typedef pair<int, int> pint;
typedef pair<long, long> plong;
typedef vector<int> vint;
typedef vector<long> vlong;

const double pi = M_PI;
const int INF10 = 1000000001;
const long long INF18 = 1e18 + 1;
const int mod = 1000000007;
// const int mod = 998244353;

struct edge {
  edge(int i, long c = 1) {
    to = i;
    cost = c;
  }
  int to;
  long cost;
};

// bを何回足せばaを超えるか(O(a/b))
// a+b-1/bとすればよし

// 2進数表示したときの最高桁(O(log n))
int bi_max(long n) {
  int m = 0;
  for (m; (1 << m) <= n; m++)
    ;
  m = m - 1;
  return m;
}
// bi_eに二進数表示したやつを代入(O(log^2 n))
// bitset<N>
// a(n)でnの二進数表示が得られて、a[i]=0or1でi番目のfragが立ってるかわかる x^n
// mod m (nが負の時は０)(O(log n))
long myPow(long x, long n, long m = mod) {
  if (n < 0)
    return 0;
  if (n == 0)
    return 1;
  if (n % 2 == 0)
    return myPow(x * x % m, n / 2, m);
  else
    return x * myPow(x, n - 1, m) % m;
}
// auto mod int
// https://youtu.be/L8grWxBlIZ4?t=9858
// https://youtu.be/ERZuLAxZffQ?t=4807 : optimize
// https://youtu.be/8uowVvQ_-Mo?t=1329 : division
struct mint {
  ll x; // typedef long long ll;
  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; }
  bool operator!=(const int a) { return this->x != a; }
  bool operator==(const int a) { return this->x == a; }
};
istream &operator>>(istream &is, const mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }
// combination mod prime
// https://www.youtube.com/watch?v=8uowVvQ_-Mo&feature=youtu.be&t=1619
struct combination {
  vector<mint> fact, ifact;
  combination(int n) : fact(n + 1), ifact(n + 1) {
    assert(n < mod);
    fact[0] = 1;
    for (int i = 1; i <= n; ++i)
      fact[i] = fact[i - 1] * i;
    ifact[n] = fact[n].inv();
    for (int i = n; i >= 1; --i)
      ifact[i - 1] = ifact[i] * i;
  }
  mint operator()(int n, int k) {
    if (k < 0 || k > n)
      return 0;
    return fact[n] * ifact[k] * ifact[n - k];
  }
};
template <class T> void maxin(T &a, T b) { a = max(a, b); }
template <class T> void minin(T &a, T b) { a = min(a, b); }
template <class M, class N> constexpr common_type_t<M, N> gcd(M a, N b) {
  a = abs(a);
  b = abs(b);
  if (a < b)
    return gcd(b, a);
  M r;
  while ((r = a % b)) {
    a = b;
    b = r;
  }
  return b;
}
template <class M, class N> constexpr common_type_t<M, N> lcm(M a, N b) {
  return a * b / gcd(a, b);
}

const int N_MAX = 1000005;
ll h, w, k;
int sx, sy, ex, ey;
vector<char> s[N_MAX];

ll bfs() {
  vector<vector<ll>> d(h + 1, vector<ll>(w + 1, INF18));
  queue<pair<int, int>> que;
  int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
  d[sx - 1][sy - 1] = 0;
  que.push({sx - 1, sy - 1});
  while (que.size()) {
    pint now = que.front();
    que.pop();
    if (now.first == ex - 1 && now.second == ey - 1)
      break;

    rep(i, 4) {
      rep1(j, k) {
        int nx = now.first + dx[i] * j, ny = now.second + dy[i] * j;
        if (nx >= 0 && nx < h && ny >= 0 && ny < w && s[nx][ny] != '@') {
          if (d[nx][ny] == INF18) {
            d[nx][ny] = d[now.first][now.second] + 1;
            que.push({nx, ny});
          }
        } else
          break;
      }
    }
  }
  return d[ex - 1][ey - 1];
}

int main() {
  int x, y;
  // 入力
  cin >> h >> w >> k;
  cin >> sx >> sy >> ex >> ey;
  char a;
  rep(i, h) {
    rep(j, w) {
      cin >> a;
      s[i].push_back(a);
    }
  }
  // 処理
  auto ans = bfs();
  // 出力
  if (ans == INF18)
    cout << -1 << endl;
  else
    cout << ans << endl;
}