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

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

struct S {
  int d, x, y, v;
  S(int d, int x, int y, int v) : d(d), x(x), y(y), v(v) {}
};
auto cmp = [](S x, S y) { return x.d > y.d; };
priority_queue<S, vector<S>, decltype(cmp)> q(cmp);

int h, w, k, sx, sy, gx, gy, dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
bool inside(int x, int y) { return x >= 0 && x < h && y >= 0 && y < w; }

int main() {
  cin >> h >> w >> k >> sx >> sy >> gx >> gy;
  --sx, --sy, --gx, --gy;
  bool b[h][w], c[h][w][4];
  fill(b[0], b[h], 0);
  for (int i = 0; i < h; ++i) {
    string s;
    cin >> s;
    for (int j = 0; j < w; ++j)
      if (s[j] == '.')
        b[i][j] = 1;
  }

  fill(c[0][0], c[h][0], 0);
  for (int i = 0; i < 4; ++i)
    q.emplace(0, sx, sy, i);

  while (!q.empty()) {
    S s = q.top();
    q.pop();
    if (s.x == gx && s.y == gy) {
      cout << (s.d + k - 1) / k << endl;
      return 0;
    }
    if (c[s.x][s.y][s.v])
      continue;
    c[s.x][s.y][s.v] = 1;
    for (int i = 0; i < 4; ++i) {
      int nx = s.x + dx[i];
      int ny = s.y + dy[i];
      if (!inside(nx, ny) || !b[nx][ny] || c[nx][ny][i])
        continue;
      int nd = (s.v == i ? s.d + 1 : (s.d + k - 1) / k * k + 1);
      q.emplace(nd, nx, ny, i);
    }
  }
  cout << -1 << endl;
  return 0;
}


Error: TLE

Fixed submission:

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

struct S {
  int d, x, y, v, l;
  S();
  S(int d, int x, int y, int v, int l) : d(d), x(x), y(y), v(v), l(l){};
};

auto c = [](S a, S b) { return a.d > b.d; };

int h, w, k, x1, x2, z1, z2, v[][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
priority_queue<S, vector<S>, decltype(c)> q(c);

int main() {
  cin >> h >> w >> k >> x1 >> z1 >> x2 >> z2;
  bool b[h + 2][w + 2];
  fill(b[0], b[h + 2], 0);
  for (int i = 0; i < h; ++i) {
    string s;
    cin >> s;
    for (int j = 0; j < w; ++j)
      if (s[j] == '.')
        b[i + 1][j + 1] = 1;
  }

  int d[h + 2][w + 2];
  fill(d[1], d[h + 1], h * w);
  d[x1][x2] = 0;

  q.emplace(0, x1, z1, -1, k);

  while (!q.empty()) {
    S s = q.top();
    q.pop();
    if (s.x == x2 && s.y == z2) {
      cout << s.d << endl;
      return 0;
    }
    if (s.d > d[s.x][s.y] + 1)
      continue;
    if (s.d < d[s.x][s.y])
      d[s.x][s.y] = s.d;

    for (int i = 0; i < 4; ++i) {
      if ((i + 2) % 4 == s.v)
        continue;
      int nx = s.x + v[i][0];
      int ny = s.y + v[i][1];
      if (!b[nx][ny])
        continue;
      int nd = s.d + 1;
      if (s.v == i && s.l < k)
        --nd;
      int nl = (s.v == i ? s.l % k + 1 : 1);
      q.emplace(nd, nx, ny, i, nl);
    }
  }
  cout << -1 << 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 < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define INF 1e9
typedef long long ll;

vector<int> dx{1, 0, -1, 0};
vector<int> dy{0, 1, 0, -1};

int main() {
  int H, W, K;
  cin >> H >> W >> K;
  pair<int, int> start, goal;
  cin >> start.first >> start.second >> goal.first >> goal.second;

  vector<vector<char>> pond(H + 2, vector<char>(W + 2, '@'));
  vector<vector<int>> G(H + 2, vector<int>(W + 2, -1));
  FOR(i, 1, H + 1) {
    string S;
    cin >> S;
    FOR(j, 1, W + 1) { pond[i][j] = S[j - 1]; }
  }
  G[start.first][start.second] = 0;

  queue<pair<int, int>> q;
  q.push(start);

  while (!q.empty()) {
    auto pos = q.front();
    q.pop();

    if (pos == goal) {
      cout << G[pos.first][pos.second] << endl;
      return 0;
    }
    REP(i, 4) {
      FOR(k, 1, K + 1) {
        int next_x = pos.first + k * dx[i];
        int next_y = pos.second + k * dy[i];
        if (pond[next_x][next_y] == '.' && G[next_x][next_y] == -1) {
          // 水上かつ未探索なら
          G[next_x][next_y] = G[pos.first][pos.second] + 1;
          q.push(make_pair(next_x, next_y));
        } else if (pond[next_x][next_y] == '@' ||
                   G[next_x][next_y] <= G[pos.first][pos.second]) {
          break;
        }
      }
    }
  }

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

Error: TLE

Fixed submission:

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

#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define INF 1e9
typedef long long ll;

vector<int> dx{1, 0, -1, 0};
vector<int> dy{0, 1, 0, -1};

int main() {
  int H, W, K;
  cin >> H >> W >> K;
  pair<int, int> start, goal;
  cin >> start.first >> start.second >> goal.first >> goal.second;

  vector<vector<char>> pond(H + 2, vector<char>(W + 2, '@'));
  vector<vector<int>> G(H + 2, vector<int>(W + 2, -1));
  FOR(i, 1, H + 1) {
    string S;
    cin >> S;
    FOR(j, 1, W + 1) { pond[i][j] = S[j - 1]; }
  }
  G[start.first][start.second] = 0;

  queue<pair<int, int>> q;
  q.push(start);

  while (!q.empty()) {
    auto pos = q.front();
    q.pop();

    if (pos == goal) {
      cout << G[pos.first][pos.second] << endl;
      return 0;
    }
    REP(i, 4) {
      FOR(k, 1, K + 1) {
        int next_x = pos.first + k * dx[i];
        int next_y = pos.second + k * dy[i];
        if (pond[next_x][next_y] == '.' && G[next_x][next_y] == -1) {
          // 水上かつ未探索なら
          G[next_x][next_y] = G[pos.first][pos.second] + 1;
          q.push(make_pair(next_x, next_y));
        } else if (pond[next_x][next_y] == '@') {
          break;
        }
      }
    }
  }

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

#include <bits/stdc++.h>
#define all(x) x.begin(), x.end()
#define ll long long
#define pii pair<int, int>
#define sz(x) (int)x.size()
#define f first
#define s second
using namespace std;
const int N = 2e5 + 3;
const int M = 1e8;
const int tox[4] = {1, -1, 0, 0};
const ll inf = 1e18;
const int toy[4] = {0, 0, 1, -1};
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ll n, m, k;
  cin >> n >> m >> k;
  vector<vector<char>> a(n, vector<char>(m));
  vector<vector<ll>> dst(n, vector<ll>(m, inf));
  ll x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  --x1;
  --x2;
  --y1;
  --y2;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> a[i][j];
    }
  }
  priority_queue<pair<int, pii>, vector<pair<int, pii>>,
                 greater<pair<int, pii>>>
      q;
  q.push({0, {x1, y1}});
  dst[x1][y1] = 0;
  while (!q.empty()) {
    auto c = q.top();
    q.pop();
    int dt = c.f, x = c.s.f, y = c.s.s;
    if (dst[x][y] < dt)
      continue;
    for (int i = 0; i < 4; i++) {
      for (int j = 1; j <= k; j++) {
        int nx = x + tox[i] * j, ny = y + toy[i] * j;
        if (nx < 0 || ny < 0 || nx >= n || ny >= m) {
          break;
        }
        if (a[nx][ny] != '@' && dst[nx][ny] > dt + 1) {
          dst[nx][ny] = dt + 1;
          q.push({dt + 1, {nx, ny}});
        } else if (a[nx][ny] == '@' || dst[nx][ny] < dt + 1) {
          break;
        }
      }
    }
  }
  cout << (dst[x2][y2] == inf ? -1 : dst[x2][y2]);
  return 0;
}
// abcd


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define all(x) x.begin(), x.end()
#define ll long long
#define pii pair<int, int>
#define sz(x) (int)x.size()
#define f first
#define s second
using namespace std;
const int N = 2e5 + 3;
const int M = 1e8;
const int tox[4] = {1, -1, 0, 0};
const ll inf = 1e18;
const int toy[4] = {0, 0, 1, -1};
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ll n, m, k;
  cin >> n >> m >> k;
  vector<vector<char>> a(n, vector<char>(m));
  vector<vector<ll>> dst(n, vector<ll>(m, inf));
  ll x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  --x1;
  --x2;
  --y1;
  --y2;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> a[i][j];
    }
  }
  priority_queue<pair<int, pii>, vector<pair<int, pii>>,
                 greater<pair<int, pii>>>
      q;
  q.push({0, {x1, y1}});
  dst[x1][y1] = 0;
  while (!q.empty()) {
    auto c = q.top();
    q.pop();
    int dt = c.f, x = c.s.f, y = c.s.s;
    if (dst[x][y] < dt)
      continue;
    for (int i = 0; i < 4; i++) {
      for (int j = 1; j <= k; j++) {
        int nx = x + tox[i] * j, ny = y + toy[i] * j;
        if (nx < 0 || ny < 0 || nx >= n || ny >= m) {
          break;
        }
        if (a[nx][ny] != '@' && dst[nx][ny] >= dt + 1) {
          dst[nx][ny] = dt + 1;
          q.push({dt + 1, {nx, ny}});
        } else {
          break;
        }
      }
    }
  }
  cout << (dst[x2][y2] == inf ? -1 : dst[x2][y2]);
  return 0;
}
// abcd
Problem p02644 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define f first
#define s second
#define pii pair<int, int>
using namespace std;
typedef queue<pii> qpii;

int xo[4] = {1, -1, 0, 0};
int yo[4] = {0, 0, 1, -1};
int n, m;

bool good(int x, int y) { return x >= 1 && x <= n && y >= 1 && y <= m; }

void test_case() {
  int k, a, b, c, d;
  cin >> n >> m >> k >> a >> b >> c >> d;

  vector<string> grid(n + 1);
  vector<vector<int>> odl(n + 1, vector<int>(m + 1, 1e9));
  vector<vector<bool>> odw(n + 1, vector<bool>(m + 1));
  for (int i = 1; i <= n; i++)
    cin >> grid[i];
  qpii Q;
  Q.push({a, b});
  odl[a][b] = 0;
  odw[a][b] = true;

  while (!Q.empty()) {
    pii cur = Q.front();
    Q.pop();
    for (int i = 0; i < 4; i++) {
      for (int j = 1; j <= k; j++) {
        int X = cur.f + xo[i] * j;
        int Y = cur.s + yo[i] * j;
        if (good(X, Y)) {
          if (grid[X][Y - 1] == '@' || odl[X][Y] < odl[cur.f][cur.s] + 1)
            break;
          if (!odw[X][Y]) {
            odl[X][Y] = odl[cur.f][cur.s] + 1;
            odw[X][Y] = true;
            Q.push({X, Y});
          }
        }
      }
    }
  }
  cout << (odl[c][d] == 1e9 ? -1 : odl[c][d]) << '\\n';
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  test_case();
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define f first
#define s second
#define pii pair<int, int>
using namespace std;
typedef queue<pii> qpii;

int xo[4] = {1, -1, 0, 0};
int yo[4] = {0, 0, 1, -1};
int n, m;

bool good(int x, int y) { return x >= 1 && x <= n && y >= 1 && y <= m; }

void test_case() {
  int k, a, b, c, d;
  cin >> n >> m >> k >> a >> b >> c >> d;

  vector<string> grid(n + 1);
  vector<vector<int>> odl(n + 1, vector<int>(m + 1, 1e9));
  vector<vector<bool>> odw(n + 1, vector<bool>(m + 1));
  for (int i = 1; i <= n; i++)
    cin >> grid[i];
  qpii Q;
  Q.push({a, b});
  odl[a][b] = 0;
  odw[a][b] = true;

  while (!Q.empty()) {
    pii cur = Q.front();
    Q.pop();
    for (int i = 0; i < 4; i++) {
      for (int j = 1; j <= k; j++) {
        int X = cur.f + xo[i] * j;
        int Y = cur.s + yo[i] * j;
        if (good(X, Y) && !odw[X][Y]) {
          if (grid[X][Y - 1] == '@' || odl[X][Y] < odl[cur.f][cur.s] + 1)
            break;
          odl[X][Y] = odl[cur.f][cur.s] + 1;
          odw[X][Y] = true;
          Q.push({X, Y});
        }
      }
    }
  }
  cout << (odl[c][d] == 1e9 ? -1 : odl[c][d]) << '\\n';
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  test_case();
  return 0;
}
Problem p02644 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define fast_cin() ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define endl '\\n'
#define int long long

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

int32_t main() {
  fast_cin();
  int n, m, k;
  cin >> n >> m >> k;
  int x1, x2, y1, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  --x1, --x2, --y1, --y2;
  string s[n + 1];
  for (int i = 0; i < n; ++i)
    cin >> s[i];
  queue<pair<int, int>> q;
  q.push({x1, y1});
  int dist[n + 1][m + 1];
  bool vis[n + 1][m + 1];
  memset(vis, false, sizeof vis);
  memset(dist, 5, sizeof dist);
  dist[x1][y1] = 0;
  auto valid = [&](int x, int y) {
    return x >= 0 and x < n and y >= 0 and y < m and s[x][y] != '@';
  };
  while (!q.empty()) {
    auto [x, y] = q.front();
    q.pop();
    vis[x][y] = true;
    for (int dir = 0; dir < 4; ++dir) {
      for (int len = 1; len <= k; ++len) {
        int nx = x + dx[dir] * len, ny = y + dy[dir] * len;
        if (!valid(nx, ny))
          break;
        if (vis[nx][ny] and dist[nx][ny] < dist[x][y] + 1)
          break;
        if (vis[nx][ny]) {
          assert(dist[nx][ny] == dist[x][y] + 1);
          continue;
        }
        dist[nx][ny] = dist[x][y] + 1;
        vis[nx][ny] = true;
        q.push({nx, ny});
      }
    }
  }
  int ans = dist[x2][y2];
  if (!vis[x2][y2])
    ans = -1;
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

#define fast_cin() ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define endl '\\n'
#define int long long

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

int32_t main() {
  fast_cin();
  int n, m, k;
  cin >> n >> m >> k;
  int x1, x2, y1, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  --x1, --x2, --y1, --y2;
  string s[n + 1];
  for (int i = 0; i < n; ++i)
    cin >> s[i];
  queue<pair<int, int>> q;
  q.push({x1, y1});
  int dist[n + 1][m + 1];
  bool vis[n + 1][m + 1];
  memset(vis, false, sizeof vis);
  memset(dist, 5, sizeof dist);
  dist[x1][y1] = 0;
  auto valid = [&](int x, int y) {
    return x >= 0 and x < n and y >= 0 and y < m and s[x][y] != '@';
  };
  while (!q.empty()) {
    auto [x, y] = q.front();
    q.pop();
    vis[x][y] = true;
    for (int dir = 0; dir < 4; ++dir) {
      for (int len = 1; len <= k; ++len) {
        int nx = x + dx[dir] * len, ny = y + dy[dir] * len;
        if (!valid(nx, ny))
          break;
        if (vis[nx][ny] and dist[nx][ny] < dist[x][y] + 1)
          break;
        if (vis[nx][ny])
          assert(dist[nx][ny] == dist[x][y] + 1);
        dist[nx][ny] = dist[x][y] + 1;
        vis[nx][ny] = true;
        q.push({nx, ny});
      }
    }
  }
  int ans = dist[x2][y2];
  if (!vis[x2][y2])
    ans = -1;
  cout << ans << endl;
  return 0;
}
Problem p02644 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef int64_t ll;
const int INF = (int)1e9;
int H, W, K;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};

int main() {
  int x1, y1, x2, y2;
  cin >> H >> W >> K;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--, y1--, x2--, y2--;
  vector<string> S(H);
  for (int i = 0; i < H; i++)
    cin >> S[i];
  vector<vector<int>> D(H, vector<int>(W, INF));
  queue<pair<int, int>> P;
  D[x1][y1] = 0;
  P.push({x1, y1});
  while (!P.empty()) {
    int x = P.front().first, y = P.front().second;
    P.pop();
    int nd = D[x][y] + 1;
    for (int r = 0; r < 4; r++) {
      int tx = x, ty = y;
      for (int i = 0; i < K; i++) {
        tx += dx[r], ty += dy[r];
        if (tx < 0 || ty < 0 || tx >= H || ty >= W || S[tx][ty] == '@' ||
            D[tx][ty] < nd)
          break;
        if (D[tx][ty] > nd) {
          D[tx][ty] = nd;
          P.push(make_pair(tx, ty));
        }
      }
    }
  }
  int ans = D[x2][y2];
  cout << (ans < INF ? ans : -1) << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef int64_t ll;
const int INF = (int)1e9;
int H, W, K;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};

int main() {
  int x1, y1, x2, y2;
  cin >> H >> W >> K;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--, y1--, x2--, y2--;
  vector<string> S(H);
  for (int i = 0; i < H; i++)
    cin >> S[i];
  vector<vector<int>> D(H, vector<int>(W, INF));
  queue<pair<int, int>> P;
  D[x1][y1] = 0;
  P.push({x1, y1});
  while (!P.empty()) {
    int x = P.front().first, y = P.front().second;
    P.pop();
    int nd = D[x][y] + 1;
    for (int r = 0; r < 4; r++) {
      int tx = x, ty = y;
      for (int i = 0; i < K; i++) {
        tx += dx[r], ty += dy[r];
        if (tx < 0 || ty < 0 || tx >= H || ty >= W || S[tx][ty] == '@')
          break;
        if (D[tx][ty] > nd) {
          D[tx][ty] = nd;
          P.push(make_pair(tx, ty));
        }
      }
    }
  }
  int ans = D[x2][y2];
  cout << (ans < INF ? ans : -1) << endl;
}
Problem p02644 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll inf = 1e18;
#define rep(i, a, b) for (int i = a; i < b; i++)
#define per(i, a, b) for (int i = b - 1; i >= a; i--)
#define int ll
using pint = pair<int, int>;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};

signed main() {
  int h, w, k;
  cin >> h >> w >> k;
  int xs, ys, xt, yt;
  cin >> ys >> xs >> yt >> xt;
  --xs, --xt, --ys, --yt;
  char c[h][w];
  rep(i, 0, h) rep(j, 0, w) cin >> c[i][j];
  int dist[h][w];
  rep(i, 0, h) rep(j, 0, w) dist[i][j] = inf;
  queue<pint> q;
  q.push({ys, xs});
  dist[ys][xs] = 0;
  while (!q.empty()) {
    pint now = q.front();
    q.pop();
    int ny = now.first, nx = now.second;
    rep(i, 0, 4) {
      rep(j, 1, k + 1) {
        int nexty = ny + dy[i] * j, nextx = nx + dx[i] * j;
        if (nexty < 0 || nexty >= h || nextx < 0 || nextx >= w)
          break;
        if (c[nexty][nextx] == '@')
          break;
        if (dist[nexty][nextx] <= dist[ny][nx])
          break;
        if (dist[nexty][nextx] > dist[ny][nx] + 1) {
          dist[nexty][nextx] = dist[ny][nx] + 1;
          q.push({nexty, nextx});
        }
      }
    }
  }
  /*
  rep (i, 0, h) {
      rep (j, 0, w) cout << dist[i][j] << " ";
      cout << "\\n";
  }
  */
  if (dist[yt][xt] == inf) {
    cout << "-1\\n";
  } else {
    cout << dist[yt][xt] << "\\n";
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll inf = 1e18;
#define rep(i, a, b) for (int i = a; i < b; i++)
#define per(i, a, b) for (int i = b - 1; i >= a; i--)
#define int ll
using pint = pair<int, int>;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};

signed main() {
  int h, w, k;
  cin >> h >> w >> k;
  int xs, ys, xt, yt;
  cin >> ys >> xs >> yt >> xt;
  --xs, --xt, --ys, --yt;
  char c[h][w];
  rep(i, 0, h) rep(j, 0, w) cin >> c[i][j];
  int dist[h][w];
  rep(i, 0, h) rep(j, 0, w) dist[i][j] = inf;
  queue<pint> q;
  q.push({ys, xs});
  dist[ys][xs] = 0;
  while (!q.empty()) {
    pint now = q.front();
    q.pop();
    int ny = now.first, nx = now.second;
    rep(i, 0, 4) {
      rep(j, 1, k + 1) {
        int nexty = ny + dy[i] * j, nextx = nx + dx[i] * j;
        if (nexty < 0 || nexty >= h || nextx < 0 || nextx >= w)
          break;
        if (c[nexty][nextx] == '@')
          break;
        if (dist[nexty][nextx] <= dist[ny][nx])
          break;
        dist[nexty][nextx] = dist[ny][nx] + 1;
        q.push({nexty, nextx});
      }
    }
  }
  /*
  rep (i, 0, h) {
      rep (j, 0, w) cout << dist[i][j] << " ";
      cout << "\\n";
  }
  */
  if (dist[yt][xt] == inf) {
    cout << "-1\\n";
  } else {
    cout << dist[yt][xt] << "\\n";
  }
}
Problem p02644 (C++) — buggy submission, verdict: Time Limit Exceeded

#pragma GCC optimize("Ofast", "inline", "-ffast-math")
#pragma GCC target("avx,sse2,sse3,sse4,mmx")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/hash_policy.hpp>
using namespace __gnu_pbds;
using namespace std;

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

signed main() {
  ios::sync_with_stdio(false);
  cin >> n >> m >> k;
  vector<vector<int>> d;
  d.resize(n + 1, vector<int>(m + 1, -1));
  vector<vector<char>> c;
  c.resize(n + 1, vector<char>(m + 1));
  int xa, ya, xb, yb;
  cin >> xa >> ya >> xb >> yb;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> c[i][j];
    }
  }
  queue<pair<int, int>> q;
  d[xa][ya] = 0;
  q.push(make_pair(xa, ya));
  while (!q.empty()) {
    pair<int, int> t = q.front();
    q.pop();
    for (int dir = 0; dir < 4; dir++) {
      int nx = t.first, ny = t.second;
      for (int i = 1; i <= k; i++) {
        nx += dx[dir], ny += dy[dir];
        if (nx <= 0 || ny <= 0 || nx > n || ny > m)
          break;
        if (d[nx][ny] >= 0 && d[nx][ny] <= d[t.first][t.second])
          break;
        if (c[nx][ny] == '@')
          break;
        if (d[nx][ny] == -1) {
          d[nx][ny] = d[t.first][t.second] + 1;
          q.push(make_pair(nx, ny));
        }
      }
    }
  }
  cout << d[xb][yb] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#pragma GCC optimize("Ofast", "inline", "-ffast-math")
#pragma GCC target("avx,sse2,sse3,sse4,mmx")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/hash_policy.hpp>
using namespace __gnu_pbds;
using namespace std;

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

signed main() {
  ios::sync_with_stdio(false);
  cin >> n >> m >> k;
  vector<vector<int>> d;
  d.resize(n + 1, vector<int>(m + 1, -1));
  vector<vector<char>> c;
  c.resize(n + 1, vector<char>(m + 1));
  int xa, ya, xb, yb;
  cin >> xa >> ya >> xb >> yb;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> c[i][j];
    }
  }
  queue<pair<int, int>> q;
  d[xa][ya] = 0;
  q.push(make_pair(xa, ya));
  while (!q.empty()) {
    pair<int, int> t = q.front();
    q.pop();
    for (int dir = 0; dir < 4; dir++) {
      int nx = t.first, ny = t.second;
      for (int i = 1; i <= k; i++) {
        nx += dx[dir], ny += dy[dir];
        if (nx <= 0 || ny <= 0 || nx > n || ny > m)
          break;
        if (d[nx][ny] >= 0 && d[nx][ny] <= d[t.first][t.second])
          break;
        if (c[nx][ny] == '@')
          break;
        d[nx][ny] = d[t.first][t.second] + 1;
        q.push(make_pair(nx, ny));
      }
    }
  }
  cout << d[xb][yb] << endl;
  return 0;
}