alexjercan/bugnet huggingface dataset:
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 < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define all(x) (x).begin(), (x).end()
#define V vector
typedef V<int> vi;
typedef V<vi> vvi;
typedef long long ll;
typedef pair<int, int> P;
typedef tuple<int, int, int> T;
constexpr int INF = INT_MAX >> 1;
constexpr ll LINF = 5000000000000000LL;
constexpr int MOD = 1000000007;

int main() {

  int h, w, k, sy, sx, gy, gx;
  cin >> h >> w >> k >> sy >> sx >> gy >> gx;
  sy--, sx--, gy--, gx--;
  V<string> s(h);
  rep(i, h) cin >> s[i];

  vvi dist(h, vi(w, INF));
  dist[sy][sx] = 0;
  queue<P> q;
  q.push(P(sy, sx));

  while (!q.empty()) {
    P p = q.front();
    q.pop();
    int y = p.first, x = p.second;
    vi dy = {0, 0, 1, -1};
    vi dx = {1, -1, 0, 0};
    rep(i, 4) rep1(j, k) {
      int ey, ex;
      ey = y + dy[i] * j;
      ex = x + dx[i] * j;
      if (ey < 0 || h <= ey)
        break;
      if (ex < 0 || w <= ex)
        break;
      if (s[ey][ex] == '@')
        break;
      if (dist[ey][ex] != INF && dist[ey][ex] < dist[y][x] + 1)
        break;
      if (dist[ey][ex] != INF && dist[ey][ex] <= dist[y][x] + 1)
        continue;
      dist[ey][ex] = dist[y][x] + 1;
      q.push(P(ey, ex));
    }
  }

  if (dist[gy][gx] == INF)
    dist[gy][gx] = -1;
  cout << dist[gy][gx] << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define all(x) (x).begin(), (x).end()
#define V vector
typedef V<int> vi;
typedef V<vi> vvi;
typedef long long ll;
typedef pair<int, int> P;
typedef tuple<int, int, int> T;
constexpr int INF = INT_MAX >> 1;
constexpr ll LINF = 5000000000000000LL;
constexpr int MOD = 1000000007;

int main() {

  int h, w, k, sy, sx, gy, gx;
  cin >> h >> w >> k >> sy >> sx >> gy >> gx;
  sy--, sx--, gy--, gx--;
  V<string> s(h);
  rep(i, h) cin >> s[i];

  vvi dist(h, vi(w, INF));
  dist[sy][sx] = 0;
  queue<P> q;
  q.push(P(sy, sx));

  while (!q.empty()) {
    P p = q.front();
    q.pop();
    int y = p.first, x = p.second;
    vi dy = {0, 0, 1, -1};
    vi dx = {1, -1, 0, 0};
    rep(i, 4) rep1(j, k) {
      int ey, ex;
      ey = y + dy[i] * j;
      ex = x + dx[i] * j;
      if (ey < 0 || h <= ey)
        break;
      if (ex < 0 || w <= ex)
        break;
      if (s[ey][ex] == '@')
        break;
      if (dist[ey][ex] != INF && dist[ey][ex] <= dist[y][x] + 1)
        continue;
      dist[ey][ex] = dist[y][x] + 1;
      q.push(P(ey, ex));
    }
  }

  if (dist[gy][gx] == INF)
    dist[gy][gx] = -1;
  cout << dist[gy][gx] << endl;
}
Problem p02644 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

int main() {
  size_t H, W, K, x1, y1, x2, y2;
  cin >> H >> W >> K >> x1 >> y1 >> x2 >> y2;
  x1--;
  y1--;
  x2--;
  y2--;
  vector<string> c(H);
  for (string &s : c) {
    cin >> s;
  }

  size_t INIT = H * W + 5;
  vector<vector<size_t>> dp(H, vector<size_t>(W, INIT));
  dp[x1][y1] = 0;
  queue<pair<size_t, size_t>> q;
  q.push(make_pair(x1, y1));
  vector<pair<int, int>> diff{
      make_pair(0, 1),
      make_pair(1, 0),
      make_pair(0, -1),
      make_pair(-1, 0),
  };
  while (!q.empty()) {
    size_t x = q.front().first;
    size_t y = q.front().second;
    q.pop();
    for (pair<int, int> d : diff) {
      for (size_t k = 1; k <= K; k++) {
        size_t x_next = x + k * d.first;
        size_t y_next = y + k * d.second;
        if (x_next < H && y_next < W) {
          if (c[x_next][y_next] == '@') {
            break;
          }
          if (dp[x][y] + 1 < dp[x_next][y_next]) {
            dp[x_next][y_next] = dp[x][y] + 1;
            q.push(make_pair(x_next, y_next));
          } else {
            if (dp[x][y] + 1 > dp[x_next][y_next]) {
              break;
            }
          }
        } else {
          break;
        }
      }
    }
  }
  if (dp[x2][y2] == INIT) {
    cout << -1 << endl;
  } else {
    cout << dp[x2][y2] << endl;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int main() {
  size_t H, W, K, x1, y1, x2, y2;
  cin >> H >> W >> K >> x1 >> y1 >> x2 >> y2;
  x1--;
  y1--;
  x2--;
  y2--;
  vector<string> c(H);
  for (string &s : c) {
    cin >> s;
  }

  size_t INIT = H * W + 5;
  vector<vector<size_t>> dp(H, vector<size_t>(W, INIT));
  dp[x1][y1] = 0;
  queue<pair<size_t, size_t>> q;
  q.push(make_pair(x1, y1));
  vector<pair<int, int>> diff{
      make_pair(0, 1),
      make_pair(1, 0),
      make_pair(0, -1),
      make_pair(-1, 0),
  };
  while (!q.empty()) {
    size_t x = q.front().first;
    size_t y = q.front().second;
    q.pop();
    for (pair<int, int> d : diff) {
      for (size_t k = 1; k <= K; k++) {
        size_t x_next = x + k * d.first;
        size_t y_next = y + k * d.second;
        if (x_next < H && y_next < W) {
          if (c[x_next][y_next] == '@') {
            break;
          }
          if (dp[x][y] + 1 <= dp[x_next][y_next]) {
            dp[x_next][y_next] = dp[x][y] + 1;
            q.push(make_pair(x_next, y_next));
          } else {
            break;
          }
        } else {
          break;
        }
      }
    }
  }
  if (dp[x2][y2] == INIT) {
    cout << -1 << endl;
  } else {
    cout << dp[x2][y2] << endl;
  }
}
Problem p02644 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>
#define maxn 1000010
char mp[maxn], line[maxn];
int d[maxn], r1, c1, r2, c2, H, W, K,
    next[][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}, in[maxn];
struct node {
  int r, c;
} q[maxn * 10];
int main() {
  int i, j, h, t, r, c, nr, nc, p, np;
  scanf("%d%d%d", &H, &W, &K);
  scanf("%d%d%d%d", &r1, &c1, &r2, &c2), r1--, c1--, r2--, c2--;
  for (i = 0; i < H; i++) {
    scanf("%s", line);
    for (j = 0; j < W; j++)
      mp[i * W + j] = line[j];
  }
  for (i = 0; i < H; i++)
    for (j = 0; j < W; j++)
      d[i * W + j] = maxn;
  d[r1 * W + c1] = 0, h = t = 1, q[t].r = r1, q[t].c = c1, t++,
             in[r1 * W + c1] = 1;
  while (h < t) {
    r = q[h].r, c = q[h].c, in[r * W + c] = 0;
    if (r == r2 && c == c2)
      return 0 * printf("%d\\n", d[r * W + c]);
    for (i = 0; i < 4; i++)
      for (j = 1; j <= K; j++) {
        nr = r + next[i][0] * j, nc = c + next[i][1] * j;
        if (0 <= nr && nr < H && 0 <= nc && nc < W) {
          p = r * W + c, np = nr * W + nc;
          if (mp[np] == '@' || d[np] <= d[p])
            break;
          if (d[np] >= d[p] + 1) {
            d[np] = d[p] + 1;
            if (!in[np])
              q[t].r = nr, q[t].c = nc, t++, in[np] = 1;
          }
        } else
          break;
      }
    h++;
  }
  printf("-1\\n");
  return 0;
}

Error: 0

Fixed submission:

#include <stdio.h>
#define maxn 1000010
char mp[maxn], line[maxn];
int d[maxn], r1, c1, r2, c2, H, W, K,
    next[][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
struct node {
  int r, c;
} q[maxn];
int main() {
  int i, j, h, t, r, c, nr, nc, p, np;
  scanf("%d%d%d", &H, &W, &K);
  scanf("%d%d%d%d", &r1, &c1, &r2, &c2), r1--, c1--, r2--, c2--;
  for (i = 0; i < H; i++) {
    scanf("%s", line);
    for (j = 0; j < W; j++)
      mp[i * W + j] = line[j];
  }
  for (i = 0; i < H; i++)
    for (j = 0; j < W; j++)
      d[i * W + j] = maxn;
  d[r1 * W + c1] = 0, h = t = 1, q[t].r = r1, q[t].c = c1, t++;
  while (h < t) {
    r = q[h].r, c = q[h].c;
    if (r == r2 && c == c2)
      return 0 * printf("%d\\n", d[r * W + c]);
    for (i = 0; i < 4; i++)
      for (j = 1; j <= K; j++) {
        nr = r + next[i][0] * j, nc = c + next[i][1] * j;
        if (0 <= nr && nr < H && 0 <= nc && nc < W) {
          p = r * W + c, np = nr * W + nc;
          if (mp[np] == '@' || d[np] <= d[p])
            break;
          if (d[np] >= d[p] + 1)
            d[np] = d[p] + 1, q[t].r = nr, q[t].c = nc, t++;
        } else
          break;
      }
    h++;
  }
  printf("-1\\n");
  return 0;
}
Problem p02644 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#ifdef tabr
#include "library/debug.cpp"
#else
#define debug(...)
#endif

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int h, w, k;
  cin >> h >> w >> k;
  int sx, sy, tx, ty;
  cin >> sx >> sy >> tx >> ty;
  vector<vector<int>> d(h + 2, vector<int>(w + 2, 1e9));
  for (int i = 0; i < w + 2; i++) {
    d[0][i] = d[h + 1][i] = 0;
  }
  for (int i = 1; i <= h; i++) {
    string s;
    cin >> s;
    d[i][0] = d[i][w + 1] = 0;
    for (int j = 0; j < w; j++) {
      if (s[j] == '@') {
        d[i][j + 1] = 0;
      }
    }
  }
  d[sx][sy] = 0;
  queue<pair<int, int>> que;
  que.emplace(sx, sy);
  vector<int> dx = {1, -1, 0, 0}, dy = {0, 0, 1, -1};
  vector<vector<bool>> was(h + 2, vector<bool>(w + 2));
  was[sx][sy] = true;
  while (!que.empty()) {
    int x, y;
    tie(x, y) = que.front();
    que.pop();
    for (int kk = 0; kk < 4; kk++) {
      int i = x, j = y;
      for (int l = 0; l < k; l++) {
        i += dx[kk], j += dy[kk];
        if (d[i][j] <= d[x][y]) {
          break;
        }
        if (was[i][j]) {
          continue;
        }
        was[i][j] = true;
        d[i][j] = d[x][y] + 1;
        if (i == tx && j == ty) {
          cout << d[i][j] << '\\n';
          return 0;
        }
        que.emplace(i, j);
      }
    }
  }
  cout << -1 << '\\n';
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#ifdef tabr
#include "library/debug.cpp"
#else
#define debug(...)
#endif

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int h, w, k;
  cin >> h >> w >> k;
  int sx, sy, tx, ty;
  cin >> sx >> sy >> tx >> ty;
  vector<vector<int>> d(h + 2, vector<int>(w + 2, 1e9));
  for (int i = 0; i < w + 2; i++) {
    d[0][i] = d[h + 1][i] = 0;
  }
  for (int i = 1; i <= h; i++) {
    string s;
    cin >> s;
    d[i][0] = d[i][w + 1] = 0;
    for (int j = 0; j < w; j++) {
      if (s[j] == '@') {
        d[i][j + 1] = 0;
      }
    }
  }
  d[sx][sy] = 0;
  queue<pair<int, int>> que;
  que.emplace(sx, sy);
  vector<int> dx = {1, -1, 0, 0}, dy = {0, 0, 1, -1};
  vector<vector<bool>> was(h + 2, vector<bool>(w + 2));
  was[sx][sy] = true;
  while (!que.empty()) {
    int x, y;
    tie(x, y) = que.front();
    que.pop();
    for (int kk = 0; kk < 4; kk++) {
      int i = x, j = y;
      for (int l = 0; l < k; l++) {
        i += dx[kk], j += dy[kk];
        if (d[i][j] <= d[x][y]) {
          break;
        }
        if (was[i][j]) {
          continue;
        }
        d[i][j] = d[x][y] + 1;
        if (i == tx && j == ty) {
          cout << d[i][j] << '\\n';
          return 0;
        }
        que.emplace(i, j);
      }
    }
  }
  cout << -1 << '\\n';
  return 0;
}
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 < n; ++i)
using ll = long long;
const ll MOD = 1000000007; // 998244353;
const int SIZE = 1e4;
const int INF = 1e9;
const int NaN = -1;
vector<pair<int, int>> rlud = {make_pair(1, 0), make_pair(-1, 0),
                               make_pair(0, 1), make_pair(0, -1)};
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int h, w, k;
  cin >> h >> w >> k;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  y1--;
  x2--;
  y2--;
  vector<int> grid[SIZE];
  rep(i, h) {
    string s;
    cin >> s;
    rep(j, w) { grid[i].push_back(s[j] == '@' ? NaN : INF); }
  }
  queue<pair<int, int>> q;
  q.push(make_pair(x1, y1));
  grid[x1][y1] = 0;
  while (!q.empty()) {
    int x = q.front().first, y = q.front().second;
    q.pop();
    int cost = grid[x][y];
    for (auto &&i : rlud) {
      int dx = x, dy = y;
      dx += i.first;
      dy += i.second;
      int cnt = 0;
      while (dx < h && dy < w && dx >= 0 && dy >= 0) {
        if (grid[dx][dy] == NaN ||
            (grid[dx][dy] != INF && grid[dx][dy] <= cost) || cnt > k)
          break;
        if (grid[dx][dy] == INF)
          q.push(make_pair(dx, dy));
        grid[dx][dy] = cost + 1;
        dx += i.first;
        dy += i.second;
        cnt++;
        if (cnt == k)
          break;
      }
    }
  }

  cout << (grid[x2][y2] == INF ? -1 : grid[x2][y2]) << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; ++i)
using ll = long long;
const ll MOD = 1000000007; // 998244353;
const int SIZE = 1e4;
const int INF = 1e9;
const int NaN = -1;
vector<pair<int, int>> rlud = {make_pair(1, 0), make_pair(-1, 0),
                               make_pair(0, 1), make_pair(0, -1)};
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int h, w, k;
  cin >> h >> w >> k;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  y1--;
  x2--;
  y2--;
  vector<int> grid[SIZE];
  rep(i, h) {
    string s;
    cin >> s;
    rep(j, w) { grid[i].push_back(s[j] == '@' ? NaN : INF); }
  }
  queue<pair<int, int>> q;
  q.push(make_pair(x1, y1));
  grid[x1][y1] = 0;
  while (!q.empty()) {
    int x = q.front().first, y = q.front().second;
    q.pop();
    int cost = grid[x][y];
    for (auto &&i : rlud) {
      int dx = x, dy = y;
      dx += i.first;
      dy += i.second;
      int cnt = 0;
      while (dx < h && dy < w && dx >= 0 && dy >= 0) {
        if (grid[dx][dy] == NaN ||
            (grid[dx][dy] != INF && grid[dx][dy] <= cost) || cnt > k)
          break;
        grid[dx][dy] = cost + 1;
        q.push(make_pair(dx, dy));
        dx += i.first;
        dy += i.second;
        cnt++;
        if (cnt == k)
          break;
      }
    }
  }

  cout << (grid[x2][y2] == INF ? -1 : grid[x2][y2]) << endl;
}
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++)
typedef int64_t Int;
typedef pair<int, int> P;

int main() {
  int H, W, K;
  cin >> H >> W >> K;
  int x1, x2, y1, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  x2--;
  y1--;
  y2--;

  vector<int> dx(4), dy(4);
  dx[0] = 1;
  dx[1] = 0;
  dx[2] = -1;
  dx[3] = 0;
  dy[0] = 0;
  dy[1] = 1;
  dy[2] = 0;
  dy[3] = -1;

  vector<string> lotus(H);

  vector<vector<int>> cnt(H, vector<int>(W, -1));

  rep(i, H) { cin >> lotus[i]; }

  cnt[x1][y1] = 0;

  queue<P> q;
  q.push(make_pair(x1, y1));

  while (!q.empty()) {
    P now = q.front();
    q.pop();
    int nx = now.first, ny = now.second;
    rep(i, 4) {
      rep1(j, K) {
        int tx = nx + dx[i] * j;
        int ty = ny + dy[i] * j;
        if (tx < 0 || tx >= H || ty < 0 || ty >= W)
          break;
        if (lotus[tx][ty] == '@')
          break;
        if (cnt[tx][ty] == -1) {
          cnt[tx][ty] = cnt[nx][ny] + 1;
          q.push(make_pair(tx, ty));
          continue;
        }
        if (cnt[tx][ty] <= cnt[nx][ny])
          break;
        if (cnt[tx][ty] > cnt[nx][ny] + 1)
          q.push(make_pair(tx, ty));
        cnt[tx][ty] = min(cnt[tx][ty], cnt[nx][ny] + 1);
      }
    }
  }

  cout << cnt[x2][y2] << 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++)
typedef int64_t Int;
typedef pair<int, int> P;

int main() {
  int H, W, K;
  cin >> H >> W >> K;
  int x1, x2, y1, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  x2--;
  y1--;
  y2--;

  vector<int> dx(4), dy(4);
  dx[0] = 1;
  dx[1] = 0;
  dx[2] = -1;
  dx[3] = 0;
  dy[0] = 0;
  dy[1] = 1;
  dy[2] = 0;
  dy[3] = -1;

  vector<string> lotus(H);

  vector<vector<int>> cnt(H, vector<int>(W, -1));

  rep(i, H) { cin >> lotus[i]; }

  cnt[x1][y1] = 0;

  queue<P> q;
  q.push(make_pair(x1, y1));

  while (!q.empty()) {
    P now = q.front();
    q.pop();
    int nx = now.first, ny = now.second;
    rep(i, 4) {
      rep1(j, K) {
        int tx = nx + dx[i] * j;
        int ty = ny + dy[i] * j;
        if (tx < 0 || tx >= H || ty < 0 || ty >= W)
          break;
        if (lotus[tx][ty] == '@')
          break;
        if (cnt[tx][ty] == -1) {
          cnt[tx][ty] = cnt[nx][ny] + 1;
          q.push(make_pair(tx, ty));
          continue;
        }
        if (cnt[tx][ty] <= cnt[nx][ny])
          break;
        cnt[tx][ty] = min(cnt[tx][ty], cnt[nx][ny] + 1);
        q.push(make_pair(tx, ty));
      }
    }
  }

  cout << cnt[x2][y2] << endl;
}
Problem p02644 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

const ll INF = 1e18;
const int di[4] = {-1, 0, 1, 0};
const int dj[4] = {0, 1, 0, -1};

int main() {
  int h, w, k, si, sj, gi, gj;
  cin >> h >> w >> k >> si >> sj >> gi >> gj;
  si--;
  sj--;
  gi--;
  gj--;
  vector<string> g(h);
  rep(i, h) cin >> g[i];

  // 各マスについてスタートから到達までのコスト（かく回数）
  vector<vector<ll>> cost(h, vector<ll>(w, INF));
  cost[si][sj] = 0; // スタートマスはコスト0

  // 調べるマスを入れるキュー
  queue<P> q;

  // 調べるマスをキューに追加する関数
  auto add_g = [&](int i, int j) { q.push(make_pair(i, j)); };

  // スタート地点から調べる
  add_g(si, sj);

  while (!q.empty()) { // キューが空でない限りループ
    // キューから調べるマスの座標を取り出す
    P p = q.front();
    int cur_i = p.first;
    int cur_j = p.second;
    const ll c = cost[cur_i][cur_j];
    q.pop();

    // キューに調べるマスを追加
    rep(i, 4) rep(j, k) {
      // 行先のマスの座標
      int to_i = cur_i + di[i] * (j + 1);
      int to_j = cur_j + dj[i] * (j + 1);

      if (h <= to_i || to_i < 0 || w <= to_j || to_j < 0)
        break; // 範囲外だったら追加しない
      if (c >= cost[to_i][to_j])
        break; // 行先のコストの方が小さかったら追加しない
      if (g[to_i][to_j] == '@')
        break; // ハスがあったら追加しない

      if (c + 1 <
          cost[to_i][to_j]) { // 行先のコストをもっと小さくできそうなら追加する
        add_g(to_i, to_j);
        cost[to_i][to_j] = c + 1; // 行先のコストを更新
      }
    }
  }

  ll ans = cost[gi][gj];
  if (ans == INF)
    cout << -1 << endl;
  else
    cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

const ll INF = 1e18;
const int di[4] = {-1, 0, 1, 0};
const int dj[4] = {0, 1, 0, -1};

int main() {
  int h, w, k, si, sj, gi, gj;
  cin >> h >> w >> k >> si >> sj >> gi >> gj;
  si--;
  sj--;
  gi--;
  gj--;
  vector<string> g(h);
  rep(i, h) cin >> g[i];

  // 各マスについてスタートから到達までのコスト（かく回数）
  vector<vector<ll>> cost(h, vector<ll>(w, INF));
  cost[si][sj] = 0; // スタートマスはコスト0

  // 調べるマスを入れるキュー
  queue<P> q;

  // 調べるマスをキューに追加する関数
  auto add_g = [&](int i, int j) { q.push(make_pair(i, j)); };

  // スタート地点から調べる
  add_g(si, sj);

  while (!q.empty()) { // キューが空でない限りループ
    // キューから調べるマスの座標を取り出す
    P p = q.front();
    int cur_i = p.first;
    int cur_j = p.second;
    const ll c = cost[cur_i][cur_j];
    q.pop();

    // キューに調べるマスを追加
    rep(i, 4) rep(j, k) {
      // 行先のマスの座標
      int to_i = cur_i + di[i] * (j + 1);
      int to_j = cur_j + dj[i] * (j + 1);

      if (h <= to_i || to_i < 0 || w <= to_j || to_j < 0)
        break; // 範囲外だったら追加しない
      if (g[to_i][to_j] == '@')
        break; // ハスがあったら追加しない

      if (c + 1 <
          cost[to_i][to_j]) { // 行先のコストをもっと小さくできそうなら追加する
        add_g(to_i, to_j);
        cost[to_i][to_j] = c + 1; // 行先のコストを更新
      }
    }
  }

  ll ans = cost[gi][gj];
  if (ans == INF)
    cout << -1 << endl;
  else
    cout << ans << endl;

  return 0;
}
Problem p02644 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

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

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int h, w, k;
  cin >> h >> w >> k;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--, y1--, x2--, y2--;
  string S[h];
  for (int i = 0; i < h; i++)
    cin >> S[i];
  constexpr int inf = 1e9;
  vector<vector<int>> D(h, vector<int>(w, inf));
  D[x1][y1] = 0;
  queue<tuple<int, int>> Q;
  Q.emplace(x1, y1);
  auto in = [&](int x, int y) {
    return 0 <= x && x < h && 0 <= y && y < w && S[x][y] != '@';
  };
  while (!Q.empty()) {
    auto [x, y] = Q.front();
    Q.pop();
    int d = D[x][y] + 1;
    for (int i = 0; i < 4; i++) {
      int s = x, t = y;
      for (int j = 0; j < k; j++) {
        s += dx[i], t += dy[i];
        if (!in(s, t))
          break;
        if (D[s][t] < d)
          break;
        if (D[s][t] > d) {
          D[s][t] = d;
          Q.emplace(s, t);
        }
      }
    }
  }
  if (D[x2][y2] == inf)
    D[x2][y2] = -1;
  cout << D[x2][y2] << "\\n";
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

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

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int h, w, k;
  cin >> h >> w >> k;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--, y1--, x2--, y2--;
  string S[h];
  for (int i = 0; i < h; i++)
    cin >> S[i];
  constexpr int inf = 1e9;
  vector<vector<int>> D(h, vector<int>(w, inf));
  D[x1][y1] = 0;
  queue<tuple<int, int>> Q;
  Q.emplace(x1, y1);
  auto in = [&](int x, int y) {
    return 0 <= x && x < h && 0 <= y && y < w && S[x][y] != '@';
  };
  while (!Q.empty()) {
    auto [x, y] = Q.front();
    Q.pop();
    int d = D[x][y] + 1;
    for (int i = 0; i < 4; i++) {
      int s = x, t = y;
      for (int j = 0; j < k; j++) {
        s += dx[i], t += dy[i];
        if (!in(s, t))
          break;
        if (D[s][t] > d) {
          D[s][t] = d;
          Q.emplace(s, t);
        }
      }
    }
  }
  if (D[x2][y2] == inf)
    D[x2][y2] = -1;
  cout << D[x2][y2] << "\\n";
  return 0;
}