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

#include <bits/stdc++.h>
using namespace std;
typedef signed long long lint;

mt19937 mt(time(nullptr));
// uniform_int_distribution< int > rand_int(0, 10) // rand[0, 10] inclusive

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed;
  int h, w, k, xs, ys, xg, yg;
  ;
  cin >> h >> w >> k >> xs >> ys >> xg >> yg;
  xs--;
  ys--;
  xg--;
  yg--;
  vector<string> m(h);
  for (int i = 0; i < h; ++i)
    cin >> m[i];
  vector<tuple<int, int, int>> q(h * w * 2, make_tuple(xs, ys, 0));
  q[0] = make_tuple(xs, ys, 0);
  vector<vector<int>> v(h, vector<int>(w, -1));
  v[xs][ys] = 0;
  vector<int> dx = {1, 0, -1, 0};
  vector<int> dy = {0, 1, 0, -1};
  int vi = 1;
  for (int i = 0; i < h * w; ++i) {
    int x = get<0>(q[i]), y = get<1>(q[i]), c = get<2>(q[i]);
    if (x == xg && y == yg) {
      cout << c << endl;
      return 0;
    }
    for (int dr = 0; dr < 4; ++dr) {
      for (int d = 1; d <= k; ++d) {
        int xn = x + dx[dr] * d;
        int yn = y + dy[dr] * d;
        if (xn < 0 || h <= xn)
          break;
        if (yn < 0 || w <= yn)
          break;
        if (m[xn][yn] == '@')
          break;
        if (v[xn][yn] >= 0) {
          if (v[xn][yn] < c + 1)
            break;
          continue;
        }
        v[xn][yn] = c + 1;
        q[vi] = make_tuple(xn, yn, c + 1);
        vi++;
        // cout << xn << "/" << yn << " = " << c+1 << endl;
      }
    }
  }
  cout << -1 << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef signed long long lint;

mt19937 mt(time(nullptr));
// uniform_int_distribution< int > rand_int(0, 10) // rand[0, 10] inclusive

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed;
  int h, w, k, xs, ys, xg, yg;
  ;
  cin >> h >> w >> k >> xs >> ys >> xg >> yg;
  xs--;
  ys--;
  xg--;
  yg--;
  vector<string> m(h);
  for (int i = 0; i < h; ++i)
    cin >> m[i];
  vector<tuple<int, int, int>> q(h * w * 2, make_tuple(xs, ys, 0));
  q[0] = make_tuple(xs, ys, 0);
  vector<vector<bool>> v(h, vector<bool>(w, false));
  v[xs][ys] = true;
  vector<int> dx = {1, 0, -1, 0};
  vector<int> dy = {0, 1, 0, -1};
  int vi = 1;
  for (int i = 0; i < h * w; ++i) {
    int x = get<0>(q[i]), y = get<1>(q[i]), c = get<2>(q[i]);
    if (x == xg && y == yg) {
      cout << c << endl;
      return 0;
    }
    for (int dr = 0; dr < 4; ++dr) {
      for (int d = 1; d <= k; ++d) {
        int xn = x + dx[dr] * d;
        int yn = y + dy[dr] * d;
        if (xn < 0 || h <= xn)
          break;
        if (yn < 0 || w <= yn)
          break;
        if (m[xn][yn] == '@')
          break;
        if (v[xn][yn])
          continue;
        v[xn][yn] = true;
        q[vi] = make_tuple(xn, yn, c + 1);
        vi++;
        // cout << xn << "/" << yn << " = " << c+1 << endl;
      }
    }
  }
  cout << -1 << endl;
  return 0;
}
Problem p02644 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using ll = long long;
using ull = unsigned long long;
using namespace std;
constexpr int MAX = 1000000;
constexpr int inf = 1 << 30;

int h, w, k;
int main() {
  cin.tie(nullptr);
  std::ios::sync_with_stdio(false);

  cin >> h >> w >> k;
  vector<string> m(h);
  int sx, sy, gx, gy;
  cin >> sy >> sx >> gy >> gx;
  sx--;
  sy--;
  gx--;
  gy--;
  for (auto &a : m)
    cin >> a;
  vector<vector<int>> f(h + 5, vector<int>(w + 5, inf));
  priority_queue<tuple<int, int, int>> q;
  q.push({0, sy, sx});
  f[sy][sx] = 0;
  while (!q.empty()) {
    auto cur = q.top();
    q.pop();
    int y = get<1>(cur);
    int x = get<2>(cur);
    int cost = -get<0>(cur);
    for (auto d :
         initializer_list<pair<int, int>>{{-1, 0}, {0, 1}, {1, 0}, {0, -1}}) {
      for (int i = 1; i <= k; ++i) {
        int dy = y + d.first * i;
        int dx = x + d.second * i;
        if (dy < 0 or dy > h - 1 or dx < 0 or dx > w - 1 or m[dy][dx] == '@')
          break;
        if (f[dy][dx] > cost + 1) {
          f[dy][dx] = cost + 1;
          q.push({-f[dy][dx], dy, dx});
        } else if (f[dy][dx] < cost + 1)
          break;
      }
    }
  }

  if (f[gy][gx] == inf)
    cout << -1 << endl;
  else
    cout << f[gy][gx] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using ll = long long;
using ull = unsigned long long;
using namespace std;
constexpr int MAX = 1000000;
constexpr int inf = 1 << 30;

int h, w, k;
int main() {
  cin.tie(nullptr);
  std::ios::sync_with_stdio(false);

  cin >> h >> w >> k;
  vector<string> m(h);
  int sx, sy, gx, gy;
  cin >> sy >> sx >> gy >> gx;
  sx--;
  sy--;
  gx--;
  gy--;
  for (auto &a : m)
    cin >> a;
  vector<vector<int>> f(h + 5, vector<int>(w + 5, inf));
  priority_queue<tuple<int, int, int>> q;
  q.push({0, sy, sx});
  f[sy][sx] = 0;
  while (!q.empty()) {
    auto cur = q.top();
    q.pop();
    int y = get<1>(cur);
    int x = get<2>(cur);
    int cost = -get<0>(cur);
    for (auto d :
         initializer_list<pair<int, int>>{{-1, 0}, {0, 1}, {1, 0}, {0, -1}}) {
      for (int i = 1; i <= k; ++i) {
        int dy = y + d.first * i;
        int dx = x + d.second * i;
        if (dy < 0 or dy > h - 1 or dx < 0 or dx > w - 1 or m[dy][dx] == '@')
          break;
        if (f[dy][dx] > cost + 1) {
          f[dy][dx] = cost + 1;
          q.push({-f[dy][dx], dy, dx});
        }
      }
    }
  }

  if (f[gy][gx] == inf)
    cout << -1 << endl;
  else
    cout << f[gy][gx] << endl;
  return 0;
}
Problem p02644 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
const int dx[4] = {-1, 1, 0, 0}, dy[4] = {0, 0, -1, 1};
int n, m, k, x1, x2, h1, y2;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m >> k >> x1 >> h1 >> x2 >> y2;
  x1--;
  h1--;
  x2--;
  y2--;
  char grid[n + 10][m + 10];
  int dist[n + 10][m + 10];
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      cin >> grid[i][j];
  queue<pair<int, int>> q;
  q.push(make_pair(x1, h1));
  memset(dist, 0x3f, sizeof(dist));
  dist[x1][h1] = 0;
  while (!q.empty()) {
    int nx = q.front().first;
    int ny = q.front().second;
    if (nx == x2 && ny == y2) {
      cout << dist[nx][ny] << endl;
      return 0;
    }
    q.pop();
    for (int i = 0; i < 4; i++) {
      for (int j = 1; j <= k; j++) {
        int tx = nx + dx[i] * j;
        int ty = ny + dy[i] * j;
        if (tx >= 0 && tx < n && ty >= 0 && ty < m) {
          if (grid[tx][ty] == '@')
            break;
          if (dist[tx][ty] > 1e7) {
            dist[tx][ty] = dist[nx][ny] + 1;
            q.push(make_pair(tx, ty));
          }
          if (dist[tx][ty] <= dist[nx][ny])
            break;
        } else
          break;
      }
    }
  }
  cout << -1 << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int dx[4] = {-1, 1, 0, 0}, dy[4] = {0, 0, -1, 1};
int n, m, k, x1, x2, h1, y2;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m >> k >> x1 >> h1 >> x2 >> y2;
  x1--;
  h1--;
  x2--;
  y2--;
  char grid[n + 10][m + 10];
  int dist[n + 10][m + 10];
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      cin >> grid[i][j];
  queue<pair<int, int>> q;
  q.push(make_pair(x1, h1));
  memset(dist, 0x3f, sizeof(dist));
  dist[x1][h1] = 0;
  while (!q.empty()) {
    int nx = q.front().first;
    int ny = q.front().second;
    if (nx == x2 && ny == y2) {
      cout << dist[nx][ny] << endl;
      return 0;
    }
    q.pop();
    for (int i = 0; i < 4; i++) {
      for (int j = 1; j <= k; j++) {
        int tx = nx + dx[i] * j;
        int ty = ny + dy[i] * j;
        if (tx >= 0 && tx < n && ty >= 0 && ty < m) {
          if (grid[tx][ty] == '@')
            break;
          if (dist[tx][ty] > 1e7) {
            dist[tx][ty] = dist[nx][ny] + 1;
            q.push(make_pair(tx, ty));
          }
        } else
          break;
      }
    }
  }
  cout << -1 << endl;
  return 0;
}
Problem p02644 (C++) — buggy submission, verdict: Time Limit Exceeded

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

typedef long long ll;

ll H, W, K;
ll SX, SY, TX, TY;
vector<vector<bool>> L;
queue<pair<ll, ll>> Q;
vector<vector<ll>> D;
const ll INF = 1ll << 60;
const ll DX[] = {0, 1, 0, -1};
const ll DY[] = {1, 0, -1, 0};

int main() {
  cin >> H >> W >> K;
  cin >> SX >> SY >> TX >> TY;
  L.resize(H + 2);
  for (auto &Lx : L)
    Lx.resize(W + 2, true);
  for (ll x = 1; x <= H; ++x) {
    for (ll y = 1; y <= W; ++y) {
      char c;
      cin >> c;
      if (c == '.')
        L[x][y] = false;
    }
  }
  D.resize(H + 2);
  for (auto &Dx : D)
    Dx.resize(W + 2, INF);
  Q.push({SX, SY});
  D[SX][SY] = 0;
  while (!Q.empty()) {
    ll x, y;
    tie(x, y) = Q.front();
    Q.pop();
    ll d = D[x][y];
    for (ll i = 0; i < 4; ++i) {
      ll dx = DX[i], dy = DY[i];
      for (ll j = 1; j <= K; ++j) {
        ll x2 = x + j * dx;
        ll y2 = y + j * dy;
        if (L[x2][y2])
          break;
        if (D[x2][y2] <= d)
          break;
        if (D[x2][y2] == d + 1)
          continue;
        Q.push({x2, y2});
        D[x2][y2] = d + 1;
      }
    }
  }
  ll res = D[TX][TY];
  cout << (res == INF ? -1 : res) << endl;
  return 0;
}


Error: TLE

Fixed submission:

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

typedef long long ll;

ll H, W, K;
ll SX, SY, TX, TY;
vector<vector<bool>> L;
queue<pair<ll, ll>> Q;
vector<vector<ll>> D;
const ll INF = 1ll << 60;
const ll DX[] = {0, 1, 0, -1};
const ll DY[] = {1, 0, -1, 0};

int main() {
  cin >> H >> W >> K;
  cin >> SX >> SY >> TX >> TY;
  L.resize(H + 2);
  for (auto &Lx : L)
    Lx.resize(W + 2, true);
  for (ll x = 1; x <= H; ++x) {
    for (ll y = 1; y <= W; ++y) {
      char c;
      cin >> c;
      if (c == '.')
        L[x][y] = false;
    }
  }
  D.resize(H + 2);
  for (auto &Dx : D) {
    Dx.resize(W + 2, INF);
  }
  Q.push({SX, SY});
  D[SX][SY] = 0;
  while (!Q.empty()) {
    ll x, y;
    tie(x, y) = Q.front();
    Q.pop();
    ll d = D[x][y];
    for (ll i = 0; i < 4; ++i) {
      ll dx = DX[i], dy = DY[i];
      for (ll j = 1; j <= K; ++j) {
        ll x2 = x + j * dx;
        ll y2 = y + j * dy;
        if (L[x2][y2])
          break;
        if (D[x2][y2] <= d)
          break;
        Q.push({x2, y2});
        D[x2][y2] = d + 1;
      }
    }
  }
  ll res = D[TX][TY];
  cout << (res == INF ? -1 : res) << endl;
  return 0;
}
Problem p02644 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

const int INF = 1 << 30;
const 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 >> 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];
  }
  vector<vector<int>> D(H, vector<int>(W, INF));
  D[sy][sx] = 0;
  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)));
  while (!que.empty()) {
    auto q = que.top();
    que.pop();
    int d = q.first, y = q.second.first, x = q.second.second;
    if (d > D[y][x])
      continue;
    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 (D[ny][nx] < D[y][x] + 1)
          break;
        if (chmin(D[ny][nx], d + 1)) {
          que.push(make_pair(D[ny][nx], make_pair(ny, nx)));
        }
      }
    }
  }
  if (D[gy][gx] == INF) {
    cout << -1 << '\\n';
  } else {
    cout << D[gy][gx] << '\\n';
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

const int INF = 1 << 30;
const 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 >> 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];
  }
  vector<vector<int>> D(H, vector<int>(W, INF));
  D[sy][sx] = 0;
  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)));
  while (!que.empty()) {
    auto q = que.top();
    que.pop();
    int d = q.first, y = q.second.first, x = q.second.second;
    if (d > D[y][x])
      continue;
    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(D[ny][nx], d + 1)) {
          que.push(make_pair(D[ny][nx], make_pair(ny, nx)));
        }
      }
    }
  }
  if (D[gy][gx] == INF) {
    cout << -1 << '\\n';
  } else {
    cout << D[gy][gx] << '\\n';
  }
  return 0;
}
Problem p02644 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"
using namespace std;
#define int long long
typedef long long ll;
#define rep(i, n) for (int i = 0; i < n; i++)
typedef pair<int, int> P;
typedef pair<int, P> PP;
constexpr long long inf = 1e17;
constexpr int mod = 1000000007;
vector<string> c;
vector<vector<int>> dist;
int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
signed main() {
  int h, w, k;
  cin >> h >> w >> k;
  int xa, ya, xb, yb;
  cin >> xa >> ya >> xb >> yb;
  xa--;
  xb--;
  ya--;
  yb--;
  c.resize(h);
  rep(i, h) cin >> c[i];
  dist.resize(h);
  rep(i, h) {
    dist[i].resize(w);
    rep(j, w) dist[i][j] = inf;
  }

  queue<PP> Q;
  Q.push(PP(0, P(xa, ya)));
  dist[xa][ya] = 0;
  while (Q.size()) {
    PP p = Q.front();
    Q.pop();
    int vi = p.second.first, vj = p.second.second;
    if (dist[vi][vj] < p.first)
      continue;
    rep(i, 4) {
      for (int j = 1; j <= k; j++) {
        int ni = vi + dx[i] * j, nj = vj + dy[i] * j;
        if (0 <= ni && ni < h && 0 <= nj && nj < w && c[ni][nj] == '.' &&
            dist[ni][nj] > dist[vi][vj]) {
          if (dist[ni][nj] > dist[vi][vj] + 1) {
            dist[ni][nj] = dist[vi][vj] + 1;
            Q.push(PP(dist[ni][nj], P(ni, nj)));
          }
        } else
          break;
      }
    }
  }
  if (dist[xb][yb] == inf)
    dist[xb][yb] = -1;
  cout << dist[xb][yb] << endl;
}


Error: TLE

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
// #define int long long
typedef long long ll;
#define rep(i, n) for (int i = 0; i < n; i++)
typedef pair<int, int> P;
typedef pair<int, P> PP;
constexpr int inf = 1e9;
constexpr int mod = 1000000007;
vector<string> c;
vector<vector<int>> dist;
int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
signed main() {
  int h, w, k;
  cin >> h >> w >> k;
  int xa, ya, xb, yb;
  cin >> xa >> ya >> xb >> yb;
  xa--;
  xb--;
  ya--;
  yb--;
  c.resize(h);
  rep(i, h) cin >> c[i];
  dist.resize(h);
  rep(i, h) {
    dist[i].resize(w);
    rep(j, w) dist[i][j] = inf;
  }

  queue<PP> Q;
  Q.push(PP(0, P(xa, ya)));
  dist[xa][ya] = 0;
  while (Q.size()) {
    PP p = Q.front();
    Q.pop();
    int vi = p.second.first, vj = p.second.second;
    if (dist[vi][vj] < p.first)
      continue;
    rep(i, 4) {
      for (int j = 1; j <= k; j++) {
        int ni = vi + dx[i] * j, nj = vj + dy[i] * j;
        if (0 <= ni && ni < h && 0 <= nj && nj < w && c[ni][nj] == '.') {
          if (dist[ni][nj] > dist[vi][vj] + 1) {
            dist[ni][nj] = dist[vi][vj] + 1;
            Q.push(PP(dist[ni][nj], P(ni, nj)));
          }
        } else
          break;
      }
    }
  }
  if (dist[xb][yb] == inf)
    dist[xb][yb] = -1;
  cout << dist[xb][yb] << endl;
}
Problem p02644 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int H, W, K;
int sx, sy, ex, ey;
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};

int main(void) {
  cin >> H >> W >> K;
  vector<vector<char>> c(H, vector<char>(W));
  vector<vector<int>> dp(H, vector<int>(W));
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      dp[i][j] = INT_MAX;
    }
  }
  cin >> sx >> sy >> ex >> ey;
  sx--;
  sy--;
  ex--;
  ey--;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> c[i][j];
    }
  }
  queue<pair<int, int>> q;
  q.push({sx, sy});
  dp[sx][sy] = 0;
  while (!q.empty()) {
    pair<int, int> pi = q.front();
    q.pop();
    for (int i = 0; i < 4; i++) {
      for (int j = 1; j <= K; j++) {
        int nx = pi.first + j * dx[i];
        int ny = pi.second + j * dy[i];
        if (nx < 0 || nx >= H || ny < 0 || ny >= W)
          continue;
        if (c[nx][ny] == '@' || dp[nx][ny] <= dp[pi.first][pi.second])
          break;
        if (dp[nx][ny] > dp[pi.first][pi.second] + 1) {
          q.push({nx, ny});
          dp[nx][ny] = dp[pi.first][pi.second] + 1;
        }
      }
    }
  }
  if (dp[ex][ey] == INT_MAX)
    cout << -1 << endl;
  else
    cout << dp[ex][ey] << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int H, W, K;
int sx, sy, ex, ey;
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};

int main(void) {
  cin >> H >> W >> K;
  vector<vector<char>> c(H, vector<char>(W));
  vector<vector<int>> dp(H, vector<int>(W));
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      dp[i][j] = INT_MAX;
    }
  }
  cin >> sx >> sy >> ex >> ey;
  sx--;
  sy--;
  ex--;
  ey--;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> c[i][j];
    }
  }
  queue<pair<int, int>> q;
  q.push({sx, sy});
  dp[sx][sy] = 0;
  while (!q.empty()) {
    pair<int, int> pi = q.front();
    q.pop();
    for (int i = 0; i < 4; i++) {
      for (int j = 1; j <= K; j++) {
        int nx = pi.first + j * dx[i];
        int ny = pi.second + j * dy[i];
        if (nx < 0 || nx >= H || ny < 0 || ny >= W)
          continue;
        if (c[nx][ny] == '@')
          break;
        if (dp[nx][ny] > dp[pi.first][pi.second] + 1) {
          q.push({nx, ny});
          dp[nx][ny] = dp[pi.first][pi.second] + 1;
        }
      }
    }
  }
  if (dp[ex][ey] == INT_MAX)
    cout << -1 << endl;
  else
    cout << dp[ex][ey] << endl;

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

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

const int N = 1e6 + 5;
typedef long long LL;

int h, w, k, a[N], f[N], x, y, xx, yy;
int dx[5] = {0, 1, 0, 0, -1};
int dy[5] = {0, 0, 1, -1, 0};
struct node {
  int r, c;
} q[N << 2];

int p(int x, int y) { return (x - 1) * w + y; }

int Bfs() {
  memset(f, 0x7f, sizeof(f));
  f[p(x, y)] = 0;
  int l = 1, r = 1;
  q[l] = (node){x, y};
  while (l <= r) {
    int x = q[l].r, y = q[l].c;
    if (x == xx && y == yy)
      return f[p(x, y)];
    for (int i = 1; i <= 4; i++) {
      int nx = x, ny = y;
      for (int j = 1; j <= k; j++) {
        nx += dx[i], ny += dy[i];
        if (nx > h || nx < 1 || ny < 1 || ny > w)
          break;
        if (a[p(nx, ny)])
          break;
        if (f[p(nx, ny)] <= f[p(x, y)])
          break;
        if (f[p(nx, ny)] > f[p(x, y)] + 1) {
          f[p(nx, ny)] = f[p(x, y)] + 1;
          q[++r] = (node){nx, ny};
        }
      }
    }
    l++;
  }
  return -1;
}

int main() {
  cin >> h >> w >> k >> x >> y >> xx >> yy;
  for (int i = 1; i <= h; i++)
    for (int j = 1; j <= w; j++) {
      char ch;
      cin >> ch;
      if (ch != '.')
        a[p(i, j)] = 1;
    }
  cout << Bfs() << endl;
  return 0;
}

Error: WA

Fixed submission:

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

const int N = 1e6 + 5;
typedef long long LL;

int n, m, k, a[N], x, y, X, Y;
int dx[5] = {0, 1, 0, 0, -1};
int dy[5] = {0, 0, 1, -1, 0};
char ch;
int f[N], l, r;
struct node {
  int x, y, s;
} q[N << 1];

int co(int x, int y) { return (x - 1) * m + y; }

void Bfs() {
  q[l = r = 1] = (node){x, y, 0};
  while (l <= r) {
    if (q[l].x == X && q[l].y == Y)
      break;
    for (int i = 1; i <= 4; i++) {
      for (int j = 1; j <= k; j++) {
        int nx = q[l].x + j * dx[i], ny = q[l].y + j * dy[i];
        if (nx > n || nx < 1 || ny < 1 || ny > m)
          break;
        if (a[co(nx, ny)])
          break;
        if (f[co(nx, ny)] <= f[co(x, y)])
          break;
        f[co(nx, ny)] = q[l].s + 1;
        q[++r] = (node){nx, ny, q[l].s + 1};
      }
    }
    l++;
  }
}

int main() {
  cin >> n >> m >> k >> x >> y >> X >> Y;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      cin >> ch;
      if (ch == '@')
        a[co(i, j)] = 1;
    }
  memset(f, 0x7f, sizeof(f));
  f[co(x, y)] = 0;
  Bfs();
  if (f[co(X, Y)] == 0x7f7f7f7f)
    cout << -1 << endl;
  else
    cout << f[co(X, Y)] << endl;
}
Problem p02644 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
#define DUMP(x) cout << #x << " = " << (x) << endl;
#define FOR(i, m, n) for (ll i = m; i < n; i++)
#define IFOR(i, m, n) for (ll i = n - 1; i >= m; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)
#define FOREACH(x, a) for (auto &(x) : (a))
#define ALL(v) (v).begin(), (v).end()
#define SZ(x) ll(x.size())

int main() {
  ll h, w, k;
  cin >> h >> w >> k;
  ll x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--, x2--, y1--, y2--;
  vector<string> s(h);
  REP(i, h) { cin >> s[i]; }
  vector<vector<ll>> dist(h, vector<ll>(w, -1));
  queue<P> q;
  q.push({x1, y1});
  dist[x1][y1] = 0;
  vector<ll> d = {0, 1, 0, -1};

  while (!q.empty()) {
    P p = q.front();
    q.pop();
    ll cx = p.first, cy = p.second;

    REP(i, 4) {
      FOR(j, 1, k + 1) {
        ll nx = cx + j * d[i], ny = cy + j * d[i ^ 1];
        if (nx < 0 || nx >= h || ny < 0 || ny >= w) {
          break;
        }
        if (s[nx][ny] == '@') {
          break;
        }
        if (dist[nx][ny] != -1 && dist[nx][ny] <= dist[cx][cy]) {
          break;
        }

        if (dist[nx][ny] == -1) {
          dist[nx][ny] = dist[cx][cy] + 1;
          q.push({nx, ny});
        }
      }
    }
  }
  cout << dist[x2][y2] << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
#define DUMP(x) cout << #x << " = " << (x) << endl;
#define FOR(i, m, n) for (ll i = m; i < n; i++)
#define IFOR(i, m, n) for (ll i = n - 1; i >= m; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)
#define FOREACH(x, a) for (auto &(x) : (a))
#define ALL(v) (v).begin(), (v).end()
#define SZ(x) ll(x.size())

int main() {
  ll h, w, k;
  cin >> h >> w >> k;
  ll x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--, x2--, y1--, y2--;
  vector<string> s(h);
  REP(i, h) { cin >> s[i]; }
  vector<vector<ll>> dist(h, vector<ll>(w, -1));
  queue<P> q;
  q.push({x1, y1});
  dist[x1][y1] = 0;
  vector<ll> d = {0, 1, 0, -1};

  while (!q.empty()) {
    P p = q.front();
    q.pop();
    ll cx = p.first, cy = p.second;

    REP(i, 4) {
      FOR(j, 1, k + 1) {
        ll nx = cx + j * d[i], ny = cy + j * d[i ^ 1];
        if (nx < 0 || nx >= h || ny < 0 || ny >= w) {
          break;
        }
        if (s[nx][ny] == '@') {
          break;
        }
        if (dist[nx][ny] != -1 && dist[nx][ny] <= dist[cx][cy]) {
          break;
        }
        dist[nx][ny] = dist[cx][cy] + 1;
        q.push({nx, ny});
      }
    }
  }
  cout << dist[x2][y2] << endl;
}