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

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

vector<pair<int, int>> dir = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};

int main() {
  ios::sync_with_stdio(0);
  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<string> grid(h);
  for (int i = 0; i < h; ++i)
    cin >> grid[i];
  auto valid = [&](const pair<int, int> &p) {
    return p.first >= 0 && p.first < h && p.second >= 0 && p.second < w;
  };

  vector<vector<int>> dist(h, vector<int>(w, 1e9));
  auto bfs = [&]() {
    queue<pair<int, int>> q;
    vector<vector<bool>> vis(h, vector<bool>(w));
    q.emplace(x1, y1);
    dist[x1][y1] = 0;
    vis[x1][y1] = 1;
    while (!q.empty()) {
      auto u = q.front();
      q.pop();
      for (int j = 0; j < 4; ++j) {
        for (int i = 1; i <= k; ++i) {
          int nr = u.first + i * dir[j].first;
          int nc = u.second + i * dir[j].second;
          if (!valid({nr, nc}))
            continue;
          if (grid[nr][nc] == '@' || dist[nr][nc] <= dist[u.first][u.second])
            break;
          if (vis[nr][nc])
            continue;
          dist[nr][nc] = dist[u.first][u.second] + 1;
          vis[nr][nc] = 1;
          q.emplace(nr, nc);
        }
      }
    }
    return (dist[x2][y2] >= (int)1e9 ? -1 : dist[x2][y2]);
  };
  cout << bfs() << '\\n';
}


Error: TLE

Fixed submission:

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

vector<pair<int, int>> dir = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};

int main() {
  ios::sync_with_stdio(0);
  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<string> grid(h);
  for (int i = 0; i < h; ++i)
    cin >> grid[i];
  auto valid = [&](const pair<int, int> &p) {
    return p.first >= 0 && p.first < h && p.second >= 0 && p.second < w;
  };

  auto bfs = [&]() {
    queue<pair<int, int>> q;
    vector<vector<bool>> vis(h, vector<bool>(w));
    vector<vector<int>> dist(h, vector<int>(w, 1e9));
    q.emplace(x1, y1);
    dist[x1][y1] = 0;
    // vis[x1][y1] = 1;
    while (!q.empty()) {
      auto u = q.front();
      q.pop();
      for (int j = 0; j < 4; ++j) {
        for (int i = 1; i <= k; ++i) {
          int nr = u.first + i * dir[j].first;
          int nc = u.second + i * dir[j].second;
          if (!valid({nr, nc}) || vis[nr][nc])
            continue;
          if (grid[nr][nc] != '.' || dist[nr][nc] <= dist[u.first][u.second])
            break;
          dist[nr][nc] = dist[u.first][u.second] + 1;
          vis[nr][nc] = 1;
          q.emplace(nr, nc);
        }
      }
    }
    return (dist[x2][y2] >= (int)1e9 ? -1 : dist[x2][y2]);
  };
  cout << bfs() << '\\n';
}
Problem p02644 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
#define LL long long
#define pb push_back
#define mp make_pair
#define INF 0x3f3f3f3f
#define Inf 1000000000000000000LL
#define F first
#define S second
using namespace std;
typedef pair<int, int> pii;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int n, m, k;
int sx, sy, ex, ey;
vector<vector<int>> a;
vector<vector<int>> d;
bool outboard(int x, int y) { return x < 1 || x > n || y < 1 || y > m; }
int main() {
  cin >> n >> m >> k;
  cin >> sx >> sy >> ex >> ey;
  vector<int> tp(m + 10, INF);
  for (int i = 1; i <= n + 10; i++)
    d.pb(tp);
  for (int i = 1; i <= n + 10; i++)
    a.pb(tp);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      char c;
      cin >> c;
      a[i][j] = (c == '@');
    }
  }
  queue<pii> pq;
  pq.push(mp(sx, sy));
  d[sx][sy] = 0;
  while (pq.size()) {
    pii x = pq.front();
    pq.pop();
    int nstep = d[x.F][x.S] + 1;
    for (int i = 0; i < 4; i++) {
      for (int j = 1; j <= k; j++) {
        int tx = x.F + dx[i] * j, ty = x.S + dy[i] * j;
        if (outboard(tx, ty) || a[tx][ty] || d[tx][ty] < nstep)
          break;
        if (d[tx][ty] > nstep) {
          d[tx][ty] = nstep;
          pq.push(mp(tx, ty));
        }
      }
    }
  }
  if (d[ex][ey] == INF)
    puts("-1");
  else
    cout << d[ex][ey] << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
#define LL long long
#define pb push_back
#define mp make_pair
#define INF 0x3f3f3f3f
#define Inf 1000000000000000000LL
#define F first
#define S second
using namespace std;
typedef pair<int, int> pii;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int n, m, k;
int sx, sy, ex, ey;
vector<vector<int>> a;
vector<vector<int>> d;
bool outboard(int x, int y) { return x < 1 || x > n || y < 1 || y > m; }
int main() {
  cin >> n >> m >> k;
  cin >> sx >> sy >> ex >> ey;
  vector<int> tp(m + 10, INF);
  for (int i = 1; i <= n + 10; i++)
    d.pb(tp);
  for (int i = 1; i <= n + 10; i++)
    a.pb(tp);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      char c;
      cin >> c;
      a[i][j] = (c == '@');
    }
  }
  queue<pii> pq;
  pq.push(mp(sx, sy));
  d[sx][sy] = 0;
  while (pq.size()) {
    pii x = pq.front();
    pq.pop();
    int nstep = d[x.F][x.S] + 1;
    for (int i = 0; i < 4; i++) {
      for (int j = 1; j <= k; j++) {
        int tx = x.F + dx[i] * j, ty = x.S + dy[i] * j;
        if (outboard(tx, ty) || a[tx][ty])
          break;
        if (d[tx][ty] > nstep) {
          d[tx][ty] = nstep;
          pq.push(mp(tx, ty));
        }
      }
    }
  }
  if (d[ex][ey] == INF)
    puts("-1");
  else
    cout << d[ex][ey] << endl;
  return 0;
}
Problem p02644 (C++) — buggy submission, verdict: Time Limit Exceeded

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

const ll INF = 1e18;

int main() {
  int h, w, k, x, y, nx, ny, K;
  cin >> h >> w >> k;
  int a, b, c, d;
  cin >> a >> b >> c >> d;
  a--;
  b--;
  c--;
  d--;
  vector<string> g(h);
  rep(i, h) cin >> g[i];
  vector<int> dx = {1, -1, 0, 0};
  vector<int> dy = {0, 0, 1, -1};
  vector<vector<ll>> depth(h, vector<ll>(w, INF));
  queue<P> q;
  q.push(make_pair(b, a));
  depth[a][b] = 0;
  while (!q.empty()) {
    x = q.front().first;
    y = q.front().second;
    q.pop();
    if (x == d and y == c)
      break;
    rep(i, 4) {
      nx = x;
      ny = y;
      K = k;
      while (K--) {
        nx += dx[i];
        ny += dy[i];
        if (0 <= nx and nx < w and 0 <= ny and ny < h and g[ny][nx] != '@' and
            depth[ny][nx] > depth[y][x]) {
          if (depth[ny][nx] != depth[y][x] + 1) {
            depth[ny][nx] = depth[y][x] + 1;
            q.push(make_pair(nx, ny));
          }
        } else
          break;
      }
    }
  }
  if (depth[c][d] == INF)
    cout << -1 << endl;
  else
    cout << depth[c][d] << endl;
}

Error: TLE

Fixed submission:

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

const ll INF = 1e18;

int main() {
  int h, w, k, x, y, nx, ny, K;
  cin >> h >> w >> k;
  int a, b, c, d;
  cin >> a >> b >> c >> d;
  a--;
  b--;
  c--;
  d--;
  vector<string> g(h);
  rep(i, h) cin >> g[i];
  vector<int> dx = {1, -1, 0, 0};
  vector<int> dy = {0, 0, 1, -1};
  vector<vector<ll>> depth(h, vector<ll>(w, INF));
  queue<P> q;
  q.push(make_pair(b, a));
  depth[a][b] = 0;
  while (!q.empty()) {
    x = q.front().first;
    y = q.front().second;
    q.pop();
    if (x == d and y == c)
      break;
    rep(i, 4) {
      nx = x;
      ny = y;
      K = k;
      while (K--) {
        nx += dx[i];
        ny += dy[i];
        if (0 <= nx and nx < w and 0 <= ny and ny < h and g[ny][nx] != '@' and
            depth[ny][nx] > depth[y][x]) {
          depth[ny][nx] = depth[y][x] + 1;
          q.push(make_pair(nx, ny));
        } else
          break;
      }
    }
  }
  if (depth[c][d] == INF)
    cout << -1 << endl;
  else
    cout << depth[c][d] << endl;
}
Problem p02644 (C++) — buggy submission, verdict: Time Limit Exceeded

// F.

#include <algorithm>
#include <iostream>
#include <queue>
#include <sstream>
#include <vector>

using namespace std;
typedef long long LL;
typedef pair<int, int> II;
typedef pair<LL, II> III;

int main(int argc, char *argv[]) {
#ifdef _MSC_VER
  while (true)
#endif
  {
    int h = -1, w, k, sx, sy, gx, gy;
    LL ans = -1;
    cin >> h >> w >> k >> sy >> sx >> gy >> gx;
    --sx, --sy, --gx, --gy;
    if (h < 0)
      return 0;
    vector<string> b(h);
    for (int i = 0; i < h; ++i) {
      cin >> b[i];
    }
    priority_queue<III, vector<III>, greater<>> q;
    vector<int> dist(h * w, 1 << 30);
    q.emplace(III(0, II(sy, sx)));
    dist[sy * w + sx] = 0;
    while (!q.empty()) {
      III top = q.top();
      q.pop();
      int y = top.second.first, x = top.second.second;
      if (x == gx && y == gy) {
        ans = top.first;
        break;
      }
      const int dx[4] = {-1, 1, 0, 0};
      const int dy[4] = {0, 0, -1, 1};
      for (int dir = 0; dir < 4; ++dir) {
        for (int d = 1; d <= k; ++d) {
          int nx = x + dx[dir] * d, ny = y + dy[dir] * d;
          if (nx < 0 || nx >= w || ny < 0 || ny >= h)
            break;
          if (b[ny][nx] != '.')
            break;
          int cost = top.first + 1;
          if (cost > dist[ny * w + nx])
            break;
          if (cost < dist[ny * w + nx]) {
            dist[ny * w + nx] = cost;
            q.emplace(III(cost, II(ny, nx)));
          }
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

// F.

#include <algorithm>
#include <iostream>
#include <queue>
#include <sstream>
#include <vector>

using namespace std;
typedef long long LL;
typedef pair<int, int> II;
typedef pair<LL, II> III;

int main(int argc, char *argv[]) {
#ifdef _MSC_VER
  while (true)
#endif
  {
    int h = -1, w, k, sx, sy, gx, gy;
    LL ans = -1;
    cin >> h >> w >> k >> sy >> sx >> gy >> gx;
    --sx, --sy, --gx, --gy;
    if (h < 0)
      return 0;
    vector<string> b(h);
    for (int i = 0; i < h; ++i) {
      cin >> b[i];
    }
    priority_queue<III, vector<III>, greater<>> q;
    vector<int> dist(h * w, 1 << 30);
    q.emplace(III(0, II(sy, sx)));
    dist[sy * w + sx] = 0;
    while (!q.empty()) {
      III top = q.top();
      q.pop();
      int y = top.second.first, x = top.second.second;
      if (x == gx && y == gy) {
        ans = top.first;
        break;
      }
      const int dx[4] = {-1, 1, 0, 0};
      const int dy[4] = {0, 0, -1, 1};
      for (int dir = 0; dir < 4; ++dir) {
        for (int d = 1; d <= k; ++d) {
          int nx = x + dx[dir] * d, ny = y + dy[dir] * d;
          if (nx < 0 || nx >= w || ny < 0 || ny >= h)
            break;
          if (b[ny][nx] != '.')
            break;
          int cost = top.first + 1;
          if (cost > dist[ny * w + nx])
            break;
          dist[ny * w + nx] = cost;
          q.emplace(III(cost, II(ny, nx)));
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p02644 (C++) — buggy submission, verdict: Time Limit Exceeded

// 6/14 リアルタイム参加
#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>;
using Graph = vector<vector<int>>;

int H, W, K;
int sh, sw, gh, gw;
vector<string> field;

// vector<vector<bool>> seen;
// vector<vector<ll>> dist;

const int dh[4] = {1, -1, 0, 0};
const int dw[4] = {0, 0, 1, -1};

int main() {
  cin >> H >> W >> K;
  cin >> sh >> sw >> gh >> gw;
  sh--;
  sw--;
  gh--;
  gw--;

  field.resize(H);
  rep(h, H) { cin >> field[h]; }

  // memset(seen, 0, sizeof(seen));

  vector<vector<ll>> dist(H, vector<ll>(W, -1)); // 未訪問
  queue<P> que;

  dist[sh][sw] = 0;
  que.push({sh, sw});

  while (!que.empty()) {
    P p = que.front();
    que.pop();

    // pから辿れる場所を全部たどる。
    rep(i, 4) {
      int count = 0;

      for (int j = 1; j <= K; j++) {
        int nh = p.first + dh[i] * j;
        int nw = p.second + dw[i] * j;

        // 場外アウトしたり、ハスの場合はbreak。それ以上伸ばさない。
        if (nh < 0 || nh >= H || nw < 0 || nw >= W)
          break;
        if (field[nh][nw] == '@')
          break;

        // 移動先が探索済みの時もブレイク。
        if (dist[nh][nw] != -1) {
          count += 1;

          if (count > 100)
            break;

          continue;
        }

        dist[nh][nw] = dist[p.first][p.second] + 1;
        que.push({nh, nw});
      }
    }
  }

  // rep(h, H) {
  //     rep(w, W) {
  //         cout << dist[h][w] << " ";
  //     }
  //     cout << endl;
  // }

  cout << dist[gh][gw] << endl;
}

Error: TLE

Fixed submission:

// 6/14 リアルタイム参加
#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>;
using Graph = vector<vector<int>>;

int H, W, K;
int sh, sw, gh, gw;
vector<string> field;

// vector<vector<bool>> seen;
// vector<vector<ll>> dist;

const int dh[4] = {1, -1, 0, 0};
const int dw[4] = {0, 0, 1, -1};

int main() {
  cin >> H >> W >> K;
  cin >> sh >> sw >> gh >> gw;
  sh--;
  sw--;
  gh--;
  gw--;

  field.resize(H);
  rep(h, H) { cin >> field[h]; }

  // memset(seen, 0, sizeof(seen));

  vector<vector<ll>> dist(H, vector<ll>(W, -1)); // 未訪問
  queue<P> que;

  dist[sh][sw] = 0;
  que.push({sh, sw});

  while (!que.empty()) {
    P p = que.front();
    que.pop();

    // pから辿れる場所を全部たどる。
    rep(i, 4) {
      for (int j = 1; j <= K; j++) {
        int nh = p.first + dh[i] * j;
        int nw = p.second + dw[i] * j;

        // 場外アウトしたり、ハスの場合はbreak。それ以上伸ばさない。
        if (nh < 0 || nh >= H || nw < 0 || nw >= W)
          break;
        if (field[nh][nw] == '@')
          break;

        // 移動先が探索済みの時もブレイク。
        if (dist[nh][nw] != -1)
          continue;

        dist[nh][nw] = dist[p.first][p.second] + 1;
        que.push({nh, nw});
      }
    }
  }

  // rep(h, H) {
  //     rep(w, W) {
  //         cout << dist[h][w] << " ";
  //     }
  //     cout << endl;
  // }

  cout << dist[gh][gw] << endl;
}
Problem p02644 (C++) — buggy submission, verdict: Runtime Error

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

const int INF = 1 << 30;
const int MaxRC = 1e3;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};

struct DATA {
  int x, y;
  DATA(int x = 0, int y = 0) : x(x), y(y) {}
} st, ed, now, nxt;

int main() {
  int R, C, K;
  while (scanf("%d %d %d\\n", &R, &C, &K) != EOF) {

    scanf("%d %d %d %d\\n", &st.x, &st.y, &ed.x, &ed.y);
    st.x--, st.y--, ed.x--, ed.y--;
    vector<vector<char>> mat(R, vector<char>(C));
    for (int i = 0; i < R; i++, getchar())
      for (int j = 0; j < C; j++)
        mat[i][j] = getchar();

    vector<vector<int>> step(R, vector<int>(C, INF));
    queue<DATA> Q;
    Q.push(DATA(st.x, st.y));
    step[st.x][st.y] = 0;
    while (Q.empty() == 0) {
      now = Q.front();
      Q.pop();
      if (now.x == ed.x and now.y == ed.y)
        break;
      for (int i = 0; i < 4; i++)
        for (int k = 1; k <= K; k++) {
          nxt = DATA(now.x + k * dx[i], now.y + k * dy[i]);
          if (nxt.x == -1 or nxt.x == R or nxt.y == -1 or nxt.y == C or
              mat[nxt.x][nxt.y] == '@' or
              step[nxt.x][nxt.y] <= step[now.x][now.y])
            break;
          if (step[nxt.x][nxt.y] > step[now.x][now.y] + 1) {
            step[nxt.x][nxt.y] = step[now.x][now.y] + 1;
            Q.push(nxt);
          }
        }
    }
    printf("%d\\n", (step[ed.x][ed.y] == INF) ? -1 : step[ed.x][ed.y]);
  }
}


Error: -11

Fixed submission:

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

const int INF = 1 << 30;
const int MaxRC = 1e3;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};

struct DATA {
  int x, y;
  DATA(int x = 0, int y = 0) : x(x), y(y) {}
} st, ed, now, nxt;

int main() {
  int R, C, K;
  while (scanf("%d %d %d\\n", &R, &C, &K) != EOF) {

    vector<vector<char>> mat(R, vector<char>(C));
    for (int i = 0; i < R; i++, getchar())
      for (int j = 0; j < C; j++)
        mat[i][j] = getchar();
    scanf("%d %d %d %d\\n", &st.x, &st.y, &ed.x, &ed.y);
    st.x--, st.y--, ed.x--, ed.y--;

    vector<vector<int>> step(R, vector<int>(C, INF));
    queue<DATA> Q;
    Q.push(DATA(st.x, st.y));
    step[st.x][st.y] = 0;
    while (Q.empty() == 0) {
      now = Q.front();
      Q.pop();
      if (now.x == ed.x and now.y == ed.y)
        break;
      for (int i = 0; i < 4; i++)
        for (int k = 1; k <= K; k++) {
          nxt = DATA(now.x + k * dx[i], now.y + k * dy[i]);
          if (nxt.x == -1 or nxt.x == R or nxt.y == -1 or nxt.y == C or
              mat[nxt.x][nxt.y] == '@' or
              step[nxt.x][nxt.y] <= step[now.x][now.y])
            break;
          if (step[nxt.x][nxt.y] > step[now.x][now.y] + 1) {
            step[nxt.x][nxt.y] = step[now.x][now.y] + 1;
            Q.push(nxt);
          }
        }
    }
    printf("%d\\n", (step[ed.x][ed.y] == INF) ? -1 : step[ed.x][ed.y]);
  }
}
Problem p02644 (C++) — buggy submission, verdict: Time Limit Exceeded

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

const int INF = 1 << 30;
const int MaxRC = 1e3;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};

struct DATA {
  int x, y;
  DATA(int x = 0, int y = 0) : x(x), y(y) {}
} st, ed, now, nxt;

int main() {
  int R, C, K;
  while (scanf("%d %d %d\\n", &R, &C, &K) != EOF) {
    scanf("%d %d %d %d\\n", &st.x, &st.y, &ed.x, &ed.y);
    st.x--, st.y--, ed.x--, ed.y--;

    vector<vector<char>> mat(R, vector<char>(C));
    for (int i = 0; i < R; i++, getchar())
      for (int j = 0; j < C; j++)
        mat[i][j] = getchar();
    vector<vector<int>> step(R, vector<int>(C, INF));
    queue<DATA> Q;
    Q.push(DATA(st.x, st.y));
    step[st.x][st.y] = 0;
    while (Q.empty() == 0) {
      now = Q.front();
      Q.pop();
      if (now.x == ed.x and now.y == ed.y)
        break;
      for (int i = 0; i < 4; i++)
        for (int k = 1; k <= K; k++) {
          nxt = DATA(now.x + k * dx[i], now.y + k * dy[i]);
          if (nxt.x == -1 or nxt.x == R or nxt.y == -1 or nxt.y == C or
              mat[nxt.x][nxt.y] == '@' or
              step[nxt.x][nxt.y] <= step[now.x][now.y])
            break;
          else if (step[nxt.x][nxt.y] > step[now.x][now.y] + 1) {
            step[nxt.x][nxt.y] = step[now.x][now.y] + 1;
            Q.push(nxt);
          }
        }
    }
    printf("%d\\n", (step[ed.x][ed.y] == INF) ? -1 : step[ed.x][ed.y]);
  }
}


Error: TLE

Fixed submission:

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

const int INF = 1 << 30;
const int MaxRC = 1e3;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};

struct DATA {
  int x, y;
  DATA(int x = 0, int y = 0) : x(x), y(y) {}
} st, ed, now, nxt;

int main() {
  int R, C, K;
  while (scanf("%d %d %d\\n", &R, &C, &K) != EOF) {
    scanf("%d %d %d %d\\n", &st.x, &st.y, &ed.x, &ed.y);
    st.x--, st.y--, ed.x--, ed.y--;

    vector<vector<char>> mat(R, vector<char>(C));
    for (int i = 0; i < R; i++, getchar())
      for (int j = 0; j < C; j++)
        mat[i][j] = getchar();
    vector<vector<int>> step(R, vector<int>(C, INF));
    queue<DATA> Q;
    Q.push(DATA(st.x, st.y));
    step[st.x][st.y] = 0;
    while (Q.empty() == 0) {
      now = Q.front();
      Q.pop();
      if (now.x == ed.x and now.y == ed.y)
        break;
      for (int i = 0; i < 4; i++)
        for (int k = 1; k <= K; k++) {
          nxt = DATA(now.x + k * dx[i], now.y + k * dy[i]);
          if (nxt.x == -1 or nxt.x == R or nxt.y == -1 or nxt.y == C or
              mat[nxt.x][nxt.y] == '@' or
              step[nxt.x][nxt.y] <= step[now.x][now.y])
            break;
          step[nxt.x][nxt.y] = step[now.x][now.y] + 1;
          Q.push(nxt);
        }
    }
    printf("%d\\n", (step[ed.x][ed.y] == INF) ? -1 : step[ed.x][ed.y]);
  }
}
Problem p02644 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
#define pb push_back
#define endl '\\n'
#define snd second
#define fst first
#define fastio cin.tie(NULL), cout.sync_with_stdio(true)

typedef long long int ll;
typedef unsigned long long int ull;
typedef vector<int> vi;
typedef pair<ll, int> ii;
typedef pair<ii, int> iii;

const int mod = 1e9 + 7;
const int N = 100005;

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

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

void solve() {
  scanf("%d%d%d", &n, &m, &k);
  queue<ii> q;
  char v[n + 1][m + 1];
  int see[n + 1][m + 1], x1, y1, x2, y2;
  scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
  for (int i = 1; i <= n; i++) {
    getchar();
    for (int j = 1; j <= m; j++) {
      scanf("%c", &v[i][j]);
      see[i][j] = mod;
    }
  }
  q.push({x1, y1});
  see[x1][y1] = 0;
  while (!q.empty()) {
    int i = q.front().fst, j = q.front().snd;
    q.pop();
    for (int t = 0; t < 4; t++) {
      for (int l = 1; l <= k; l++) {
        int ni = i + dx[t] * l, nj = j + dy[t] * l;
        if (!valid(ni, nj) or v[ni][nj] == '@' or see[ni][nj] < see[i][j] + 1)
          break;
        if (see[ni][nj] > see[i][j] + 1) {
          see[ni][nj] = see[i][j] + 1;
          q.push({ni, nj});
        }
      }
    }
  }
  if (see[x2][y2] == mod)
    printf("-1\\n");
  else
    printf("%d\\n", see[x2][y2]);
}

int main() {
  // fastio;
  int t = 1;
  // cin >> t;
  while (t--)
    solve();
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
#define pb push_back
#define endl '\\n'
#define snd second
#define fst first
#define fastio cin.tie(NULL), cout.sync_with_stdio(true)

typedef long long int ll;
typedef unsigned long long int ull;
typedef vector<int> vi;
typedef pair<ll, int> ii;
typedef pair<ii, int> iii;

const int mod = 1e9 + 7;
const int N = 100005;

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

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

void solve() {
  scanf("%d%d%d", &n, &m, &k);
  queue<ii> q;
  char v[n + 1][m + 1];
  int see[n + 1][m + 1], x1, y1, x2, y2;
  scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
  for (int i = 1; i <= n; i++) {
    getchar();
    for (int j = 1; j <= m; j++) {
      scanf("%c", &v[i][j]);
      see[i][j] = mod;
    }
  }
  q.push({x1, y1});
  see[x1][y1] = 0;
  while (!q.empty()) {
    int i = q.front().fst, j = q.front().snd;
    q.pop();
    for (int t = 0; t < 4; t++) {
      for (int l = 1; l <= k; l++) {
        int ni = i + dx[t] * l, nj = j + dy[t] * l;
        if (!valid(ni, nj) or v[ni][nj] == '@' or see[ni][nj] < see[i][j] + 1)
          break;
        see[ni][nj] = see[i][j] + 1;
        q.push({ni, nj});
      }
    }
  }
  if (see[x2][y2] == mod)
    printf("-1\\n");
  else
    printf("%d\\n", see[x2][y2]);
}

int main() {
  // fastio;
  int t = 1;
  // cin >> t;
  while (t--)
    solve();
  return 0;
}