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

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

// template
#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define ALL(v) (v).begin(), (v).end()
typedef long long int ll;
const int inf = 0x3fffffff;
const ll INF = 0x1fffffffffffffff;
const double eps = 1e-12;
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
// end

typedef pair<int, int> P;
typedef pair<int, P> T;

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

int main() {
  int h, w, k;
  cin >> h >> w >> k;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  y1--;
  x2--;
  y2--;
  vector<vector<char>> g(h, vector<char>(w));
  rep(i, 0, h) rep(j, 0, w) cin >> g[i][j];
  vector<vector<int>> dist(h, vector<int>(w, inf));
  dist[x1][y1] = 0;
  queue<P> que;
  que.push(P(x1, y1));
  while (!que.empty()) {
    auto [nx, ny] = que.front();
    que.pop();
    rep(i, 0, 4) rep(add, 1, k + 1) {
      int tx = nx + dx[i] * add, ty = ny + dy[i] * add;
      if (tx < 0 or tx >= h or ty < 0 or ty >= w or g[tx][ty] == '@')
        break;
      if (dist[tx][ty] <= dist[nx][ny])
        break;
      if (dist[tx][ty] == inf) {
        dist[tx][ty] = dist[nx][ny] + 1;
        que.push(P(tx, ty));
      }
    }
  }
  if (dist[x2][y2] == inf)
    puts("-1");
  else
    cout << dist[x2][y2] << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

// template
#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define ALL(v) (v).begin(), (v).end()
typedef long long int ll;
const int inf = 0x3fffffff;
const ll INF = 0x1fffffffffffffff;
const double eps = 1e-12;
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
// end

typedef pair<int, int> P;
typedef pair<int, P> T;

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

int main() {
  int h, w, k;
  cin >> h >> w >> k;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  y1--;
  x2--;
  y2--;
  vector<vector<char>> g(h, vector<char>(w));
  rep(i, 0, h) rep(j, 0, w) cin >> g[i][j];
  vector<vector<int>> dist(h, vector<int>(w, inf));
  dist[x1][y1] = 0;
  queue<P> que;
  que.push(P(x1, y1));
  while (!que.empty()) {
    auto [nx, ny] = que.front();
    que.pop();
    rep(i, 0, 4) rep(add, 1, k + 1) {
      int tx = nx + dx[i] * add, ty = ny + dy[i] * add;
      if (tx < 0 or tx >= h or ty < 0 or ty >= w or g[tx][ty] == '@')
        break;
      if (dist[tx][ty] == inf) {
        dist[tx][ty] = dist[nx][ny] + 1;
        que.push(P(tx, ty));
      }
    }
  }
  if (dist[x2][y2] == inf)
    puts("-1");
  else
    cout << dist[x2][y2] << endl;
  return 0;
}
Problem p02644 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> P;
typedef vector<ll> VI;
typedef vector<VI> VVI;
const ll MOD = 1000000007;
const ll INF = 4611686018427387903;
#define REP(i, n) for (int i = 0; i < n; i++)
#define ALL(v) v.begin(), v.end()

int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
struct xy {
  int x;
  int y;
};
struct xyd {
  int x;
  int y;
  ll d;
};

int main() {
  int h, w, k;
  cin >> h >> w >> k;
  xy s, g;
  cin >> s.x >> s.y >> g.x >> g.y;
  s.x--;
  s.y--;
  g.x--;
  g.y--;
  vector<vector<bool>> f(h, vector<bool>(w, 0));
  char c;
  REP(i, h) REP(j, w) {
    cin >> c;
    if (c == '@')
      f[i][j] = 1;
  }
  VVI d(h, VI(w, INF));
  d[s.x][s.y] = 0;
  queue<xy> q;
  q.push({s.x, s.y});
  while (!q.empty()) {
    int x = q.front().x, y = q.front().y;
    q.pop();
    REP(i, 4) {
      for (int j = 1; j <= k; j++) {
        int tox = x + dx[i] * j, toy = y + dy[i] * j;
        if (tox < 0 || tox >= h || toy < 0 || toy >= w)
          break;
        if (f[tox][toy])
          break;
        if (d[tox][toy] < d[x][y] + 1)
          break;
        else if (d[tox][toy] == d[x][y] + 1)
          continue;
        else {
          d[tox][toy] = d[x][y] + 1;
          q.push({tox, toy});
        }
      }
    }
  }
  if (d[g.x][g.y] == INF)
    cout << -1 << endl;
  else
    cout << d[g.x][g.y] << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> P;
typedef vector<ll> VI;
typedef vector<VI> VVI;
const ll MOD = 1000000007;
const ll INF = 4611686018427387903;
#define REP(i, n) for (int i = 0; i < n; i++)
#define ALL(v) v.begin(), v.end()

int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
struct xy {
  int x;
  int y;
};
struct xyd {
  int x;
  int y;
  ll d;
};

int main() {
  int h, w, k;
  cin >> h >> w >> k;
  xy s, g;
  cin >> s.x >> s.y >> g.x >> g.y;
  s.x--;
  s.y--;
  g.x--;
  g.y--;
  vector<vector<bool>> f(h, vector<bool>(w, 0));
  char c;
  REP(i, h) REP(j, w) {
    cin >> c;
    if (c == '@')
      f[i][j] = 1;
  }
  VVI d(h, VI(w, INF));
  d[s.x][s.y] = 0;
  queue<xy> q;
  q.push({s.x, s.y});
  while (!q.empty()) {
    int x = q.front().x, y = q.front().y;
    q.pop();
    REP(i, 4) {
      for (int j = 1; j <= k; j++) {
        int tox = x + dx[i] * j, toy = y + dy[i] * j;
        if (tox < 0 || tox >= h || toy < 0 || toy >= w)
          break;
        if (f[tox][toy])
          break;
        if (d[tox][toy] < d[x][y])
          break;
        else if (d[tox][toy] == d[x][y])
          continue;
        else {
          d[tox][toy] = d[x][y] + 1;
          q.push({tox, toy});
        }
      }
    }
  }
  if (d[g.x][g.y] == INF)
    cout << -1 << endl;
  else
    cout << d[g.x][g.y] << endl;
}
Problem p02644 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
ll ans = 0;
ll h, w, k;
bool inarea(ll x, ll y) { return x >= 0 && y >= 0 && x < h && y < w; }
ll dx[4] = {0, 1, 0, -1};
ll dy[4] = {1, 0, -1, 0};
int main() {
  cin >> h >> w >> k;
  ll xs, xg, ys, yg;
  cin >> xs >> ys >> xg >> yg;
  xs--;
  xg--;
  ys--;
  yg--;
  vector<vector<ll>> v(h, vector<ll>(w, 0));
  vector<vector<ll>> d(h, vector<ll>(w, 10000000));
  rep(i, h) rep(j, w) {
    char a;
    cin >> a;
    if (a == '@')
      v[i][j] = 1;
  }
  queue<pair<ll, ll>> q;
  q.push({xs, ys});
  d[xs][ys] = 0;
  while (!q.empty()) {
    auto p = q.front();
    q.pop();
    rep(l, 4) {
      rep(i, k) {
        if (!inarea(p.first + dx[l] * (i + 1), p.second + dy[l] * (i + 1)))
          break;
        if (v[p.first + dx[l] * (i + 1)][p.second + dy[l] * (i + 1)])
          break;
        if (d[p.first + dx[l] * (i + 1)][p.second + dy[l] * (i + 1)] <=
            d[p.first][p.second])
          break;
        if (d[p.first + dx[l] * (i + 1)][p.second + dy[l] * (i + 1)] >
            d[p.first][p.second] + 1) {
          d[p.first + dx[l] * (i + 1)][p.second + dy[l] * (i + 1)] =
              d[p.first][p.second] + 1;
          q.push({p.first + dx[l] * (i + 1), p.second + dy[l] * (i + 1)});
        }
      }
    }
  }
  cout << (d[xg][yg] == 10000000 ? -1 : d[xg][yg]) << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
ll ans = 0;
ll h, w, k;
bool inarea(ll x, ll y) { return x >= 0 && y >= 0 && x < h && y < w; }
ll dx[4] = {0, 1, 0, -1};
ll dy[4] = {1, 0, -1, 0};
int main() {
  cin >> h >> w >> k;
  ll xs, xg, ys, yg;
  cin >> xs >> ys >> xg >> yg;
  xs--;
  xg--;
  ys--;
  yg--;
  vector<vector<ll>> v(h, vector<ll>(w, 0));
  vector<vector<ll>> d(h, vector<ll>(w, 10000000));
  rep(i, h) rep(j, w) {
    char a;
    cin >> a;
    if (a == '@')
      v[i][j] = 1;
  }
  queue<pair<ll, ll>> q;
  q.push({xs, ys});
  d[xs][ys] = 0;
  while (!q.empty()) {
    auto p = q.front();
    q.pop();
    rep(l, 4) {
      rep(i, k) {
        if (inarea(p.first + dx[l] * (i + 1), p.second + dy[l] * (i + 1)) &&
            v[p.first + dx[l] * (i + 1)][p.second + dy[l] * (i + 1)] == 0 &&
            d[p.first + dx[l] * (i + 1)][p.second + dy[l] * (i + 1)] >=
                d[p.first][p.second] + 1) {
          d[p.first + dx[l] * (i + 1)][p.second + dy[l] * (i + 1)] =
              d[p.first][p.second] + 1;
          q.push({p.first + dx[l] * (i + 1), p.second + dy[l] * (i + 1)});
        } else
          break;
      }
    }
  }
  cout << (d[xg][yg] == 10000000 ? -1 : d[xg][yg]) << endl;
  return 0;
}
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++)

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

int main() {
  int h, w, k;
  cin >> h >> w >> k;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--, y1--, x2--, y2--;
  vector<string> field(h);
  rep(i, h) cin >> field[i];

  vector<vector<int>> dist(h, vector<int>(w, -1));
  dist[x1][y1] = 0;

  queue<pair<int, int>> que;
  que.push(make_pair(x1, y1));

  while (!que.empty()) {
    pair<int, int> current_pos = que.front();
    int x = current_pos.first;
    int y = current_pos.second;
    que.pop();

    rep(d, 4) {
      rep(i, k) {
        int next_x = x + (i + 1) * dx[d];
        int next_y = y + (i + 1) * dy[d];
        if (next_x < 0 || next_x >= h || next_y < 0 || next_y >= w)
          break;
        if (field[next_x][next_y] == '@')
          break;
        if (dist[next_x][next_y] <= dist[x][y] && dist[next_x][next_y] != -1)
          break;
        if (dist[next_x][next_y] != -1)
          continue;

        que.push(make_pair(next_x, next_y));
        dist[next_x][next_y] = dist[x][y] + 1;
      }
    }
  }

  /*for(int i = 0; i < h; i++){
      for(int j = 0; j < w; j++){
          cout << dist[i][j] << ' ';
      }
      cout << endl;
  }*/

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

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)

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

int main() {
  int h, w, k;
  cin >> h >> w >> k;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--, y1--, x2--, y2--;
  vector<string> field(h);
  rep(i, h) cin >> field[i];

  vector<vector<int>> dist(h, vector<int>(w, -1));
  dist[x1][y1] = 0;

  queue<pair<int, int>> que;
  que.push(make_pair(x1, y1));

  while (!que.empty()) {
    pair<int, int> current_pos = que.front();
    int x = current_pos.first;
    int y = current_pos.second;
    que.pop();

    rep(d, 4) {
      rep(i, k) {
        int next_x = x + (i + 1) * dx[d];
        int next_y = y + (i + 1) * dy[d];
        if (next_x < 0 || next_x >= h || next_y < 0 || next_y >= w)
          break;
        if (field[next_x][next_y] == '@')
          break;
        if (dist[next_x][next_y] <= dist[x][y] && dist[next_x][next_y] != -1)
          break;

        que.push(make_pair(next_x, next_y));
        dist[next_x][next_y] = dist[x][y] + 1;
      }
    }
  }

  /*for(int i = 0; i < h; i++){
      for(int j = 0; j < w; j++){
          cout << dist[i][j] << ' ';
      }
      cout << endl;
  }*/

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

/*
 *  Lang    : C++
 *  Author  : little_angel
 */
#include <bits/stdc++.h>

using namespace std;

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);

  int H, W, K;
  cin >> H >> W >> K;
  vector<pair<int, int>> del = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};

  const int inf = 1e9;
  vector<vector<int>> stroke(H, vector<int>(W, inf));
  vector<string> g(H);
  int sx, sy, ex, ey, dx, dy, mx, my, x, y;
  cin >> sx >> sy >> ex >> ey;
  for (auto &i : g)
    cin >> i;
  queue<pair<int, int>> q;
  q.push({sx - 1, sy - 1});
  stroke[sx - 1][sy - 1] = 0;

  while (not q.empty()) {
    tie(x, y) = q.front();
    q.pop();
    for (auto diff : del) {
      tie(dx, dy) = diff;
      for (int k = 1; k <= K; ++k) {
        mx = x + k * dx;
        my = y + k * dy;
        if (mx < 0 or H <= mx or my < 0 or W <= my or g[mx][my] == '@' or
            1 + stroke[x][y] > stroke[mx][my])
          break;
        if (stroke[mx][my] > 1 + stroke[x][y]) {
          stroke[mx][my] = 1 + stroke[x][y];
          if (mx == ex - 1 and my == ey - 1) {
            cout << stroke[mx][my] << endl;
            return 0;
          }
          q.push({mx, my});
        }
      }
    }
  }

  cout << -1 << endl;
}

Error: TLE

Fixed submission:

/*
 *  Lang    : C++
 *  Author  : little_angel
 */
#include <bits/stdc++.h>

using namespace std;

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);

  int H, W, K;
  cin >> H >> W >> K;
  vector<pair<int, int>> del = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};

  const int inf = 1e9;
  vector<vector<int>> stroke(H, vector<int>(W, inf));
  vector<string> g(H);
  int sx, sy, ex, ey, dx, dy, mx, my, x, y;
  cin >> sx >> sy >> ex >> ey;
  for (auto &i : g)
    cin >> i;
  queue<pair<int, int>> q;
  q.push({sx - 1, sy - 1});
  stroke[sx - 1][sy - 1] = 0;

  while (not q.empty()) {
    tie(x, y) = q.front();
    q.pop();
    for (auto diff : del) {
      tie(dx, dy) = diff;
      for (int k = 1; k <= K; ++k) {
        mx = x + k * dx;
        my = y + k * dy;
        if (mx < 0 or H <= mx or my < 0 or W <= my or g[mx][my] == '@')
          break;
        if (stroke[mx][my] > 1 + stroke[x][y]) {
          stroke[mx][my] = 1 + stroke[x][y];
          if (mx == ex - 1 and my == ey - 1) {
            cout << stroke[mx][my] << endl;
            return 0;
          }
          q.push({mx, my});
        }
      }
    }
  }

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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rep2(i, i0, n) for (int i = i0; i < n; ++i)

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

  vector<vector<bool>> c(h, vector<bool>(w, false));
  rep(i, h) {
    string s;
    cin >> s;
    rep(j, w) {
      if (s[j] == '@')
        c[i][j] = true;
    }
  }

  vector<vector<int>> d(h, vector<int>(w, -1));
  queue<pair<pair<int, int>, int>> que;

  que.push({{x1, y1}, 0});
  d[y1][x1] = 0;

  while (!que.empty()) {
    int cx = que.front().first.first;
    int cy = que.front().first.second;
    int cd = que.front().second;
    que.pop();

    if (cx == x2 && cy == y2) {
      cout << cd << endl;
      return 0;
    }

    int shift[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
    rep(i, 4) {
      rep2(j, 1, k + 1) {
        int nx = cx + j * shift[i][0];
        int ny = cy + j * shift[i][1];
        int nd = cd + 1;
        if (nx < 0 || nx >= w || ny < 0 || ny >= h)
          break;
        if (c[ny][nx])
          break;
        if (d[ny][nx] >= 0 && d[ny][nx] <= cd)
          break;
        if (d[ny][nx] < 0 || d[ny][nx] > nd) {
          que.push({{nx, ny}, nd});
          d[ny][nx] = nd;
        }
      }
    }
  }

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

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rep2(i, i0, n) for (int i = i0; i < n; ++i)

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

  vector<vector<bool>> c(h, vector<bool>(w, false));
  rep(i, h) {
    string s;
    cin >> s;
    rep(j, w) {
      if (s[j] == '@')
        c[i][j] = true;
    }
  }

  vector<vector<int>> d(h, vector<int>(w, -1));
  queue<pair<pair<int, int>, int>> que;

  que.push({{x1, y1}, 0});
  d[y1][x1] = 0;

  while (!que.empty()) {
    int cx = que.front().first.first;
    int cy = que.front().first.second;
    int cd = que.front().second;
    que.pop();

    if (cx == x2 && cy == y2) {
      cout << cd << endl;
      return 0;
    }

    int shift[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
    rep(i, 4) {
      rep2(j, 1, k + 1) {
        int nx = cx + j * shift[i][0];
        int ny = cy + j * shift[i][1];
        int nd = cd + 1;
        if (nx < 0 || nx >= w || ny < 0 || ny >= h)
          break;
        if (c[ny][nx])
          break;
        if (d[ny][nx] >= 0)
          continue;

        que.push({{nx, ny}, nd});
        d[ny][nx] = nd;
      }
    }
  }

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

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

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

  vector<string> c(H);
  for (string &C : c) {
    cin >> C;
  }

  vector<vector<int64_t>> cost(H, vector<int64_t>(W, LLONG_MAX));
  struct Element {
    int64_t x, y, cost;
    bool operator>(const Element &rhs) const { return cost > rhs.cost; }
  };
  priority_queue<Element, vector<Element>, greater<Element>> pq;
  pq.push({x1, y1, cost[x1][y1] = 0});

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

  while (!pq.empty()) {
    Element t = pq.top();
    pq.pop();
    if (t.cost > cost[t.x][t.y]) {
      continue;
    }

    for (int64_t i = 0; i < 4; i++) {
      int64_t nx = t.x, ny = t.y;
      int64_t new_cost = t.cost + 1;
      for (int64_t j = 0; j < K; j++) {
        nx += dx[i];
        ny += dy[i];
        if (nx < 0 || H <= nx || ny < 0 || W <= ny || c[nx][ny] == '@' ||
            new_cost > cost[nx][ny]) {
          break;
        }
        if (new_cost < cost[nx][ny]) {
          pq.push({nx, ny, cost[nx][ny] = new_cost});
        }
      }
    }
  }

  int64_t ans = cost[x2][y2];
  cout << (ans == LLONG_MAX ? -1 : ans) << endl;
}

Error: TLE

Fixed submission:

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

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

  vector<string> c(H);
  for (string &C : c) {
    cin >> C;
  }

  vector<vector<int64_t>> cost(H, vector<int64_t>(W, LLONG_MAX));
  struct Element {
    int64_t x, y, cost;
    bool operator>(const Element &rhs) const { return cost > rhs.cost; }
  };
  priority_queue<Element, vector<Element>, greater<Element>> pq;
  pq.push({x1, y1, cost[x1][y1] = 0});

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

  while (!pq.empty()) {
    Element t = pq.top();
    pq.pop();
    if (t.cost > cost[t.x][t.y]) {
      continue;
    }

    for (int64_t i = 0; i < 4; i++) {
      int64_t nx = t.x, ny = t.y;
      int64_t new_cost = t.cost + 1;
      for (int64_t j = 0; j < K; j++) {
        nx += dx[i];
        ny += dy[i];
        if (nx < 0 || H <= nx || ny < 0 || W <= ny || c[nx][ny] == '@') {
          break;
        }
        if (new_cost <= cost[nx][ny]) {
          pq.push({nx, ny, cost[nx][ny] = new_cost});
        } else {
          break;
        }
      }
    }
  }

  int64_t ans = cost[x2][y2];
  cout << (ans == LLONG_MAX ? -1 : ans) << endl;
}
Problem p02644 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

  //    H = W = 1e3;
  //    K = 1e3 / 2 - 1;
  //    x1 = y1 = 0;
  //    x2 = y2 = 1e3 - 1;

  vector<string> c(H);
  for (string &C : c) {
    cin >> C;
  }
  //    for (string& C : c) {
  //        C.assign(W, '.');
  //    }

  vector<vector<int64_t>> cost(H, vector<int64_t>(W, LLONG_MAX));
  struct Element {
    int64_t x, y, cost;
    bool operator>(const Element &rhs) const { return cost > rhs.cost; }
  };
  priority_queue<Element, vector<Element>, greater<Element>> pq;
  pq.push({x1, y1, cost[x1][y1] = 0});

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

  while (!pq.empty()) {
    Element t = pq.top();
    pq.pop();
    if (t.cost > cost[t.x][t.y]) {
      continue;
    }

    for (int64_t i = 0; i < 4; i++) {
      int64_t nx = t.x, ny = t.y;
      int64_t new_cost = t.cost + 1;
      int64_t fail_num = 0;
      for (int64_t j = 0; j < K; j++) {
        nx += dx[i];
        ny += dy[i];
        if (nx < 0 || H <= nx || ny < 0 || W <= ny || c[nx][ny] == '@') {
          break;
        }
        if (new_cost < cost[nx][ny]) {
          pq.push({nx, ny, cost[nx][ny] = new_cost});
          fail_num = 0;
        } else if (++fail_num >= 5) {
          break;
        }
      }
    }
  }

  int64_t ans = cost[x2][y2];
  cout << (ans == LLONG_MAX ? -1 : ans) << endl;
}

Error: TLE

Fixed submission:

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

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

  vector<string> c(H);
  for (string &C : c) {
    cin >> C;
  }

  vector<vector<int64_t>> cost(H, vector<int64_t>(W, LLONG_MAX));
  struct Element {
    int64_t x, y, cost;
    bool operator>(const Element &rhs) const { return cost > rhs.cost; }
  };
  priority_queue<Element, vector<Element>, greater<Element>> pq;
  pq.push({x1, y1, cost[x1][y1] = 0});

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

  while (!pq.empty()) {
    Element t = pq.top();
    pq.pop();
    if (t.cost > cost[t.x][t.y]) {
      continue;
    }

    for (int64_t i = 0; i < 4; i++) {
      int64_t nx = t.x, ny = t.y;
      int64_t new_cost = t.cost + 1;
      for (int64_t j = 0; j < K; j++) {
        nx += dx[i];
        ny += dy[i];
        if (nx < 0 || H <= nx || ny < 0 || W <= ny || c[nx][ny] == '@') {
          break;
        }
        if (new_cost < cost[nx][ny]) {
          pq.push({nx, ny, cost[nx][ny] = new_cost});
        }
      }
    }
  }

  int64_t ans = cost[x2][y2];
  cout << (ans == LLONG_MAX ? -1 : ans) << endl;
}