alexjercan/bugnet huggingface dataset:
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 (chmin(D[ny][nx], d + 1)) {
          que.push(make_pair(D[ny][nx], make_pair(ny, nx)));
        } else if (D[ny][nx] != d + 1) {
          break;
        }
      }
    }
  }
  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) || D[ny][nx] == d + 1) {
          que.push(make_pair(D[ny][nx], make_pair(ny, nx)));
        } else {
          break;
        }
      }
    }
  }
  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;

const int dir[4][2] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}};

const int INF = 1e9;

int main() {

  int h, w, k, x1, y1, x2, y2;
  cin >> h >> w >> k >> x1 >> y1 >> x2 >> y2;

  --x1, --y1, --x2, --y2;

  char MP[h][w] = {};

  int dis[h][w] = {};

  for (int i = 0; i < h; i++)

    for (int j = 0; j < w; j++)

      cin >> MP[i][j], dis[i][j] = INF;

  queue<pair<int, int>> que;

  dis[x1][y1] = 0;

  que.push({x1, y1});

  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 (nx < 0 or ny < 0 or nx >= h or ny >= w or MP[nx][ny] == '@' or
            dis[nx][ny] < nd)

          break;

        if (dis[nx][ny] > nd) {

          dis[nx][ny] = nd;

          que.push({nx, ny});
        }
      }
    }
  }

  int ans = dis[x2][y2];

  cout << (ans == INF ? -1 : ans);
}


Error: TLE

Fixed submission:

#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
char **Mat;
int **vis;
int n, m;
int dx[4] = {1, -1, 0, 0}, dy[4] = {0, 0, 1, -1};
int check(int x, int y) {
  if (x < 1 || x > n || y < 1 || y > m)
    return 0;
  return 1;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int k;
  int x1, x2, y1, y2;
  cin >> n >> m >> k >> x1 >> y1 >> x2 >> y2;
  Mat = new char *[n + 5];
  for (int i = 0; i < n + 5; ++i)
    Mat[i] = new char[m + 5];
  vis = new int *[n + 5];
  for (int i = 0; i < n + 5; ++i)
    vis[i] = new int[m + 5];
  for (int i = 0; i <= n + 2; ++i)
    for (int j = 0; j <= m + 2; ++j)
      vis[i][j] = 0;
  for (int i = 1; i <= n; ++i)
    cin >> Mat[i] + 1;
  queue<int> qx, qy, ql;
  qx.push(x1);
  qy.push(y1);
  vis[x1][y1] = 1;
  ql.push(0);
  while (qx.size()) {
    int tx = qx.front();
    int ty = qy.front();
    vis[tx][ty] = 1;
    // cout<<tx<<" "<<ty<<endl;
    int len = ql.front();
    if (tx == x2 && ty == y2) {
      cout << len;
      return 0;
    }
    for (int i = 0; i < 4; ++i)
      for (int j = 1; j <= k; ++j) {
        int nx = tx + dx[i] * j;
        int ny = ty + dy[i] * j;
        // cout<<tx<<" "<<ty<<"   "<<nx<<" "<<ny<<endl;
        if (!check(nx, ny) || Mat[nx][ny] == '@')
          break;
        // cout<<vis[nx][ny];
        if (vis[nx][ny])
          continue;
        qx.push(nx);
        qy.push(ny);
        // cout<<"  通过！"<<endl;
        vis[nx][ny] = 1;
        ql.push(len + 1);
      }
    qx.pop();
    qy.pop();
    ql.pop();
  }
  cout << -1 << endl;
  return 0;
}
Problem p02644 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  ios::sync_with_stdio(false);

  int H, W, K;
  cin >> H >> W >> K;

  int sx, sy, gx, gy;
  cin >> sx >> sy >> gx >> gy;
  --sx, --sy;
  --gx, --gy;

  vector<string> G(H);
  for (int i = 0; i < H; ++i)
    cin >> G[i];

  vector<vector<vector<int>>> vis(H, vector<vector<int>>(W, vector<int>(2)));
  vector<vector<int>> dis(H, vector<int>(W, 0x3f3f3f3f));
  queue<tuple<int, int, int>> bag;
  bag.emplace(dis[sx][sy] = 0, sx, sy);
  while (bag.size()) {
    int d, x, y;
    tie(d, x, y) = bag.front();
    bag.pop();
    const int dx[] = {0, 1, 0, -1};
    const int dy[] = {1, 0, -1, 0};
    for (int _ = 0; _ < 4; ++_) {
      for (int i = 1; i <= K; ++i) {
        int nx = x + i * dx[_];
        int ny = y + i * dy[_];
        if (nx < 0 || nx >= H)
          break;
        if (ny < 0 || ny >= W)
          break;
        if (G[nx][ny] == '@')
          break;
        if (vis[nx][ny][_ & 1])
          break;
        vis[nx][ny][_ & 1] = 1;
        if (dis[nx][ny] < d + 1)
          break;
        bag.emplace(dis[nx][ny] = d + 1, nx, ny);
      }
    }
  }

  if (dis[gx][gy] == 0x3f3f3f3f)
    cout << -1 << endl;
  else
    cout << dis[gx][gy] << endl;

  return 0;
}


Error: TLE

Fixed submission:

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

int main() {
  ios::sync_with_stdio(false);

  int H, W, K;
  cin >> H >> W >> K;

  int sx, sy, gx, gy;
  cin >> sx >> sy >> gx >> gy;
  --sx, --sy;
  --gx, --gy;

  vector<string> G(H);
  for (int i = 0; i < H; ++i)
    cin >> G[i];

  vector<vector<vector<int>>> vis(H, vector<vector<int>>(W, vector<int>(2)));
  vector<vector<int>> dis(H, vector<int>(W, 0x3f3f3f3f));
  queue<tuple<int, int, int>> bag;
  bag.emplace(dis[sx][sy] = 0, sx, sy);
  while (bag.size()) {
    int d, x, y;
    tie(d, x, y) = bag.front();
    bag.pop();
    if (dis[x][y] < d)
      continue;
    const int dx[] = {0, 1, 0, -1};
    const int dy[] = {1, 0, -1, 0};
    for (int _ = 0; _ < 4; ++_) {
      for (int i = 1; i <= K; ++i) {
        int nx = x + i * dx[_];
        int ny = y + i * dy[_];
        if (nx < 0 || nx >= H)
          continue;
        if (ny < 0 || ny >= W)
          continue;
        if (G[nx][ny] == '@')
          break;
        if (vis[nx][ny][_ & 1])
          continue;
        vis[nx][ny][_ & 1] = 1;
        if (dis[nx][ny] < d + 1)
          continue;
        bag.emplace(dis[nx][ny] = d + 1, nx, ny);
      }
    }
  }

  if (dis[gx][gy] == 0x3f3f3f3f)
    cout << -1 << endl;
  else
    cout << dis[gx][gy] << endl;

  return 0;
}
Problem p02644 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long i64;
const int MAX = 1e6 + 10;
const int MOD = 1e9 + 7;
const int oo = 1e9;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
#define endl '\\n'
  int h, w, k;
  cin >> h >> w >> k;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  vector<vector<bool>> p(h + 2, vector<bool>(w + 2));
  for (int i = 1; i <= h; i++) {
    string s;
    cin >> s;
    for (int j = 1; j <= w; j++) {

      p[i][j] = (s[j - 1] == '@') ? false : true;
    }
  }

  int dir[4][2] = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}};
  vector<vector<int>> dis(h + 2, vector<int>(w + 2, -1));

  vector<pair<int, int>> que;
  que.push_back({x1, y1});
  dis[x1][y1] = 0;
  int front = 0;
  while (front < que.size()) {
    auto cur = que[front];
    front++;
    int cd = dis[cur.first][cur.second];
    for (int d = 0; d < 4; d++) {

      for (int t = 1; t <= k; t++) {
        int x = cur.first + t * dir[d][0];
        int y = cur.second + t * dir[d][1];
        if (!p[x][y]) {
          break;
        }
        if (dis[x][y] != -1) {
          if (dis[x][y] <= cd)
            break;
          else
            continue;
        }
        dis[x][y] = cd + 1;
        que.push_back({x, y});
      }
    }
  }
  cout << dis[x2][y2] << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long i64;
const int MAX = 1e6 + 10;
const int MOD = 1e9 + 7;
const int oo = 1e9;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
#define endl '\\n'
  int h, w, k;
  cin >> h >> w >> k;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  vector<vector<bool>> p(h + 2, vector<bool>(w + 2));
  for (int i = 1; i <= h; i++) {
    string s;
    cin >> s;
    for (int j = 1; j <= w; j++) {

      p[i][j] = (s[j - 1] == '@') ? false : true;
    }
  }

  int dir[4][2] = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}};
  vector<vector<int>> dis(h + 2, vector<int>(w + 2, -1));

  vector<pair<int, int>> que;
  que.push_back({x1, y1});
  dis[x1][y1] = 0;
  int front = 0;
  while (front < que.size()) {
    auto cur = que[front];
    front++;
    int cd = dis[cur.first][cur.second];
    for (int d = 0; d < 4; d++) {

      for (int t = 1; t <= k; t++) {
        int x = cur.first + t * dir[d][0];
        int y = cur.second + t * dir[d][1];
        if (!p[x][y]) {
          break;
        }
        if (dis[x][y] != -1) {
          if (dis[x][y] <= cd)
            break;
        }
        dis[x][y] = cd + 1;
        que.push_back({x, y});
      }
    }
  }
  cout << dis[x2][y2] << endl;
}
Problem p02644 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define ll long long int
#define pb push_back
#define rep(i, j, n) for (ll i = j; i < n; i++)
#define per(i, j, n) for (ll i = j; i >= n; i--)
#define all(x) x.begin(), x.end()
typedef pair<int, int> pii;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<ll> vll;

vector<vector<char>> a;
vector<vector<ll>> dist, vis;

int main() {
  ll n, m, k, x1, y1, x2, y2;
  cin >> n >> m >> k >> x1 >> y1 >> x2 >> y2;
  a = vector<vector<char>>(n + 1, vector<char>(m + 1));
  vis = dist = vector<vector<ll>>(n + 1, vector<ll>(m + 1, 0));
  rep(i, 1, n + 1) {
    rep(j, 1, m + 1) { cin >> a[i][j]; }
  }
  queue<pair<ll, ll>> q;
  q.push({x1, y1});
  ll dirx[] = {0, 0, 1, -1};
  ll diry[] = {1, -1, 0, 0};
  dist[x1][y1] = 0;
  vis[x1][y1] = 1;
  while (!q.empty()) {
    ll x0 = q.front().first;
    ll y0 = q.front().second;
    q.pop();
    rep(i, 0, 4) {
      rep(j, 1, k + 1) {
        ll nx = x0 + j * dirx[i];
        ll ny = y0 + j * diry[i];
        if (nx > n || nx < 1 || ny > m || ny < 1)
          break;
        if (a[nx][ny] == '@')
          break;
        if (vis[nx][ny] && dist[x0][y0] + 1 > dist[nx][ny])
          break;
        if (vis[nx][ny])
          continue;

        vis[nx][ny] = 1;
        dist[nx][ny] = dist[x0][y0] + 1;
        q.push({nx, ny});
        if (nx == x2 && ny == y2) {
          cout << dist[x2][y2];
          return 0;
        }
      }
    }
  }
  cout << -1 << endl;
}


Error: TLE

Fixed submission:

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

#define ll long long int
#define pb push_back
#define rep(i, j, n) for (ll i = j; i < n; i++)
#define per(i, j, n) for (ll i = j; i >= n; i--)
#define all(x) x.begin(), x.end()
typedef pair<int, int> pii;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<ll> vll;

vector<vector<char>> a;
vector<vector<ll>> dist, vis;

int main() {
  ll n, m, k, x1, y1, x2, y2;
  cin >> n >> m >> k >> x1 >> y1 >> x2 >> y2;
  a = vector<vector<char>>(n + 1, vector<char>(m + 1));
  vis = dist = vector<vector<ll>>(n + 1, vector<ll>(m + 1, 0));
  rep(i, 1, n + 1) {
    rep(j, 1, m + 1) { cin >> a[i][j]; }
  }
  queue<pair<ll, ll>> q;
  q.push({x1, y1});
  ll dirx[] = {0, 0, 1, -1};
  ll diry[] = {1, -1, 0, 0};
  dist[x1][y1] = 0;
  vis[x1][y1] = 1;
  while (!q.empty()) {
    ll x0 = q.front().first;
    ll y0 = q.front().second;
    q.pop();
    rep(i, 0, 4) {
      rep(j, 1, k + 1) {
        ll nx = x0 + j * dirx[i];
        ll ny = y0 + j * diry[i];
        if (nx > n || nx < 1 || ny > m || ny < 1)
          break;
        if (a[nx][ny] == '@')
          break;
        if (vis[nx][ny])
          continue;

        vis[nx][ny] = 1;
        dist[nx][ny] = dist[x0][y0] + 1;
        q.push({nx, ny});
        if (nx == x2 && ny == y2) {
          cout << dist[x2][y2];
          return 0;
        }
      }
    }
  }
  cout << -1 << endl;
}
Problem p02644 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i < b; ++i)
#define all(c) c.begin(), c.end()
#define gmax(x, y) x = max(x, y)
#define gmin(x, y) x = min(x, y)
#define gadd(x, y) x = add(x, y)
#define gmul(x, y) x = mul(x, y)
using namespace std;

typedef pair<int, int> pii;
typedef long long ll;
typedef vector<int> vi;

pii dir[4] = {{0, 1}, {0, -1}, {-1, 0}, {1, 0}};
int h, w, k;
int sx, sy, gx, gy;

int bfs(vector<vi> &g) {
  vector<vi> d(g.size(), vi(g[0].size()));
  queue<pii> q;
  int dist = 1;
  q.push({sx, sy});
  d[sx][sy] = 1;
  while (q.size()) {
    ++dist;
    int cnt = q.size();
    while (cnt--) {
      int x = q.front().first;
      int y = q.front().second;
      q.pop();
      rep(i, 0, 4) {
        int cx = x, cy = y;
        rep(j, 0, k) {
          cx += dir[i].first;
          cy += dir[i].second;
          if ((d[cx][cy] != 0 && d[cx][cy] < dist) || g[cx][cy] == 0) {
            break;
          }
          if (d[cx][cy] == 0) {
            d[cx][cy] = dist;
            q.push({cx, cy});
          }
        }
      }
    }
  }
  int ans = d[gx][gy];
  --ans;
  return ans;
}

int main() {
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(0);
  scanf("%d %d %d", &h, &w, &k);
  vector<vi> g(h + 2, vi(w + 2));
  scanf("%d %d %d %d", &sx, &sy, &gx, &gy);
  rep(i, 1, h + 1) {
    rep(j, 1, w + 1) {
      char c;
      scanf(" %c", &c);
      g[i][j] = c == '.' ? 1 : 0;
    }
  }
  int ans = bfs(g);
  printf("%d\\n", ans);
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i < b; ++i)
#define all(c) c.begin(), c.end()
#define gmax(x, y) x = max(x, y)
#define gmin(x, y) x = min(x, y)
#define gadd(x, y) x = add(x, y)
#define gmul(x, y) x = mul(x, y)
using namespace std;

typedef pair<int, int> pii;
typedef long long ll;
typedef vector<int> vi;

pii dir[4] = {{0, 1}, {0, -1}, {-1, 0}, {1, 0}};
int h, w, k;
int sx, sy, gx, gy;

int bfs(vector<vi> &g) {
  vector<vi> d(g.size(), vi(g[0].size()));
  queue<pii> q;
  int dist = 1;
  q.push({sx, sy});
  d[sx][sy] = 1;
  while (q.size()) {
    ++dist;
    int cnt = q.size();
    while (cnt--) {
      int x = q.front().first;
      int y = q.front().second;
      q.pop();
      rep(i, 0, 4) {
        int cx = x, cy = y;
        rep(j, 0, k) {
          cx += dir[i].first;
          cy += dir[i].second;
          if ((d[cx][cy] != 0 && d[cx][cy] < dist) || g[cx][cy] == 0) {
            break;
          }
          d[cx][cy] = dist;
          q.push({cx, cy});
        }
      }
    }
  }
  int ans = d[gx][gy];
  --ans;
  return ans;
}

int main() {
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(0);
  scanf("%d %d %d", &h, &w, &k);
  vector<vi> g(h + 2, vi(w + 2));
  scanf("%d %d %d %d", &sx, &sy, &gx, &gy);
  rep(i, 1, h + 1) {
    rep(j, 1, w + 1) {
      char c;
      scanf(" %c", &c);
      g[i][j] = c == '.' ? 1 : 0;
    }
  }
  int ans = bfs(g);
  printf("%d\\n", ans);
}
Problem p02644 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ld = long double;

const int INF = 1e9 + 69;

int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
int h, w, k;
int x1, y11, x2, y2;
queue<pair<int, int>> Q;

bool valid(int a, int b) { return a >= 1 && a <= h && b >= 1 && b <= w; }

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  cin >> h >> w >> k;
  cin >> x1 >> y11;
  cin >> x2 >> y2;

  vector<vector<char>> c(h + 1, vector<char>(w + 1));
  vector<vector<bool>> vis(h + 1, vector<bool>(w + 1, 0));
  vector<vector<int>> dist(h + 1, vector<int>(w + 1, INF));
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      cin >> c[i][j];
    }
  }

  Q.emplace(x1, y11);
  dist[x1][y11] = 0;
  while (!Q.empty()) {
    pair<int, int> cur = Q.front();
    int x = cur.first;
    int y = cur.second;
    vis[x][y] = true;
    Q.pop();
    for (int i = 0; i < 4; i++) {
      for (int j = 1; j <= k; j++) {
        int nx = x + j * dx[i];
        int ny = y + j * dy[i];
        if (!valid(nx, ny) || c[nx][ny] == '@' || dist[nx][ny] <= dist[x][y])
          break;
        if (dist[nx][ny] == dist[x][y] + 1)
          continue;
        if (vis[nx][ny])
          continue;
        dist[nx][ny] = dist[x][y] + 1;
        Q.emplace(nx, ny);
      }
    }
  }
  if (!vis[x2][y2])
    cout << -1 << '\\n';
  else
    cout << dist[x2][y2] << '\\n';
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ld = long double;

const int INF = 1e9 + 69;

int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
int h, w, k;
int x1, y11, x2, y2;
queue<pair<int, int>> Q;

bool valid(int a, int b) { return a >= 1 && a <= h && b >= 1 && b <= w; }

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  cin >> h >> w >> k;
  cin >> x1 >> y11;
  cin >> x2 >> y2;

  vector<vector<char>> c(h + 1, vector<char>(w + 1));
  vector<vector<bool>> vis(h + 1, vector<bool>(w + 1, 0));
  vector<vector<int>> dist(h + 1, vector<int>(w + 1, INF));
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      cin >> c[i][j];
    }
  }

  Q.emplace(x1, y11);
  dist[x1][y11] = 0;
  while (!Q.empty()) {
    pair<int, int> cur = Q.front();
    int x = cur.first;
    int y = cur.second;
    vis[x][y] = true;
    Q.pop();
    for (int i = 0; i < 4; i++) {
      for (int j = 1; j <= k; j++) {
        int nx = x + j * dx[i];
        int ny = y + j * dy[i];
        if (!valid(nx, ny) || c[nx][ny] == '@' || dist[nx][ny] <= dist[x][y])
          break;
        if (vis[nx][ny])
          continue;
        dist[nx][ny] = min(dist[nx][ny], dist[x][y] + 1);
        Q.emplace(nx, ny);
      }
    }
  }
  if (!vis[x2][y2])
    cout << -1 << '\\n';
  else
    cout << dist[x2][y2] << '\\n';
  return 0;
}
Problem p02644 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
int H, w, k, X, Y, xx, yy, maxx = 23333333;
int q[4] = {-1, 0, 1, 0};
int p[4] = {0, 1, 0, -1};
vector<int> l[3];
int main(void) {
  cin >> H >> w >> k >> xx >> yy >> X >> Y;
  xx--;
  yy--;
  X--;
  Y--;
  char c[H][w];
  int f[H][w];
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < w; j++) {
      cin >> c[i][j];
      f[i][j] = maxx;
    }
  }
  int h = 0, r = 1;
  l[0].push_back(xx);
  l[1].push_back(yy);
  l[2].push_back(0);
  f[xx][yy] = 0;
  while (h < r) {
    for (int i = 0; i < 4; i++) {
      int x1 = l[0][h], y1 = l[1][h];
      for (int j = 0; j < k; j++) {
        x1 += q[i];
        y1 += p[i];
        if (x1 < 0 || x1 >= H || y1 < 0 || y1 >= w || c[x1][y1] == '@' ||
            f[x1][y1] < l[2][h] + 1)
          break;
        if (f[x1][y1] > l[2][h] + 1) {
          f[x1][y1] = l[2][h] + 1;
          l[0].push_back(x1);
          l[1].push_back(y1);
          l[2].push_back(l[2][h] + 1);
          r++;
        }
      }
    }
    h++;
  }
  if (f[X][Y] == maxx)
    cout << "-1" << endl;
  else
    cout << f[X][Y] << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
int H, w, k, X, Y, xx, yy, maxx = 23333333;
int q[4] = {-1, 0, 1, 0};
int p[4] = {0, 1, 0, -1};
vector<int> l[3];
int main(void) {
  cin >> H >> w >> k >> xx >> yy >> X >> Y;
  xx--;
  yy--;
  X--;
  Y--;
  char c[H][w];
  int f[H][w];
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < w; j++) {
      cin >> c[i][j];
      f[i][j] = maxx;
    }
  }
  int h = 0, r = 1;
  l[0].push_back(xx);
  l[1].push_back(yy);
  l[2].push_back(0);
  f[xx][yy] = 0;
  while (h < r) {
    for (int i = 0; i < 4; i++) {
      int x1 = l[0][h], y1 = l[1][h];
      for (int j = 0; j < k; j++) {
        x1 += q[i];
        y1 += p[i];
        if (x1 < 0 || x1 >= H || y1 < 0 || y1 >= w || c[x1][y1] == '@' ||
            f[x1][y1] < l[2][h] + 1)
          break;
        f[x1][y1] = l[2][h] + 1;
        l[0].push_back(x1);
        l[1].push_back(y1);
        l[2].push_back(l[2][h] + 1);
        r++;
      }
    }
    h++;
  }
  if (f[X][Y] == maxx)
    cout << "-1" << endl;
  else
    cout << f[X][Y] << endl;
  return 0;
}