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

#include <bits/stdc++.h>

using namespace std;

int main() {
  int h, w, k;
  cin >> h >> w >> k;
  int y1, x1, y2, x2;
  cin >> y1 >> x1 >> y2 >> x2;
  y1--;
  x1--;
  y2--;
  x2--;

  vector<string> C(h);
  for (auto &c : C)
    cin >> c;

  queue<array<int, 3>> Q;
  Q.push({y1, x1, 0});
  const constexpr int INF = 1e9;
  vector<vector<int>> D(h, vector<int>(w, INF));

  vector<int> dy = {0, 1, 0, -1}, dx = {1, 0, -1, 0};
  D[y1][x1] = 0;
  vector<vector<vector<int>>> DD(4,
                                 vector<vector<int>>(h, vector<int>(w, INF)));

  while (!Q.empty()) {
    auto [y, x, d] = Q.front();
    Q.pop();
    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 or ny >= h or nx < 0 or nx >= w)
          break;
        if (DD[i][ny][nx] != INF)
          break;
        if (D[ny][nx] <= d)
          break;
        if (C[ny][nx] == '@')
          break;
        Q.push({ny, nx, d + 1});
        D[ny][nx] = d + 1;
        DD[i][ny][nx] = d + 1;
      }
    }
  }

  if (D[y2][x2] == INF) {
    puts("-1");
    return 0;
  }

  cout << D[y2][x2] << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main() {
  int h, w, k;
  cin >> h >> w >> k;
  int y1, x1, y2, x2;
  cin >> y1 >> x1 >> y2 >> x2;
  y1--;
  x1--;
  y2--;
  x2--;

  vector<string> C(h);
  for (auto &c : C)
    cin >> c;

  queue<array<int, 3>> Q;
  Q.push({y1, x1, 0});
  const constexpr int INF = 1e9;
  vector<vector<int>> D(h, vector<int>(w, INF));

  vector<int> dy = {0, 0, 1, -1}, dx = {1, -1, 0, 0};
  D[y1][x1] = 0;

  while (!Q.empty()) {
    auto [y, x, d] = Q.front();
    Q.pop();
    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 or ny >= h or nx < 0 or nx >= w)
          break;
        if (D[ny][nx] <= d)
          break;
        if (C[ny][nx] == '@')
          break;
        Q.push({ny, nx, d + 1});
        D[ny][nx] = d + 1;
      }
    }
  }

  if (D[y2][x2] == INF) {
    puts("-1");
    return 0;
  }

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

#include <cstdio>
#include <map>
#include <queue>
#include <set>
#include <vector>

#define INF (1 << 28)

using namespace std;

int H, W, K;
int x1, y1, x2, y2;
vector<vector<char>> c;
typedef pair<int, int> P;

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

void solve() {
  queue<P> q;
  q.push(P(x1, y1));
  int d[H][W];
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      d[i][j] = INF;
    }
  }
  d[x1][y1] = 0;

  while (!q.empty()) {
    P cc = q.front();
    q.pop();
    int cx = cc.first, cy = cc.second;
    // printf("c = (%d, %d)\\n", cx, cy);
    if (cx == x2 && cy == y2) {
      break;
    }

    for (int i = 0; i < 4; i++) {
      for (int j = 1; j <= K; j++) {
        int nx = cx + dx[i] * j, ny = cy + dy[i] * j;
        // printf("n = (%d, %d) = %c\\n", nx, ny, (0 <= nx && nx < H && 0 <= ny
        // && ny < W ? c[nx][ny] : '-'));
        if (0 <= nx && nx < H && 0 <= ny && ny < W && c[nx][ny] == '.' &&
            d[nx][ny] > d[cx][cy]) {
          if (d[nx][ny] > d[cx][cy] + 1) {
            d[nx][ny] = d[cx][cy] + 1;
            q.push(P(nx, ny));
          }
        } else {
          break;
        }
      }
    }
  }

  printf("%d\\n", d[x2][y2] == INF ? -1 : d[x2][y2]);
}

int main() {
  scanf("%d %d %d", &H, &W, &K);
  scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
  x1--;
  y1--;
  x2--;
  y2--;

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

  solve();
}


Error: TLE

Fixed submission:

#include <cstdio>
#include <map>
#include <queue>
#include <set>
#include <vector>

#define INF (1 << 28)

using namespace std;

int H, W, K;
int x1, y1, x2, y2;
vector<vector<char>> c;
typedef pair<int, int> P;

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

void solve() {
  queue<P> q;
  q.push(P(x1, y1));
  int d[H][W];
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      d[i][j] = INF;
    }
  }
  d[x1][y1] = 0;

  while (!q.empty()) {
    P cc = q.front();
    q.pop();
    int cx = cc.first, cy = cc.second;
    // printf("c = (%d, %d)\\n", cx, cy);
    if (cx == x2 && cy == y2) {
      break;
    }

    for (int i = 0; i < 4; i++) {
      for (int j = 1; j <= K; j++) {
        int nx = cx + dx[i] * j, ny = cy + dy[i] * j;
        // printf("n = (%d, %d) = %c\\n", nx, ny, (0 <= nx && nx < H && 0 <= ny
        // && ny < W ? c[nx][ny] : '-'));
        if (0 <= nx && nx < H && 0 <= ny && ny < W && c[nx][ny] == '.' &&
            d[nx][ny] > d[cx][cy]) {
          d[nx][ny] = d[cx][cy] + 1;
          q.push(P(nx, ny));
        } else {
          break;
        }
      }
    }
  }

  printf("%d\\n", d[x2][y2] == INF ? -1 : d[x2][y2]);
}

int main() {
  scanf("%d %d %d", &H, &W, &K);
  scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
  x1--;
  y1--;
  x2--;
  y2--;

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

  solve();
}
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 <= k; j++) {
        vr = ur + (j * dr[i]);
        vc = uc + (j * dc[i]);
        if (valid(vr, vc)) {
          if (dis[vr][vc] > dis[ur][uc] + 1 &&
              dis[ur][uc] + 1 < dis[X2 - 1][Y2 - 1]) {
            dis[vr][vc] = dis[ur][uc] + 1;
            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;
const int dir[4][2] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}};
const int INF = 1e9;

int h, w, k;
int sx, sy, gx, gy;
vector<vector<char>> MP;
vector<vector<int>> dis;

bool outside(int x, int y) { return x < 0 or x >= h or y < 0 or y >= w; }

bool bad(int x, int y, int nd) { return MP[x][y] == '@' or dis[x][y] < nd; }

void bfs() {
  queue<pair<int, int>> que;
  que.push({sx, sy});
  dis[sx][sy] = 0;
  while (!que.empty()) {
    int x = que.front().first;
    int y = que.front().second;
    que.pop();
    for (int i = 0; i < 4; i++) {
      int nx = x, ny = y;
      int nd = dis[x][y] + 1;
      for (int j = 1; j <= k; j++) {
        nx += dir[i][0];
        ny += dir[i][1];
        if (outside(nx, ny) or bad(nx, ny, nd))
          break;
        if (dis[nx][ny] > nd) {
          que.push({nx, ny});
          dis[nx][ny] = nd;
        }
      }
    }
  }
}

int main() {
  cin >> h >> w >> k >> sx >> sy >> gx >> gy;
  --sx, --sy, --gx, --gy;
  MP.resize(h, vector<char>(w));
  dis.resize(h, vector<int>(w));
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      cin >> MP[i][j], dis[i][j] = INF;
  bfs();
  cout << (dis[gx][gy] == INF ? -1 : dis[gx][gy]);
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int dir[4][2] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}};
const int INF = 1e9;

int h, w, k;
int sx, sy, gx, gy;
vector<vector<char>> MP;
vector<vector<int>> dis;

bool outside(int x, int y) { return x < 0 or x >= h or y < 0 or y >= w; }

bool bad(int x, int y, int nd) { return MP[x][y] == '@' or dis[x][y] < nd; }

void bfs() {
  queue<pair<int, int>> que;
  que.push({sx, sy});
  dis[sx][sy] = 0;
  while (!que.empty()) {
    int x = que.front().first;
    int y = que.front().second;
    que.pop();
    for (int i = 0; i < 4; i++) {
      int nx = x, ny = y;
      int nd = dis[x][y] + 1;
      for (int j = 1; j <= k; j++) {
        nx += dir[i][0];
        ny += dir[i][1];
        if (outside(nx, ny) or bad(nx, ny, nd))
          break;
        que.push({nx, ny});
        dis[nx][ny] = nd;
      }
    }
  }
}

int main() {
  cin >> h >> w >> k >> sx >> sy >> gx >> gy;
  --sx, --sy, --gx, --gy;
  MP.resize(h, vector<char>(w));
  dis.resize(h, vector<int>(w));
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      cin >> MP[i][j], dis[i][j] = INF;
  bfs();
  cout << (dis[gx][gy] == INF ? -1 : dis[gx][gy]);
}
Problem p02644 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using Graph_c = vector<vector<ll>>;
struct Z {
  ll y;
  ll x;
  ll depth;
};

ll H, W, K, y, x, depth, oo = 1e12, xs, ys, xg, yg;
vector<ll> y_vec = {1, 0, -1, 0};
vector<ll> x_vec = {0, 1, 0, -1};
Graph_c A, B;
queue<Z> q;

ll bfs() {
  ll ans = oo;
  while (!q.empty()) {
    Z now = q.front();
    q.pop();
    y = now.y, x = now.x, depth = now.depth;
    if (y == yg && x == xg) {
      ans = min(ans, depth);
      return ans;
    }
    for (ll i = 0; i < 4; i++) {
      for (ll k = 1; k <= K; k++) {
        Z next = {y + y_vec[i] * k, x + x_vec[i] * k, depth + 1};
        // cout<<"ny:"<<next.y<<" nx:"<<next.x<<endl;
        if (next.y <= 0 || next.y > H || next.x <= 0 || next.x > W ||
            A[next.y][next.x] == -1)
          break;
        if (A[next.y][next.x] < next.depth)
          break;
        if (A[next.y][next.x] == next.depth)
          continue;
        A[next.y][next.x] = next.depth;
        q.push(next);
        // cout<<" d:"<<next.depth<<endl;
      }
    }
  }
  return oo;
}

int main() {
  cin >> H >> W >> K;
  cin >> ys >> xs >> yg >> xg;
  A.assign(H + 2, vector<ll>(W + 2, -1));
  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      char a;
      cin >> a;
      if (a == '@')
        continue;
      A[i][j] = oo;
    }
  }
  Z start = {ys, xs, 0};
  A[ys][xs] = 0;
  q.emplace(start);
  ll ans = bfs();
  if (ans == oo)
    ans = -1;
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using Graph_c = vector<vector<ll>>;
struct Z {
  ll y;
  ll x;
  ll depth;
};

ll H, W, K, y, x, depth, oo = 1e12, xs, ys, xg, yg;
vector<ll> y_vec = {1, 0, -1, 0};
vector<ll> x_vec = {0, 1, 0, -1};
Graph_c A, B;
queue<Z> q;

ll bfs() {
  ll ans = oo;
  while (!q.empty()) {
    Z now = q.front();
    q.pop();
    y = now.y, x = now.x, depth = now.depth;
    if (y == yg && x == xg) {
      ans = min(ans, depth);
      return ans;
    }
    for (ll i = 0; i < 4; i++) {
      for (ll k = 1; k <= K; k++) {
        Z next = {y + y_vec[i] * k, x + x_vec[i] * k, depth + 1};
        // cout<<"ny:"<<next.y<<" nx:"<<next.x<<endl;
        if (next.y <= 0 || next.y > H || next.x <= 0 || next.x > W ||
            A[next.y][next.x] == -1)
          break;
        if (A[next.y][next.x] < next.depth)
          break;
        A[next.y][next.x] = next.depth;
        q.push(next);
        // cout<<" d:"<<next.depth<<endl;
      }
    }
  }
  return oo;
}

int main() {
  cin >> H >> W >> K;
  cin >> ys >> xs >> yg >> xg;
  A.assign(H + 2, vector<ll>(W + 2, -1));
  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      char a;
      cin >> a;
      if (a == '@')
        continue;
      A[i][j] = oo;
    }
  }
  Z start = {ys, xs, 0};
  A[ys][xs] = 0;
  q.emplace(start);
  ll ans = bfs();
  if (ans == oo)
    ans = -1;
  cout << ans << endl;
}
Problem p02644 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
const int maxn = 1e6 + 10;
const int mod = 1e9 + 7;
vector<vector<int>> d(maxn);
int h, w, k;
string s[maxn];
int sx, sy, ex, ey;
int xx[5] = {58, 0, 0, 1, -1};
int yy[5] = {58, 1, -1, 0, 0};

void bfs() {
  queue<pair<int, int>> q;
  q.push({sx, sy});
  d[sx][sy] = 0;
  while (!q.empty()) {
    int x = q.front().first;
    int y = q.front().second;
    if (x == ex && y == ey) {
      return;
    }
    q.pop();
    for (int i = 1; i <= 4; i++) {
      for (int j = 0; j < k; j++) {
        int tx = xx[i] + x;
        int ty = yy[i] + y;
        if (i == 1)
          ty += j;
        if (i == 2)
          ty -= j;
        if (i == 3)
          tx += j;
        if (i == 4)
          tx -= j;
        if (tx < 0 || ty < 0 || tx > h - 1 || ty > w - 1 || s[tx][ty] == '@' ||
            d[tx][ty] < d[x][y] + 1) {
          break;
        }
        if (d[tx][ty] > d[x][y] + 1) // 同样长的话不能走 否则TLE
        {
          d[tx][ty] = d[x][y] + 1;
          q.push({tx, ty});
        }
      }
    }
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> h >> w >> k;
  cin >> sx >> sy >> ex >> ey;
  sx--, sy--, ex--, ey--;
  for (int i = 0; i < h; i++)
    cin >> s[i];
  int g = 1e9;
  for (int i = 0; i < h + 5; i++) {
    for (int j = 0; j < w + 5; j++) {
      d[i].pb(g);
    }
  }
  bfs();
  if (d[ex][ey] == 1e9) {
    cout << -1 << '\\n';
  } else
    cout << d[ex][ey] << '\\n';
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
const int maxn = 1e6 + 10;
const int mod = 1e9 + 7;
vector<vector<int>> d(maxn);
int h, w, k;
string s[maxn];
int sx, sy, ex, ey;
int xx[5] = {58, 0, 0, 1, -1};
int yy[5] = {58, 1, -1, 0, 0};

void bfs() {
  queue<pair<int, int>> q;
  q.push({sx, sy});
  d[sx][sy] = 0;
  while (!q.empty()) {
    int x = q.front().first;
    int y = q.front().second;
    /*if(x==ex&&y==ey)
    {
        return;
    }*/
    q.pop();
    for (int i = 1; i <= 4; i++) {
      for (int j = 0; j < k; j++) {
        int tx = xx[i] + x;
        int ty = yy[i] + y;
        if (i == 1)
          ty += j;
        if (i == 2)
          ty -= j;
        if (i == 3)
          tx += j;
        if (i == 4)
          tx -= j;
        if (tx < 0 || ty < 0 || tx > h - 1 || ty > w - 1 || s[tx][ty] == '@' ||
            d[tx][ty] < d[x][y] + 1) {
          break;
        }
        d[tx][ty] = d[x][y] + 1;
        q.push({tx, ty});
      }
    }
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> h >> w >> k;
  cin >> sx >> sy >> ex >> ey;
  sx--, sy--, ex--, ey--;
  for (int i = 0; i < h; i++)
    cin >> s[i];
  int g = 1e9;
  for (int i = 0; i < h + 5; i++) {
    for (int j = 0; j < w + 5; j++) {
      d[i].pb(g);
    }
  }
  bfs();
  if (d[ex][ey] == 1e9) {
    cout << -1 << '\\n';
  } else
    cout << d[ex][ey] << '\\n';
}
Problem p02644 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
const int maxn = 1e6 + 10;
const int mod = 1e9 + 7;

int h, w, k;
int sx, sy, ex, ey;
string s[maxn];
vector<vector<int>> d(maxn);
int xx[5] = {58, 0, 0, 1, -1};
int yy[5] = {58, 1, -1, 0, 0};

struct ac {
  int x, y, step;
};

void bfs() {
  queue<ac> q;
  q.push({sx, sy, 0});
  d[sx][sy] = 0;
  while (!q.empty()) {
    ac u = q.front();
    q.pop();
    for (int i = 1; i <= 4; i++) {
      for (int j = 1; j <= k; j++) {
        int tx = u.x + xx[i] * j;
        int ty = u.y + yy[i] * j;
        if (tx < 0 || tx >= h || ty < 0 || ty >= w || s[tx][ty] == '@' ||
            d[tx][ty] < u.step + 1)
          break;
        if (d[tx][ty] > u.step + 1) {
          q.push({tx, ty, u.step + 1});
          d[tx][ty] = u.step + 1;
        }
      }
    }
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> h >> w >> k;
  cin >> sx >> sy >> ex >> ey;
  for (int i = 0; i < h; i++) {
    d[i].resize(w);
  }
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      d[i][j] = 1e9;
    }
  }
  sx--, sy--, ex--, ey--;
  for (int i = 0; i < h; i++)
    cin >> s[i];
  bfs();
  if (d[ex][ey] == 1e9)
    cout << -1 << '\\n';
  else
    cout << d[ex][ey] << '\\n';
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
const int maxn = 1e6 + 10;
const int mod = 1e9 + 7;

int h, w, k;
int sx, sy, ex, ey;
string s[maxn];
vector<vector<int>> d(maxn);
int xx[5] = {58, 0, 0, 1, -1};
int yy[5] = {58, 1, -1, 0, 0};

struct ac {
  int x, y, step;
};

void bfs() {
  queue<ac> q;
  q.push({sx, sy, 0});
  d[sx][sy] = 0;
  while (!q.empty()) {
    ac u = q.front();
    q.pop();
    if (u.x == ex && u.y == ey)
      return;
    for (int i = 1; i <= 4; i++) {
      for (int j = 1; j <= k; j++) {
        int tx = u.x + xx[i] * j;
        int ty = u.y + yy[i] * j;
        if (tx < 0 || tx >= h || ty < 0 || ty >= w || s[tx][ty] == '@')
          break;
        if (d[tx][ty] >= u.step + 1) {
          q.push({tx, ty, u.step + 1});
          d[tx][ty] = u.step + 1;
        } else
          break;
      }
    }
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> h >> w >> k;
  cin >> sx >> sy >> ex >> ey;
  for (int i = 0; i < h; i++) {
    d[i].resize(w);
  }
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      d[i][j] = 1e9;
    }
  }
  sx--, sy--, ex--, ey--;
  for (int i = 0; i < h; i++)
    cin >> s[i];
  bfs();
  if (d[ex][ey] == 1e9)
    cout << -1 << '\\n';
  else
    cout << d[ex][ey] << '\\n';
}
Problem p02644 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <array>
#include <cmath>
#include <iostream>
#include <queue>
#include <vector>

const std::array<int32_t, 4> dx = {1, 0, -1, 0};
const std::array<int32_t, 4> dy = {0, 1, 0, -1};

int main() {
  uint32_t H = 0, W = 0, K = 0;
  std::cin >> H >> W >> K;
  std::pair<uint32_t, uint32_t> start, goal;
  std::cin >> start.first >> start.second >> goal.first >> goal.second;
  std::vector<std::vector<int32_t>> G(H + 2, std::vector<int32_t>(W + 2, -1));
  for (size_t i = 0; i < H; i++) {
    std::string str;
    std::cin >> str;
    for (size_t j = 0; j < W; j++) {
      if (str[j] == '.')
        G[i + 1][j + 1] = 0;
    }
  }

  std::queue<std::pair<uint32_t, uint32_t>> Q;
  Q.push(start);
  while (!Q.empty()) {
    uint32_t x = Q.front().first, y = Q.front().second;
    Q.pop();
    for (size_t i = 0; i < dx.size(); i++) {
      for (size_t k = 1; k <= K; k++) {
        uint32_t to_x = x + dx[i] * k, to_y = y + dy[i] * k;
        if ((to_x == start.first && to_y == start.second) ||
            G[to_x][to_y] == -1)
          break;
        if (G[to_x][to_y] != 0) {
          if (G[to_x][to_y] <= G[x][y])
            break;
          continue;
        }
        G[to_x][to_y] = G[x][y] + 1;
        Q.push({to_x, to_y});
      }
    }
  }

  std::cout << (G[goal.first][goal.second] == 0 ? -1
                                                : G[goal.first][goal.second])
            << std::endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <cmath>
#include <iostream>
#include <queue>
#include <vector>

const std::array<int32_t, 4> dx = {1, 0, -1, 0};
const std::array<int32_t, 4> dy = {0, 1, 0, -1};

int main() {
  uint32_t H = 0, W = 0, K = 0;
  std::cin >> H >> W >> K;
  std::pair<uint32_t, uint32_t> start, goal;
  std::cin >> start.first >> start.second >> goal.first >> goal.second;
  std::vector<std::vector<int32_t>> G(H + 2, std::vector<int32_t>(W + 2, -1));
  for (size_t i = 0; i < H; i++) {
    std::string str;
    std::cin >> str;
    for (size_t j = 0; j < W; j++) {
      if (str[j] == '.')
        G[i + 1][j + 1] = 0;
    }
  }

  std::queue<std::pair<uint32_t, uint32_t>> Q;
  Q.push(start);
  while (!Q.empty()) {
    uint32_t x = Q.front().first, y = Q.front().second;
    Q.pop();
    for (size_t i = 0; i < dx.size(); i++) {
      for (size_t k = 1; k <= K; k++) {
        uint32_t to_x = x + dx[i] * k, to_y = y + dy[i] * k;
        if ((to_x == start.first && to_y == start.second) ||
            G[to_x][to_y] == -1)
          break;
        if (G[to_x][to_y] != 0) {
          if (G[to_x][to_y] < G[x][y])
            break;
          continue;
        }
        G[to_x][to_y] = G[x][y] + 1;
        Q.push({to_x, to_y});
      }
    }
  }

  std::cout << (G[goal.first][goal.second] == 0 ? -1
                                                : G[goal.first][goal.second])
            << std::endl;

  return 0;
}