alexjercan/bugnet huggingface dataset:
Problem p02644 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define repo(i, n) for (int i = 1; i <= n; i++)
#define INF 1001001001
#define INFll 100100100100100
// debug用
#define PrintVec(x)                                                            \\
  for (auto elementPrintVec : x) {                                             \\
    cout << elementPrintVec << " ";                                            \\
  }                                                                            \\
  cout << endl;

using namespace std;
using ull = unsigned long long;
using ll = long long;
using P = pair<int, int>;
const int mod = 1000000007;

int up[4] = {0, -1, 0, 1};
int lr[4] = {1, 0, -1, 0};

int main() {
  int h, w, k;
  cin >> h >> w >> k;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  y1--;
  x2--;
  y2--;
  vector<string> s(h);
  rep(i, h) { cin >> s[i]; }
  queue<P> Q;
  Q.push(P(x1, y1));
  vector<vector<int>> ans(h, vector<int>(w, -1));
  ans[x1][y1] = 0;
  while (!Q.empty()) {
    P p = Q.front();
    Q.pop();
    int qx = p.first, qy = p.second;
    rep(i, 4) {
      repo(j, k) {
        int nx = qx + lr[i] * j, ny = qy + up[i] * j;
        if (nx < 0 || nx >= h || ny < 0 || ny >= w || s[nx][ny] == '@')
          break;
        if (ans[nx][ny] != -1 && ans[nx][ny] <= ans[qx][qy])
          break;
        if (ans[nx][ny] != -1)
          continue;
        ans[nx][ny] = ans[qx][qy] + 1;
        Q.push(P(nx, ny));
      }
    }
  }
  cout << ans[x2][y2] << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define repo(i, n) for (int i = 1; i <= n; i++)
#define INF 1001001001
#define INFll 100100100100100
// debug用
#define PrintVec(x)                                                            \\
  for (auto elementPrintVec : x) {                                             \\
    cout << elementPrintVec << " ";                                            \\
  }                                                                            \\
  cout << endl;

using namespace std;
using ull = unsigned long long;
using ll = long long;
using P = pair<int, int>;
const int mod = 1000000007;

int up[4] = {0, -1, 0, 1};
int lr[4] = {1, 0, -1, 0};

int main() {
  int h, w, k;
  cin >> h >> w >> k;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  y1--;
  x2--;
  y2--;
  vector<string> s(h);
  rep(i, h) { cin >> s[i]; }
  queue<P> Q;
  Q.push(P(x1, y1));
  vector<vector<int>> ans(h, vector<int>(w, -1));
  ans[x1][y1] = 0;
  while (!Q.empty()) {
    P p = Q.front();
    Q.pop();
    int qx = p.first, qy = p.second;
    rep(i, 4) {
      repo(j, k) {
        int nx = qx + lr[i] * j, ny = qy + up[i] * j;
        if (nx < 0 || nx >= h || ny < 0 || ny >= w || s[nx][ny] == '@')
          break;
        if (ans[nx][ny] != -1)
          continue;
        ans[nx][ny] = ans[qx][qy] + 1;
        Q.push(P(nx, ny));
      }
    }
  }
  cout << ans[x2][y2] << endl;
  return 0;
}
Problem p02644 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using piii = pair<ll, pii>;

ll solve() {
  ll H, W, K;
  ll x1, y1, x2, y2;
  cin >> H >> W >> K;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--, y1--, x2--, y2--;
  vector<string> C(H);
  for (int i = 0; i < H; i++) {
    cin >> C[i];
  }
  ll INF = 1LL << 60;
  vector<vector<ll>> d(H, vector<ll>(W, INF));
  pii st(x1, y1), ed(x2, y2);

  vector<int> dx = {0, 1, 0, -1}, dy = {1, 0, -1, 0};
  priority_queue<pair<ll, pii>, vector<pair<ll, pii>>, greater<>> q;
  d[st.first][st.second] = 0;
  q.push(piii(0LL, st));
  while (!q.empty()) {
    auto p = q.top();
    ll c = p.first;
    int px = p.second.first, py = p.second.second;
    q.pop();
    if (d[px][py] < c)
      continue;
    for (int ii = 0; ii < 4; ii++) {
      int x = px, y = py;
      for (int j = 0; j < K; j++) {
        x += dx[ii], y += dy[ii];
        if (x < 0 || x >= H || y < 0 || y >= W)
          break;
        if (C[x][y] == '@')
          break;
        if (d[x][y] <= d[px][py])
          break;
        if (d[x][y] > d[px][py] + 1) {
          d[x][y] = d[px][py] + 1;
          q.push(piii(d[x][y], pii(x, y)));
        }
      }
    }
  }

  ll ans = d[x2][y2];
  if (ans == INF)
    return -1;
  return ans;
}

int main() {
  auto ans = solve();
  cout << ans << "\\n";
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using piii = pair<ll, pii>;

ll solve() {
  ll H, W, K;
  ll x1, y1, x2, y2;
  cin >> H >> W >> K;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--, y1--, x2--, y2--;
  vector<string> C(H);
  for (int i = 0; i < H; i++) {
    cin >> C[i];
  }
  ll INF = 1LL << 60;
  vector<vector<ll>> d(H, vector<ll>(W, INF));
  pii st(x1, y1), ed(x2, y2);

  vector<int> dx = {0, 1, 0, -1}, dy = {1, 0, -1, 0};
  priority_queue<pair<ll, pii>, vector<pair<ll, pii>>, greater<>> q;
  d[st.first][st.second] = 0;
  q.push(piii(0LL, st));
  while (!q.empty()) {
    auto p = q.top();
    ll c = p.first;
    int px = p.second.first, py = p.second.second;
    q.pop();
    if (d[px][py] < c)
      continue;
    for (int ii = 0; ii < 4; ii++) {
      int x = px, y = py;
      for (int j = 0; j < K; j++) {
        x += dx[ii], y += dy[ii];
        if (x < 0 || x >= H || y < 0 || y >= W)
          break;
        if (C[x][y] == '@')
          break;
        if (d[x][y] <= d[px][py])
          continue;
        if (d[x][y] > d[px][py] + 1) {
          d[x][y] = d[px][py] + 1;
          q.push(piii(d[x][y], pii(x, y)));
        }
      }
    }
  }

  ll ans = d[x2][y2];
  if (ans == INF)
    return -1;
  return ans;
}

int main() {
  auto ans = solve();
  cout << ans << "\\n";
  return 0;
}
Problem p02644 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000005, INF = 0x7FFFFFFF, dx[4] = {-1, 1, 0, 0},
          dy[4] = {0, 0, -1, 1};
typedef pair<int, int> P;
priority_queue<P, vector<P>, greater<P>> Q;
vector<int> No[maxn];
vector<char> mat[maxn];
int r, c, k, n, X1, Y1, X2, Y2, u[maxn], dis[maxn];
P coo[maxn];
void bfs() {
  u[1] = No[X1][Y1];
  dis[u[1]] = 0;
  for (int head = 1, tail = 1; head <= tail; head++)
    for (int d = 0, X, Y; d < 4; d++) {
      X = coo[u[head]].first;
      Y = coo[u[head]].second;
      for (int i = 1; i <= k; i++) {
        X += dx[d];
        Y += dy[d];
        if (X < 1 || X > r || Y < 1 || Y > c || mat[X][Y] == '@')
          break;
        int v = No[X][Y];
        if (dis[u[head]] + 1 > dis[v])
          break;
        if (dis[u[head]] + 1 == dis[v])
          continue;
        dis[v] = dis[u[head]] + 1;
        u[++tail] = v;
        if (v == No[X2][Y2]) {
          printf("%d\\n", dis[v]);
          exit(0);
        }
      }
    }
}
int main() {
  scanf("%d%d%d%d%d%d%d", &r, &c, &k, &X1, &Y1, &X2, &Y2);
  for (int i = 1; i <= r; i++) {
    No[i].push_back(0);
    mat[i].push_back('.');
    for (int j = 1; j <= c; j++) {
      char x;
      scanf(" %c", &x);
      No[i].push_back(++n);
      mat[i].push_back(x);
      coo[n].first = i;
      coo[n].second = j;
    }
  }
  for (int i = 1; i <= n; i++)
    dis[i] = INF;
  bfs();
  puts("-1");
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000005, INF = 0x7FFFFFFF, dx[4] = {-1, 1, 0, 0},
          dy[4] = {0, 0, -1, 1};
typedef pair<int, int> P;
priority_queue<P, vector<P>, greater<P>> Q;
vector<int> No[maxn];
vector<char> mat[maxn];
int r, c, k, n, X1, Y1, X2, Y2, dis[maxn];
P coo[maxn];
void bfs() {
  dis[No[X1][Y1]] = 0;
  Q.push(P(0, No[X1][Y1]));
  while (!Q.empty()) {
    P node = Q.top();
    Q.pop();
    int u = node.second;
    if (node.first > dis[u])
      continue;
    for (int d = 0, X, Y; d < 4; d++) {
      X = coo[u].first;
      Y = coo[u].second;
      for (int i = 1; i <= k; i++) {
        X += dx[d];
        Y += dy[d];
        if (X < 1 || X > r || Y < 1 || Y > c || mat[X][Y] == '@')
          break;
        int v = No[X][Y];
        if (dis[u] + 1 > dis[v])
          break;
        dis[v] = dis[u] + 1;
        Q.push(P(dis[v], v));
        if (v == No[X2][Y2]) {
          printf("%d\\n", dis[v]);
          exit(0);
        }
      }
    }
  }
}
int main() {
  scanf("%d%d%d%d%d%d%d", &r, &c, &k, &X1, &Y1, &X2, &Y2);
  for (int i = 1; i <= r; i++) {
    No[i].push_back(0);
    mat[i].push_back('.');
    for (int j = 1; j <= c; j++) {
      char x;
      scanf(" %c", &x);
      No[i].push_back(++n);
      mat[i].push_back(x);
      coo[n].first = i;
      coo[n].second = j;
    }
  }
  for (int i = 1; i <= n; i++)
    dis[i] = INF;
  bfs();
  puts("-1");
  return 0;
}
Problem p02644 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using P = pair<int, int>;
int INF = 1e9;

int main() {
  int H, W, K;
  cin >> H >> W >> K;

  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;

  vector<vector<int>> dist(H + 2, vector<int>(W + 2, INF));
  vector<vector<char>> vec(H + 2, vector<char>(W + 2, '@'));
  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      cin >> vec[i][j];
    }
  }

  vector<int> dx = {0, 1, 0, -1};
  vector<int> dy = {1, 0, -1, 0};
  P start = {x1, y1};
  queue<P> Q;
  Q.push(start);
  dist[x1][y1] = 0;
  while (Q.size()) {
    P p = Q.front();
    Q.pop();
    int nx = p.first;
    int ny = p.second;
    for (int i = 0; i < 4; i++) {
      for (int j = 1; j <= K; j++) {
        if (vec[nx + dx[i] * j][ny + dy[i] * j] == '@') {
          break;
        } else if (dist[nx + dx[i] * j][ny + dy[i] * j] <= dist[nx][ny]) {
          break;
        } else if (dist[nx + dx[i] * j][ny + dy[i] * j] > dist[nx][ny] + 1) {
          dist[nx + dx[i] * j][ny + dy[i] * j] = dist[nx][ny] + 1;
          P q = {nx + dx[i] * j, ny + dy[i] * j};
          Q.push(q);
        }
      }
    }
  }
  if (dist[x2][y2] == INF) {
    dist[x2][y2] = -1;
  }
  cout << dist[x2][y2] << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using P = pair<int, int>;
int INF = 1e9;

int main() {
  int H, W, K;
  cin >> H >> W >> K;

  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;

  vector<vector<int>> dist(H + 2, vector<int>(W + 2, INF));
  vector<vector<char>> vec(H + 2, vector<char>(W + 2, '@'));
  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      cin >> vec[i][j];
    }
  }

  vector<int> dx = {0, 1, 0, -1};
  vector<int> dy = {1, 0, -1, 0};
  P start = {x1, y1};
  queue<P> Q;
  Q.push(start);
  dist[x1][y1] = 0;
  while (Q.size()) {
    P p = Q.front();
    Q.pop();
    int nx = p.first;
    int ny = p.second;
    for (int i = 0; i < 4; i++) {
      for (int j = 1; j <= K; j++) {
        if (vec[nx + dx[i] * j][ny + dy[i] * j] == '@') {
          break;
        } else if (dist[nx + dx[i] * j][ny + dy[i] * j] <= dist[nx][ny]) {
          break;
        } else {
          dist[nx + dx[i] * j][ny + dy[i] * j] = dist[nx][ny] + 1;
          P q = {nx + dx[i] * j, ny + dy[i] * j};
          Q.push(q);
        }
      }
    }
  }
  if (dist[x2][y2] == INF) {
    dist[x2][y2] = -1;
  }
  cout << dist[x2][y2] << endl;
}
Problem p02644 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"

using namespace std;

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

int main() {
  int H, W, K; // 泳げる距離
  cin >> H >> W >> K;
  int Sx, Sy, Gx, Gy;
  cin >> Sx >> Sy >> Gx >> Gy;
  --Sx;
  --Sy;
  --Gx;
  --Gy;

  vector<string> S(H);
  for (int n = 0; n < H; ++n) {
    cin >> S[n];
  }

  vector<vector<int>> D(H, vector<int>(W, 1e9));        // 回数
  vector<vector<bool>> Data(H, vector<bool>(W, false)); // 回数
  queue<pair<int, int>> q;
  q.push(make_pair(Sx, Sy));
  D[Sx][Sy] = 0;

  while (!q.empty()) {
    int x = q.front().first;
    int y = q.front().second;
    q.pop();
    if ((x == Gx) && (y == Gy)) {
      cout << D[Gx][Gy] << endl;
      return 0;
    }

    // 範囲内かつ0を4セット
    for (int l = 0; l < 4; ++l) {
      int x1 = x;
      int y1 = y;
      for (int m = 1; m <= K; ++m) {
        x1 += dx[l];
        y1 += dy[l];
        if ((0 > x1) || (H <= x1) || (0 > y1) || (W <= y1)) {
          break;
        }
        if ('@' == S[x1][y1]) {
          break;
        }
        if (Data[x1][y1] && (D[x1][y1] != (D[x][y] + 1))) {
          break;
        }
        if (!Data[x1][y1]) {
          D[x1][y1] = D[x][y] + 1;
          q.push(make_pair(x1, y1));
          Data[x1][y1] = true;
        }
      }
    }
  }

  cout << -1 << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include "bits/stdc++.h"

using namespace std;

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

int main() {
  int H, W, K; // 泳げる距離
  cin >> H >> W >> K;
  int Sx, Sy, Gx, Gy;
  cin >> Sx >> Sy >> Gx >> Gy;
  --Sx;
  --Sy;
  --Gx;
  --Gy;

  vector<string> S(H);
  for (int n = 0; n < H; ++n) {
    cin >> S[n];
  }

  vector<vector<int>> D(H, vector<int>(W, 1e9));        // 回数
  vector<vector<bool>> Data(H, vector<bool>(W, false)); // 回数
  queue<pair<int, int>> q;
  q.push(make_pair(Sx, Sy));
  D[Sx][Sy] = 0;

  while (!q.empty()) {
    int x = q.front().first;
    int y = q.front().second;
    q.pop();
    if ((x == Gx) && (y == Gy)) {
      cout << D[Gx][Gy] << endl;
      return 0;
    }

    // 範囲内かつ0を4セット
    for (int l = 0; l < 4; ++l) {
      int x1 = x;
      int y1 = y;
      for (int m = 1; m <= K; ++m) {
        x1 += dx[l];
        y1 += dy[l];
        if ((0 > x1) || (H <= x1) || (0 > y1) || (W <= y1)) {
          break;
        }
        if ('@' == S[x1][y1]) {
          break;
        }
        if (D[x1][y1] > D[x][y] + 1) {
          D[x1][y1] = D[x][y] + 1;
          if (!Data[x1][y1]) {
            q.push(make_pair(x1, y1));
            Data[x1][y1] = true;
          }
        }
      }
    }
  }

  cout << -1 << endl;
  return 0;
}
Problem p02644 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FIO                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);

const int N = 2e5 + 5, M = 1e6 + 6, OO = 0x3f3f3f3f;
int tc;
int h, w, k;
string grid[M];
int dir[] = {-1, 1, 0, 0};
int dic[] = {0, 0, -1, 1};
int dis[M], directions[M];

bool valid(int r, int c) {
  return r >= 0 && r < h && c >= 0 && c < w && grid[r][c] != '@';
}

int main() {
  int x1, x2, y1, y2;
  scanf("%d %d %d ", &h, &w, &k);
  scanf("%d %d %d %d ", &x1, &y1, &x2, &y2);

  for (int i = 0; i < h; i++) {
    string s;
    getline(cin, s);
    grid[i] = s;
  }
  x1--;
  y1--;
  x2--;
  y2--;
  memset(dis, -1, sizeof dis);
  dis[x1 * w + y1] = 0;
  directions[x1 * w + y1] = 15;
  queue<pair<int, int>> q;
  q.push({x1, y1});
  while (!q.empty()) {
    int ur = q.front().first, uc = q.front().second;
    // cout<<ur<<" "<<uc<<endl;
    q.pop();
    if (ur == x2 && uc == y2)
      break;
    for (int i = 0; i < 4; i++) {
      int steps = 1, vr, vc;
      while (steps <= k) {
        vr = ur + (dir[i] * steps);
        vc = uc + (dic[i] * steps);
        if (valid(vr, vc)) {
          if (!((directions[vr * w + vc] >> i) & 1)) {
            if (!directions[vr * w + vc]) {
              dis[vr * w + vc] = dis[ur * w + uc] + 1;
              q.push({vr, vc});
            }
            directions[vr * w + vc] |= (1 << i);
          } else if (dis[vr * w + vc] == dis[ur * w + uc] + 1) {
            steps++;
            continue;
          } else
            break;

        } else
          break;
        steps++;
      }
    }
  }
  printf("%d", dis[x2 * w + y2]);
  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
using pii = pair<int, int>;
const int INF = 1e7 + 7;
int H, W, K, st_x, st_y, end_x, end_y;

int main() {
  freopen("killer_01.txt", "r", stdin);
  freopen("killer_01_out.txt", "w", stdout);

  cin >> H >> W >> K;
  cin >> st_x >> st_y >> end_x >> end_y;
  --st_x, --st_y, --end_x, --end_y;
  vector<vector<char>> g(H, vector<char>(W));

  for (int i = 0; i < H; i++)
    for (int j = 0; j < W; j++)
      cin >> g[i][j];

  vector<vector<int>> dist(H, vector<int>(W, INF));
  dist[st_x][st_y] = 0;
  queue<pii> q;
  vector<int> dir1 = {-1, 1, 0, 0};
  vector<int> dir2 = {0, 0, 1, -1};
  q.push({st_x, st_y});

  while (!q.empty()) {
    auto f = q.front();
    q.pop();
    int x = f.F, y = f.S, d = dist[x][y];
    if (x == end_x && y == end_y) {
      cout << d << endl;
      return 0;
    }
    for (int pos = 0; pos < 4; pos++)
      for (int i = 1; i <= K; i++) {
        int xx = x + i * dir1[pos], yy = y + i * dir2[pos];
        if (xx < 0 || yy < 0 || xx >= H || yy >= W || dist[xx][yy] <= d ||
            g[xx][yy] == '@')
          break;
        if (dist[xx][yy] > d + 1) {
          dist[xx][yy] = d + 1;
          q.push({xx, yy});
        }
      }
  }

  cout << -1 << endl;
  return 0;
}
Problem p02644 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#include <fstream>
#include <iostream>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int n, m, k;
  cin >> n >> m >> k;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  y1--;
  x2--;
  y2--;
  char grid[n][m];
  int dist[n][m];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> grid[i][j];
      dist[i][j] = INT_MAX;
    }
  }

  int d[4][2] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}};
  queue<pair<int, int>> q;
  dist[x1][y1] = 0;
  q.push({x1, y1});
  while (!q.empty()) {
    int x = q.front().first;
    int y = q.front().second;
    q.pop();

    for (int i = 0; i < 4; i++) {
      int nx = x;
      int ny = y;
      int nd = dist[x][y] + 1;
      for (int j = 1; j <= k; j++) {
        nx += d[i][0];
        ny += d[i][1];

        if (nx < 0 || nx >= n || ny < 0 || ny >= m || grid[nx][ny] == '@' ||
            dist[nx][ny] < nd) {
          break;
        }
        if (dist[nx][ny] > nd) {
          dist[nx][ny] = nd;
          q.push({nx, ny});
        }
      }
    }
  }

  int ans = dist[x2][y2];
  cout << (INT_MAX == ans ? -1 : ans) << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define FIO                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);
using namespace std;
typedef long long ll;

#define row second.first
#define col second.second
#define cost first

int const N = 2e5 + 5, M = 1e3 + 5, MOD = 1e9 + 7, OO = 0x3f3f3f3f;

int n, m, k, X1, Y1, X2, Y2;
vector<vector<int>> dis;
string grid[N];

int dr[] = {0, 0, 1, -1}; // r-l-d-u
int dc[] = {1, -1, 0, 0};

bool valid(int r, int c) {
  return r >= 0 && r < n && c >= 0 && c < m && grid[r][c] == '.';
}

void dijkstra(int sr, int sc) {
  int ur, uc, vr, vc, c;
  priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>,
                 greater<pair<int, pair<int, int>>>>
      q;
  dis[sr][sc] = 0;
  q.push({0, {sr, sc}});
  while (!q.empty()) {
    ur = q.top().row;
    uc = q.top().col;
    c = q.top().cost;
    q.pop();
    if (ur == X2 - 1 && uc == Y2 - 1)
      return;
    if (dis[ur][uc] != c)
      continue;
    for (int i = 0; i < 4; i++) {
      for (int j = 1; j <= max(n, m); j++) {
        vr = ur + (j * dr[i]);
        vc = uc + (j * dc[i]);
        if (valid(vr, vc)) {
          if (dis[vr][vc] > dis[ur][uc] + ((j + k - 1) / k)) {
            dis[vr][vc] = dis[ur][uc] + ((j + k - 1) / k);
            q.push({dis[vr][vc], {vr, vc}});
          }
        } else
          break;
      }
    }
  }
}

int main() {
  FIO
          //    freopen("input.txt","rt",stdin);
          //    freopen("output.txt","wt",stdout);
          cin >>
      n >> m >> k >> X1 >> Y1 >> X2 >> Y2;
  for (int i = 0; i < n; i++)
    cin >> grid[i];
  dis = vector<vector<int>>(n + 1, vector<int>(m + 1, OO));
  dijkstra(X1 - 1, Y1 - 1);
  if (dis[X2 - 1][Y2 - 1] == OO)
    cout << "-1\\n";
  else
    cout << dis[X2 - 1][Y2 - 1] << "\\n";
}
Problem p02644 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
using pii = pair<int, int>;
const int INF = 1e7 + 7;
int H, W, K, st_x, st_y, end_x, end_y;

int main() {
  // freopen("killer_01.txt" , "r" , stdin);
  // freopen("killer_01_out.txt" , "w" , stdout);

  cin >> H >> W >> K;
  cin >> st_x >> st_y >> end_x >> end_y;
  --st_x, --st_y, --end_x, --end_y;
  vector<vector<char>> g(H, vector<char>(W));

  for (int i = 0; i < H; i++)
    for (int j = 0; j < W; j++)
      cin >> g[i][j];

  vector<vector<int>> dist(H, vector<int>(W, INF));
  dist[st_x][st_y] = 0;
  queue<pii> q;
  vector<int> dir1 = {-1, 1, 0, 0};
  vector<int> dir2 = {0, 0, 1, -1};
  q.push({st_x, st_y});

  while (!q.empty()) {
    auto f = q.front();
    q.pop();
    int x = f.F, y = f.S, d = dist[x][y];
    if (x == end_x && y == end_y) {
      cout << d << endl;
      return 0;
    }
    for (int pos = 0; pos < 4; pos++)
      for (int i = 1; i <= K; i++) {
        int xx = x + i * dir1[pos], yy = y + i * dir2[pos];
        if (xx < 0 || yy < 0 || xx >= H || yy >= W || dist[xx][yy] <= d ||
            g[xx][yy] == '@')
          break;
        if (dist[xx][yy] > d + 1) {
          dist[xx][yy] = d + 1;
          q.push({xx, yy});
        }
      }
  }

  cout << -1 << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
using pii = pair<int, int>;
const int INF = 1e7 + 7;
int H, W, K, st_x, st_y, end_x, end_y;

int main() {
  cin >> H >> W >> K;
  cin >> st_x >> st_y >> end_x >> end_y;
  --st_x, --st_y, --end_x, --end_y;
  vector<vector<char>> g(H, vector<char>(W));

  for (int i = 0; i < H; i++)
    for (int j = 0; j < W; j++)
      cin >> g[i][j];

  vector<vector<int>> dist(H, vector<int>(W, INF));
  dist[st_x][st_y] = 0;
  queue<pii> q;
  vector<int> dir1 = {-1, 1, 0, 0};
  vector<int> dir2 = {0, 0, 1, -1};
  q.push({st_x, st_y});

  while (!q.empty()) {
    auto f = q.front();
    q.pop();
    int x = f.F, y = f.S;
    if (x == end_x && y == end_y) {
      cout << dist[x][y] << endl;
      return 0;
    }
    for (int pos = 0; pos < 4; pos++)
      for (int i = 1; i <= K; i++) {
        int xx = x + i * dir1[pos], yy = y + i * dir2[pos];
        if (xx < 0 || yy < 0 || xx >= H || yy >= W || g[xx][yy] == '@')
          break;
        if (dist[xx][yy] > dist[x][y] + 1) {
          dist[xx][yy] = dist[x][y] + 1;
          q.push({xx, yy});
        }
      }
  }

  cout << -1 << endl;
  return 0;
}