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

#include <bits/stdc++.h>
// #include <atcoder/all>
#define ll long long int
#define ld long double
#define yorn(f) std::cout << ((f) ? "Yes" : "No") << endl;
#define YORN(f) std::cout << ((f) ? "YES" : "NO") << endl;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repi(i, n) for (ll i = 1; i < (ll)(n); i++)
#define all(x) (x).begin(), (x).end()
#define put(x) std::cout << x << endl;
#define println(x) std::cout << x << endl;
template <typename T> bool mins(T &x, const T &y) {
  if (x > y) {
    x = y;
    return true;
  } else
    return false;
}
template <typename T> bool maxs(T &x, const T &y) {
  if (x < y) {
    x = y;
    return true;
  } else
    return false;
}
using namespace std;
// using namespace atcoder;
void Main() {
  ll H, W, K;
  cin >> H >> W >> K;

  ll sx, sy;
  cin >> sy >> sx;
  sx--;
  sy--;
  ll gx, gy;
  cin >> gy >> gx;
  gx--;
  gy--;

  vector<string> S(H);
  rep(i, H) cin >> S[i];

  // BFS
  ll INF = 1e18;
  vector<vector<ll>> d(H, vector<ll>(W, INF));

  typedef pair<ll, ll> P;
  queue<P> que;

  que.push(P(sx, sy));
  d[sy][sx] = 0;

  int dx[] = {-1, 0, 1, 0};
  int dy[] = {0, -1, 0, 1};

  // BFS: グリッドの最短路
  while (que.empty() == false) {
    P q = que.front();
    ll x = q.first;
    ll y = q.second;
    que.pop();

    rep(i, 4) {
      repi(j, K + 1) {
        ll x2 = dx[i] * j + x;
        ll y2 = dy[i] * j + y;
        if (x2 < 0 || W <= x2 || y2 < 0 || H <= y2) {
          break;
        }
        if (S[y2][x2] == '@') {
          break;
        }
        if (d[y2][x2] <= d[y][x]) {
          break;
        }
        if (d[y2][x2] != INF) {
          // already visited
          continue;
        }
        d[y2][x2] = d[y][x] + 1;
        que.push(P(x2, y2));
      }
    }
  }

  ll res = d[gy][gx];
  if (res == INF) {
    put(-1);
  } else {
    put(res);
  }
}
signed main() {
  Main();
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
// #include <atcoder/all>
#define ll long long int
#define ld long double
#define yorn(f) std::cout << ((f) ? "Yes" : "No") << endl;
#define YORN(f) std::cout << ((f) ? "YES" : "NO") << endl;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repi(i, n) for (ll i = 1; i < (ll)(n); i++)
#define all(x) (x).begin(), (x).end()
#define put(x) std::cout << x << endl;
#define println(x) std::cout << x << endl;
template <typename T> bool mins(T &x, const T &y) {
  if (x > y) {
    x = y;
    return true;
  } else
    return false;
}
template <typename T> bool maxs(T &x, const T &y) {
  if (x < y) {
    x = y;
    return true;
  } else
    return false;
}
using namespace std;
// using namespace atcoder;
void Main() {
  ll H, W, K;
  cin >> H >> W >> K;

  ll sx, sy;
  cin >> sy >> sx;
  sx--;
  sy--;
  ll gx, gy;
  cin >> gy >> gx;
  gx--;
  gy--;

  vector<string> S(H);
  rep(i, H) cin >> S[i];

  // BFS
  ll INF = 1e18;
  vector<vector<ll>> d(H, vector<ll>(W, INF));

  typedef pair<ll, ll> P;
  queue<P> que;

  que.push(P(sx, sy));
  d[sy][sx] = 0;

  int dx[] = {-1, 0, 1, 0};
  int dy[] = {0, -1, 0, 1};

  // BFS: グリッドの最短路
  while (que.empty() == false) {
    P q = que.front();
    ll x = q.first;
    ll y = q.second;
    que.pop();

    rep(i, 4) {
      repi(j, K + 1) {
        ll x2 = dx[i] * j + x;
        ll y2 = dy[i] * j + y;
        if (x2 < 0 || W <= x2 || y2 < 0 || H <= y2) {
          break;
        }
        if (S[y2][x2] == '@') {
          break;
        }
        if (d[y2][x2] != INF) {
          // already visited
          continue;
        }
        d[y2][x2] = d[y][x] + 1;
        que.push(P(x2, y2));
      }
    }
  }

  ll res = d[gy][gx];
  if (res == INF) {
    put(-1);
  } else {
    put(res);
  }
}
signed main() {
  Main();
  return 0;
}