alexjercan/bugnet huggingface dataset:
Problem p02641 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  // your code goes here
  int x, n, a;
  if (n == 0) {
    cout << x << endl;
    return 0;
  }
  cin >> x >> n;
  set<int> s;
  for (int i = 0; i < n; ++i) {
    cin >> a;
    s.insert(a);
  }
  int ans1 = x, ans2 = x;
  while (true) {
    if (!s.count(ans1)) {
      cout << ans1 << endl;
      return 0;
    }
    if (!s.count(ans2)) {
      cout << ans2 << endl;
      return 0;
    }
    --ans1;
    ++ans2;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  // catfight
  int N, P;
  int s, start, max, c = 0;
  int last = 2;
  cin >> N >> P;
  int cats[N];
  for (int i = 0; i < N; i++) {
    cin >> cats[i];
    if (cats[i] < P && start == 0) {
      s = cats[i];
      start = i;
    }
  }
  for (int i = start; i < N; i++) {
    if (s + cats[i + 1] < P) {
      s += cats[i + 1];
      s -= cats[i];
      while (s + cats[i + c + last] < P) {
        s += cats[i + c + last];
        c++;
      }
      last += c - 1;
      if (c > 1)
        max = last;
      c = 0;
    }
  }
  cout << max << endl;
  return 0;
}
Problem p02641 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int x, n, input;
  cin >> x;
  cin >> n;
  int l = x;
  int u = x;
  vector<int> v;
  for (int i = 0; i < n; i++) {
    cin >> input;
    v.push_back(input);
  }
  sort(v.begin(), v.end());
  bool present = true;
  while (present == true) {
    if (!binary_search(v.begin(), v.end(), l)) {
      cout << l << endl;
      present = false;
    } else if (!binary_search(v.begin(), v.end(), --l)) {
      cout << l << endl;
      present = false;
    } else if (!binary_search(v.begin(), v.end(), ++u)) {
      cout << u << endl;
      present = false;
    } else if (l == 1) {
      cout << "0" << endl;
      present = false;
    }
  }
}

Error: WA

Fixed submission:

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

int findSmallest(int a[], int n) {
  int temp = a[0];
  for (int i = 1; i < n; i++) {
    if (temp > a[i])
      temp = a[i];
  }
  return temp;
}

int findLargest(int a[], int n) {
  int temp = a[0];
  for (int i = 1; i < n; i++) {
    if (temp < a[i])
      temp = a[i];
  }
  return temp;
}

void formSecondary(int a[], int n, int first) {
  for (int i = 0; i < n; i++) {
    a[i] = (first)++;
  }
}

void print(int a[], int n) {
  for (int i = 0; i < n; i++) {
    cout << a[i] << " ";
  }
}

int findDiff(int a[], int n, int x) {
  int diff = 101;
  int temp, loc;
  for (int i = 0; i < n; i++) {
    if (diff > abs(x - a[i])) {
      diff = abs(x - a[i]);
      loc = i;
    }
  }
  temp = a[loc];
  a[loc] = 100;
  return temp;
}

int searchInArray(int a[], int n, int I) {
  for (int i = 0; i < n; i++) {
    if (I == a[i])
      return 1;
  }
  return -1;
}

int main() {
  int x, n;
  cin >> x >> n;
  int a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int first, last;
  first = findSmallest(a, n);
  last = findLargest(a, n);
  int s = last - first + 3;
  int secondaryArray[s];
  formSecondary(secondaryArray, s, first - 1);

  int I, result = -1;
  I = findDiff(secondaryArray, s, x);
  // cout<<I<<" this is I\\n";
  while (1) {
    I = findDiff(secondaryArray, s, x);
    result = searchInArray(a, n, I);
    if (result == -1)
      break;
  }
  cout << I;

  return 0;
}
Problem p02641 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int X, N;
  cin >> X >> N;

  int p[N];
  for (int i = 0; i < N; i++)
    cin >> p[i];

  int Min = 1000;
  int ans = 1000;
  sort(p, p + N);
  for (int i = 0; i <= 101; i++) {
    bool flag = true;
    for (int j = 0; j < N; j++) {
      if (p[j] == i)
        flag = false;
    }
    if (!flag)
      continue;

    int num = abs(X - i);
    if (Min > num) {
      ans = i;
      Min = num;
    }
  }

  cout << ans << endl;

  return 0;
}

Error: WA

Fixed submission:

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

int main() {
  int X, N;
  cin >> X >> N;

  int p[N];
  for (int i = 0; i < N; i++)
    cin >> p[i];

  int Min = 1000;
  int ans = 1000;
  sort(p, p + N);
  for (int i = -1000; i <= 1000; i++) {
    if (p[i] == i)
      continue;

    int num = abs(X - i);
    if (Min > num) {
      ans = i;
      Min = num;
    }
  }

  cout << ans << endl;

  return 0;
}
Problem p02641 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int x, n, p, c[105];
vector<int> v;
int left(int t) {
  int l = 0, r = v.size();
  while (l < r) {
    int m = (l + r) / 2;
    if (v[m] < t)
      l = m + 1;
    else
      r = m;
  }
  return v[l - 1];
}
int right(int t) {
  int l = 0, r = v.size();
  while (l < r) {
    int m = (l + r) / 2;
    if (v[m] > t)
      r = m;
    else
      l = m + 1;
  }
  return v[r];
}
int main() {
  cin >> x >> n;
  for (int i = 1; i <= n; i++) {
    cin >> p;
    c[p] = 1;
  }
  if (n == 0 || !c[x]) {
    cout << x;
    return 0;
  }
  for (int i = 0; i <= 101; i++) {
    if (!c[i])
      v.push_back(i);
  }
  int a = left(x), b = right(x);
  if (x - a <= b - x)
    cout << a;
  else
    cout << b;
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int x, n, p, c[105];
vector<int> v;
int left(int t) {
  int l = 0, r = v.size();
  while (l < r) {
    int m = (l + r) / 2;
    if (v[m] < t)
      l = m + 1;
    else
      r = m;
  }
  return v[l - 1];
}
int right(int t) {
  int l = 0, r = v.size();
  while (l < r) {
    int m = (l + r) / 2;
    if (v[m] > t)
      r = m;
    else
      l = m + 1;
  }
  return v[r];
}
int main() {
  cin >> x >> n;
  for (int i = 1; i <= n; i++) {
    cin >> p;
    c[p] = 1;
  }
  if (n == 0 || !c[x]) {
    cout << x;
    return 0;
  }
  for (int i = 1; i <= 100; i++) {
    if (!c[i])
      v.push_back(i);
  }
  if (x == 1) {
    cout << right(x);
    return 0;
  }
  int a = left(x), b = right(x);
  if (abs(x - a) <= abs(b - x))
    cout << a;
  else
    cout << b;
  return 0;
}
Problem p02641 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rep1(i, n) for (int i = 1; i <= n; ++i)
#define co(x) cout << (x) << "\\n"
#define cosp(x) cout << (x) << " "

int main() {
  bool f[101];
  for (int i = 0; i < 101; i++) {
    f[i] = true;
  }
  int x, n;
  cin >> x >> n;
  for (int i = 0; i < n; i++) {
    int tmp;
    cin >> tmp;
    f[tmp] = false;
  }
  int lx = x;
  int rx = x;
  while (1) {
    if (lx < 0 || (f[lx])) {
      co(lx);
      return 0;
    }
    if (f[rx]) {
      co(rx);
      return 0;
    }
    lx--;
    rx++;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rep1(i, n) for (int i = 1; i <= n; ++i)
#define co(x) cout << (x) << "\\n"
#define cosp(x) cout << (x) << " "

int main() {
  bool f[101];
  for (int i = 0; i < 101; i++) {
    f[i] = true;
  }
  int x, n;
  cin >> x >> n;
  for (int i = 0; i < n; i++) {
    int tmp;
    cin >> tmp;
    f[tmp] = false;
  }
  int lx = x;
  int rx = x;
  while (1) {
    if ((lx >= 1) && f[lx]) {
      co(lx);
      return 0;
    }
    if ((rx <= 100) && f[rx]) {
      co(rx);
      return 0;
    }
    lx--;
    rx++;
  }
}
Problem p02644 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

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

  vector<vector<char>> c(H, vector<char>(W));
  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j)
      cin >> c[i][j];
  }

  vector<vector<int>> dist(H, vector<int>(W, INF));
  dist[x1][y1] = 0;
  queue<pair<int, int>> Q;
  Q.emplace(x1, y1);

  while (!Q.empty()) {
    auto [x, y] = Q.front();
    Q.pop();
    for (int i = 0; i < 4; ++i) {
      for (int j = 1; j <= K; ++j) {
        int nx = x + dx[i] * j, ny = y + dy[i] * j;
        if (nx < 0 || nx >= H || ny < 0 || ny >= W || c[nx][ny] == '@' ||
            dist[nx][ny] <= dist[x][y])
          break;
        if (dist[nx][ny] == INF) {
          dist[nx][ny] = dist[x][y] + 1;
          Q.emplace(nx, ny);
        }
      }
    }
  }
  cout << (dist[x2][y2] == INF ? -1 : dist[x2][y2]) << '\\n';
}


Error: TLE

Fixed submission:

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

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

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

  vector<vector<char>> c(H, vector<char>(W));
  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j)
      cin >> c[i][j];
  }

  vector<vector<int>> dist(H, vector<int>(W, INF));
  dist[x1][y1] = 0;
  queue<pair<int, int>> Q;
  Q.emplace(x1, y1);

  while (!Q.empty()) {
    auto [x, y] = Q.front();
    Q.pop();
    for (int i = 0; i < 4; ++i) {
      for (int j = 1; j <= K; ++j) {
        int nx = x + dx[i] * j, ny = y + dy[i] * j;
        if (nx < 0 || nx >= H || ny < 0 || ny >= W || c[nx][ny] == '@' ||
            dist[nx][ny] <= dist[x][y])
          break;
        dist[nx][ny] = dist[x][y] + 1;
        Q.emplace(nx, ny);
      }
    }
  }
  cout << (dist[x2][y2] == INF ? -1 : dist[x2][y2]) << '\\n';
}
Problem p02644 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

const int INF = 1 << 30;
const std::vector<int> dx = {1, -1, 0, 0}, dy = {0, 0, 1, -1};

template <typename T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int H, W, K;
  cin >> H >> W >> K;
  int sx, sy, gx, gy;
  cin >> sy >> sx >> gy >> gx;
  sx--, sy--, gx--, gy--;
  vector<string> S(H);
  for (int i = 0; i < H; i++) {
    cin >> S[i];
  }
  priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>,
                 greater<pair<int, pair<int, int>>>>
      que;
  que.push(make_pair(0, make_pair(sy, sx)));
  vector<vector<int>> G(H, vector<int>(W, INF));
  G[sy][sx] = 0;
  while (!que.empty()) {
    auto q = que.top();
    que.pop();
    int c = q.first, y = q.second.first, x = q.second.second;
    for (int i = 0; i < 4; i++) {
      int ny = y, nx = x;
      for (int k = 0; k < K; k++) {
        ny += dy[i], nx += dx[i];
        if (ny < 0 || ny >= H || nx < 0 || nx >= W)
          break;
        if (S[ny][nx] == '@')
          break;
        if (chmin(G[ny][nx], c + 1)) {
          que.push(make_pair(G[ny][nx], make_pair(ny, nx)));
        } else {
          if (G[ny][nx] != G[y][x] + 1)
            break;
        }
      }
    }
  }
  if (G[gy][gx] == INF) {
    cout << -1 << '\\n';
  } else {
    cout << G[gy][gx] << '\\n';
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

const int INF = 1 << 30;
const std::vector<int> dx = {1, -1, 0, 0}, dy = {0, 0, 1, -1};

template <typename T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int H, W, K;
  cin >> H >> W >> K;
  int sx, sy, gx, gy;
  cin >> sy >> sx >> gy >> gx;
  sx--, sy--, gx--, gy--;
  vector<string> S(H);
  for (int i = 0; i < H; i++) {
    cin >> S[i];
  }
  priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>,
                 greater<pair<int, pair<int, int>>>>
      que;
  que.push(make_pair(0, make_pair(sy, sx)));
  vector<vector<int>> G(H, vector<int>(W, INF));
  G[sy][sx] = 0;
  while (!que.empty()) {
    auto q = que.top();
    que.pop();
    int c = q.first, y = q.second.first, x = q.second.second;
    for (int i = 0; i < 4; i++) {
      int ny = y, nx = x;
      for (int k = 0; k < K; k++) {
        ny += dy[i], nx += dx[i];
        if (ny < 0 || ny >= H || nx < 0 || nx >= W)
          break;
        if (S[ny][nx] == '@')
          break;
        if (G[ny][nx] == G[y][x] + 1 || chmin(G[ny][nx], c + 1)) {
          chmin(G[ny][nx], c + 1);
          que.push(make_pair(G[ny][nx], make_pair(ny, nx)));
        } else {
          continue;
        }
      }
    }
  }
  if (G[gy][gx] == INF) {
    cout << -1 << '\\n';
  } else {
    cout << G[gy][gx] << '\\n';
  }
  return 0;
}
Problem p02644 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

const int inf = 1e9;
int h, w, k, x1, Y1, x2, y2, mx[] = {0, 1, 0, -1}, my[] = {1, 0, -1, 0};
string mp[100005];

struct node {
  int x, y, v;
} q[5000005];

int main() {
  cin >> h >> w >> k >> x1 >> Y1 >> x2 >> y2;
  for (int i = 1; i <= h; i++)
    cin >> mp[i];
  int d[h + 10][w + 10];
  for (int i = 1; i <= h; i++)
    for (int j = 1; j <= w; j++)
      d[i][j] = inf;
  d[x1][Y1] = 0;
  int l = 1, r = 1;
  node x;
  x.x = x1;
  x.y = Y1;
  x.v = 0;
  q[1] = x;
  while (l <= r) {
    node x = q[l++];
    if (x.v != d[x.x][x.y])
      continue;
    if (x.x == x2 && x.y == y2) {
      cout << d[x2][y2] << endl;
      return 0;
    }
    for (int i = 0; i < 4; i++) {
      for (int j = 1; j <= k; j++) {
        int nx = x.x + j * mx[i], ny = x.y + j * my[i];
        if (nx >= 1 && nx <= h && ny >= 1 && ny <= w && mp[nx][ny - 1] == '.') {
          if (d[nx][ny] > d[x.x][x.y] + 1) {
            d[nx][ny] = d[x.x][x.y] + 1;
            node xx;
            xx.x = nx;
            xx.y = ny;
            xx.v = d[nx][ny];
            q[++r] = xx;
          }
          if (d[nx][ny] <= d[x.x][x.y])
            break;
        } else
          break;
      }
    }
  }
  cout << -1 << '\\n';
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

const int inf = 1e9;
int h, w, k, x1, Y1, x2, y2, mx[] = {0, 1, 0, -1}, my[] = {1, 0, -1, 0};
string mp[100005];

struct node {
  int x, y, v;
} q[5000005];

int main() {
  cin >> h >> w >> k >> x1 >> Y1 >> x2 >> y2;
  for (int i = 1; i <= h; i++)
    cin >> mp[i];
  int d[h + 10][w + 10];
  for (int i = 1; i <= h; i++)
    for (int j = 1; j <= w; j++)
      d[i][j] = inf;
  d[x1][Y1] = 0;
  int l = 1, r = 1;
  node x;
  x.x = x1;
  x.y = Y1;
  x.v = 0;
  q[1] = x;
  while (l <= r) {
    node x = q[l++];
    if (x.v != d[x.x][x.y])
      continue;
    if (x.x == x2 && x.y == y2) {
      cout << d[x2][y2] << endl;
      return 0;
    }
    for (int i = 0; i < 4; i++) {
      for (int j = 1; j <= k; j++) {
        int nx = x.x + j * mx[i], ny = x.y + j * my[i];
        if (nx >= 1 && nx <= h && ny >= 1 && ny <= w && mp[nx][ny - 1] == '.') {
          if (d[nx][ny] > d[x.x][x.y] + 1) {
            d[nx][ny] = d[x.x][x.y] + 1;
            node xx;
            xx.x = nx;
            xx.y = ny;
            xx.v = d[nx][ny];
            q[++r] = xx;
          }
          if (d[nx][ny] < d[x.x][x.y])
            break;
        } else
          break;
      }
    }
  }
  cout << -1 << '\\n';
  return 0;
}
Problem p02644 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int h, w, k;
  cin >> h >> w >> k;
  ++k;
  int xb, yb, xe, ye;
  cin >> xb >> yb >> xe >> ye;
  --xb;
  --yb;
  --xe;
  --ye;
  vector<vector<char>> rec(h, vector<char>(w));
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      cin >> rec[i][j];
    }
  }
  vector<vector<int>> dis(h, vector<int>(w, 1 << 30));
  int dx[4] = {0, 0, -1, 1};
  int dy[4] = {1, -1, 0, 0};
  queue<vector<int>> que;
  que.push({xb, yb});
  dis[xb][yb] = 0;
  while (!que.empty()) {
    int x = que.front()[0], y = que.front()[1];
    que.pop();
    for (int r = 0; r < 4; ++r) {
      int u = x, v = y, s = dis[x][y] + 1;
      for (int d = 1; d < k; ++d) {
        u += dx[r];
        v += dy[r];
        if (0 <= u && u < h && 0 <= v && v < w && rec[u][v] == '.') {
          if (dis[u][v] > s) {
            dis[u][v] = s;
            que.push({u, v});
          } else if (dis[u][v] < s) {
            break;
          }
        } else {
          break;
        }
      }
    }
  }
  if (dis[xe][ye] == 1 << 30) {
    cout << -1 << endl;
  } else {
    cout << dis[xe][ye] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int h, w, k;
  cin >> h >> w >> k;
  ++k;
  int xb, yb, xe, ye;
  cin >> xb >> yb >> xe >> ye;
  --xb;
  --yb;
  --xe;
  --ye;
  vector<vector<char>> rec(h, vector<char>(w));
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      cin >> rec[i][j];
    }
  }
  vector<vector<int>> dis(h, vector<int>(w, 1 << 30));
  int dx[4] = {0, 0, -1, 1};
  int dy[4] = {1, -1, 0, 0};
  queue<vector<int>> que;
  que.push({xb, yb});
  dis[xb][yb] = 0;
  while (!que.empty()) {
    int x = que.front()[0], y = que.front()[1];
    que.pop();
    for (int r = 0; r < 4; ++r) {
      int u = x, v = y, s = dis[x][y] + 1;
      for (int d = 1; d < k; ++d) {
        u += dx[r];
        v += dy[r];
        if (0 <= u && u < h && 0 <= v && v < w && rec[u][v] == '.') {
          if (dis[u][v] > s) {
            dis[u][v] = s;
            que.push({u, v});
          }
        } else {
          break;
        }
      }
    }
  }
  if (dis[xe][ye] == 1 << 30) {
    cout << -1 << endl;
  } else {
    cout << dis[xe][ye] << endl;
  }
  return 0;
}
Problem p02644 (C++) — buggy submission, verdict: Runtime Error

#pragma GCC optimize(3)
#include <bits/stdc++.h>
using namespace std;
const int N = 8000005, dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1};
int qx[N], qy[N], qd[N], b, e;
int main() {
  ios::sync_with_stdio(false);
  int n, m, k, bx, by, ex, ey;
  cin >> n >> m >> k >> bx >> by >> ex >> ey;
  vector<vector<char>> s(n, vector<char>(m, '.'));
  vector<vector<int>> d[4];
  vector<int> zh(m, 1e9);
  for (int i = 0; i < 4; i++)
    for (int j = 0; j < n; j++)
      d[i].push_back(zh);
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      cin >> s[i][j];
  bx--, by--, ex--, ey--;
  for (int i = 0; i < 4; i++) {
    d[i][bx][by] = 0;
    qx[e] = bx, qy[e] = by, qd[e] = i, e++;
  }
  while (b < e) {
    int x = qx[b], y = qy[b], dd = qd[b];
    b++;
    for (int i = 0; i < 4; i++)
      d[i][x][y] = d[dd][x][y];
    if (x == ex && y == ey)
      break;
    for (int i = 0; i < 4; i++) {
      int zh = 0, kx = x, ky = y;
      while (1) {
        kx += dx[i], ky += dy[i], zh++;
        if (kx < 0 || kx >= n || ky < 0 || ky >= m || s[kx][ky] == '@')
          break;
        if (d[i][kx][ky] > d[0][x][y] + 1) {
          d[i][kx][ky] = d[0][x][y] + 1;
          qx[e] = kx, qy[e] = ky, qd[e] = i, e++;
        } else if (d[i][kx][ky] <= d[0][x][y])
          break;
        if (zh == k)
          break;
      }
    }
  }
  printf("%d\\n", d[0][ex][ey] == 1e9 ? -1 : d[0][ex][ey]);
  return 0;
}


Error: -11

Fixed submission:

#pragma GCC optimize(3)
#include <bits/stdc++.h>
using namespace std;
const int N = 80000005, dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1};
int qx[N], qy[N], b, e;
int main() {
  ios::sync_with_stdio(false);
  int n, m, k, bx, by, ex, ey;
  cin >> n >> m >> k >> bx >> by >> ex >> ey;
  vector<vector<char>> s(n, vector<char>(m, '.'));
  vector<vector<int>> d(n, vector<int>(m, 1e9));
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      cin >> s[i][j];
  bx--, by--, ex--, ey--;
  d[bx][by] = 0;
  qx[e] = bx, qy[e] = by, e++;
  while (b < e) {
    int x = qx[b], y = qy[b];
    b++;
    if (x == ex && y == ey)
      break;
    for (int i = 0; i < 4; i++) {
      int zh = 0, kx = x, ky = y;
      while (1) {
        kx += dx[i], ky += dy[i], zh++;
        if (kx < 0 || kx >= n || ky < 0 || ky >= m || s[kx][ky] == '@')
          break;
        if (d[kx][ky] >= d[x][y] + 1) {
          d[kx][ky] = d[x][y] + 1;
          qx[e] = kx, qy[e] = ky, e++;
        } else
          break;
        if (zh == k)
          break;
      }
    }
  }
  printf("%d\\n", d[ex][ey] == 1e9 ? -1 : d[ex][ey]);
  return 0;
}
Problem p02644 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#include <random>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;

int H, W, K;
int xx1, yy1, xx2, yy2;
string arr[1100000];

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

int bfs() {
  vector<vector<int>> dist(H, vector<int>(W, 1e9));
  queue<pll> q;
  dist[xx1][yy1] = 0;
  q.push({xx1, yy1});

  int cnt = 0;
  while (q.size()) {
    int qsz = q.size();
    while (qsz--) {
      int y, x;
      tie(x, y) = q.front();
      q.pop();

      if (y == yy2 && x == xx2)
        return dist[x][y];

      for (int i = 0; i < 4; i++) {
        for (int j = 1; j <= K; j++) {
          int ny = y + dy[i] * j;
          int nx = x + dx[i] * j;
          if (ny < 0 || nx < 0 || ny >= W || nx >= H)
            break;
          if (arr[nx][ny] == '@')
            break;
          if (dist[nx][ny] <= dist[x][y])
            break;
          if (dist[nx][ny] != 1e9)
            continue;
          q.push({nx, ny});
          dist[nx][ny] = cnt + 1;
        }
      }
    }
    cnt++;
  }
  return -1;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> H >> W >> K;
  cin >> xx1 >> yy1 >> xx2 >> yy2;
  xx1--, yy1--, xx2--, yy2--;
  for (int i = 0; i < H; i++)
    cin >> arr[i];

  cout << bfs() << '\\n';
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <random>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;

int H, W, K;
int xx1, yy1, xx2, yy2;
string arr[1100000];

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

int bfs() {
  vector<vector<int>> dist(H, vector<int>(W, 1e9));
  queue<pll> q;
  dist[xx1][yy1] = 0;
  q.push({xx1, yy1});

  int cnt = 0;
  while (q.size()) {
    int qsz = q.size();
    while (qsz--) {
      int y, x;
      tie(x, y) = q.front();
      q.pop();

      if (y == yy2 && x == xx2)
        return cnt;

      for (int i = 0; i < 4; i++) {
        for (int j = 1; j <= K; j++) {
          int ny = y + dy[i] * j;
          int nx = x + dx[i] * j;
          if (ny < 0 || nx < 0 || ny >= W || nx >= H)
            break;
          if (arr[nx][ny] == '@' || dist[nx][ny] < cnt + 1)
            break;
          q.push({nx, ny});
          dist[nx][ny] = cnt + 1;
          if (ny == yy2 && nx == xx2)
            return cnt + 1;
        }
      }
    }
    cnt++;
  }
  return -1;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> H >> W >> K;
  cin >> xx1 >> yy1 >> xx2 >> yy2;
  xx1--, yy1--, xx2--, yy2--;
  for (int i = 0; i < H; i++)
    cin >> arr[i];

  cout << bfs() << '\\n';
  return 0;
}