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

#include <bits/stdc++.h>
using namespace std;
const long long int INF = 1e18;

int main() {
  int h, w, k;
  cin >> h >> w >> k;

  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  int s = (x1 - 1) * w + (y1 - 1);
  int t = (x2 - 1) * w + (y2 - 1);

  vector<vector<char>> c(h, vector<char>(w));
  for (int x = 0; x < h; x++) {
    for (int y = 0; y < w; y++) {
      cin >> c[x][y];
    }
  }

  vector<long long int> d(h * w, INF);
  queue<long long int> q;
  d[s] = 0;
  q.push(s);

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

  while (q.empty() == false) {
    long long int current = q.front();
    q.pop();
    int cx = current / w;
    int cy = current % w;

    for (int i = 0; i < 4; i++) {
      int x = cx;
      int y = cy;

      for (int j = 0; j < k; j++) {
        x += dx[i];
        y += dy[i];
        int v = x * w + y;

        if (x < 0 || h <= x || y < 0 || w <= y || c[x][y] == '@')
          break;

        if (d[v] != INF && d[v] != d[current] + 1)
          break;

        if (d[v] == INF) {
          d[v] = d[current] + 1;
          q.push(v);
        }
      }
    }
  }

  /*
  for(int x=0; x<h; x++){
      for(int y=0; y<w; y++){
          if(d[x * w + y] == INF) cout << "   @";
          else cout << setw(4) << d[x * w + y];
      }
      cout << endl;
  }
  */

  if (d[t] != INF)
    cout << d[t] << endl;
  if (d[t] == INF)
    cout << -1 << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const long long int INF = 1e18;

int main() {
  int h, w, k;
  cin >> h >> w >> k;

  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  int s = (x1 - 1) * w + (y1 - 1);
  int t = (x2 - 1) * w + (y2 - 1);

  vector<vector<char>> c(h, vector<char>(w));
  for (int x = 0; x < h; x++) {
    for (int y = 0; y < w; y++) {
      cin >> c[x][y];
    }
  }

  vector<long long int> d(h * w, INF);
  queue<long long int> q;
  d[s] = 0;
  q.push(s);

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

  while (q.empty() == false) {
    long long int current = q.front();
    q.pop();
    if (current == t) {
      cout << d[t] << endl;
      return 0;
    }
    int cx = current / w;
    int cy = current % w;

    for (int i = 0; i < 4; i++) {
      int x = cx;
      int y = cy;

      for (int j = 0; j < k; j++) {
        x += dx[i];
        y += dy[i];
        int v = x * w + y;

        if (x < 0 || h <= x || y < 0 || w <= y || c[x][y] == '@')
          break;

        if (d[v] != INF && d[v] != d[current] + 1)
          break;
        d[v] = d[current] + 1;
        q.push(v);
      }
    }
  }

  /*
  for(int x=0; x<h; x++){
      for(int y=0; y<w; y++){
          if(d[x * w + y] == INF) cout << "   @";
          else cout << setw(4) << d[x * w + y];
      }
      cout << endl;
  }
  */

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

#include <algorithm>
#include <functional>
#include <iostream>
#include <queue>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

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

  vector<vector<bool>> c(h + 2, vector<bool>(w + 2, false));
  for (int i = 0; i < h; i++) {
    string s;
    cin >> s;

    for (int j = 0; j < w; j++) {
      c[i + 1][j + 1] = (s[j] == '.');
    }
  }

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

  long long inf = 1LL << 61;

  vector<vector<vector<long long>>> dp(
      h + 2, vector<vector<long long>>(w + 2, vector<long long>(4, inf)));
  priority_queue<tuple<long long, int, int, int>,
                 vector<tuple<long long, int, int, int>>,
                 greater<tuple<long long, int, int, int>>>
      que;
  for (int i = 0; i < 4; i++) {
    dp[x1][y1][i] = 0;
    que.push(make_tuple(0, x1, y1, i));
  }

  while (!que.empty()) {
    tuple<long long, int, int, int> q = que.top();
    que.pop();

    int x = get<1>(q);
    int y = get<2>(q);
    int d = get<3>(q);

    for (int i = 0; i < 4; i++) {
      if (i == d && c[x + dx[i]][y + dy[i]] &&
          dp[x + dx[i]][y + dy[i]][i] > dp[x][y][d] + 1) {
        dp[x + dx[i]][y + dy[i]][i] = dp[x][y][d] + 1;
        que.push(
            make_tuple(dp[x + dx[i]][y + dy[i]][i], x + dx[i], y + dy[i], i));
      } else if (i != d && dp[x][y][i] > ((dp[x][y][d] + k - 1) / k) * k) {
        dp[x][y][i] = ((dp[x][y][d] + k - 1) / k) * k;
        que.push(make_tuple(dp[x][y][i], x, y, i));
      }
    }
  }

  long long r = *min_element(dp[x2][y2].begin(), dp[x2][y2].end());
  cout << (r < inf ? (r + k - 1) / k : -1) << endl;
}


Error: TLE

Fixed submission:

#include <functional>
#include <iostream>
#include <queue>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

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

  vector<vector<bool>> c(h + 2, vector<bool>(w + 2, false));
  for (int i = 0; i < h; i++) {
    string s;
    cin >> s;

    for (int j = 0; j < w; j++) {
      c[i + 1][j + 1] = (s[j] == '.');
    }
  }

  vector<vector<int>> d(h + 2, vector<int>(w + 2, h * w + 1));
  d[x1][y1] = 0;

  priority_queue<tuple<int, int, int>, vector<tuple<int, int, int>>,
                 greater<tuple<int, int, int>>>
      que;
  que.push(make_tuple(0, x1, y1));
  while (!que.empty()) {
    tuple<int, int, int> q = que.top();
    que.pop();

    int x = get<1>(q);
    int y = get<2>(q);

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

    for (int i = 0; i < 4; i++) {
      for (int j = 1; j <= k && c[x + dx[i] * j][y + dy[i] * j]; j++) {
        if (d[x + dx[i] * j][y + dy[i] * j] >= d[x][y] + 1) {
          d[x + dx[i] * j][y + dy[i] * j] = d[x][y] + 1;
          que.push(make_tuple(d[x + dx[i] * j][y + dy[i] * j], x + dx[i] * j,
                              y + dy[i] * j));
        } else {
          break;
        }
      }
    }
  }

  cout << (d[x2][y2] <= h * w ? d[x2][y2] : -1) << endl;
}
Problem p02644 (C++) — buggy submission, verdict: Time Limit Exceeded

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
void read(int &x) {
  char ch = getchar();
  x = 0;
  while (!isdigit(ch))
    ch = getchar();
  while (isdigit(ch))
    x = x * 10 + ch - 48, ch = getchar();
}
const int N = 1e6 + 10;
int h, w, k, d[N];
char t[N], ch[N];
queue<pair<int, int>> q;
#define fst first
#define sed second
#define f(x, y) ((x - 1) * w + y)
int xa, ya, xb, yb, in[N];
int sx[4] = {1, 0, -1, 0}, sy[4] = {0, 1, 0, -1};
signed main() {
  read(h), read(w), read(k);
  read(xa), read(ya), read(xb), read(yb);
  for (int i = 1; i <= h; ++i) {
    scanf("%s", t + 1);
    for (int j = 1; j <= w; ++j)
      ch[f(i, j)] = t[j];
  }
  for (int i = 1; i <= h * w; ++i)
    d[i] = 1e9;
  d[f(xa, ya)] = 0;
  q.push(make_pair(xa, ya));
  while (!q.empty()) {
    int x = q.front().fst, y = q.front().sed;
    int b = f(x, y);
    q.pop();
    in[b] = 0;
    if (x == xb && y == yb)
      return printf("%d\\n", d[b]), 0;
    for (int i = 0; i < 4; ++i) {
      int tx = x, ty = y;
      for (int j = 1; j <= k; ++j) {
        tx += sx[i], ty += sy[i];
        int a = f(tx, ty);
        if (tx < 1 || ty < 1 || tx > h || ty > w)
          break;
        if (ch[a] == '@' || d[a] <= d[b])
          break;
        if (d[a] > d[b] + 1) {
          d[a] = d[b] + 1;
          if (!in[a])
            in[a] = 1, q.push(make_pair(tx, ty));
        }
      }
    }
  }
  return puts("-1"), 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
int h, w, k, xa, ya, xb, yb;
vector<char> v[N];
char ch[N];
vector<int> mn[N];
queue<pair<int, int>> q;
int sx[4] = {1, 0, -1, 0}, sy[4] = {0, 1, 0, -1};
void Do_it() {
  mn[xa][ya - 1] = 0;
  q.push(make_pair(xa, ya));
  while (!q.empty()) {
    int x = q.front().first, y = q.front().second;
    q.pop();
    //		printf ("%d %d %d\\n", x, y, mn[x][y - 1]);
    for (int i = 0; i < 4; ++i) {
      int tx = x, ty = y;
      for (int j = 1; j <= k; ++j) {

        tx += sx[i], ty += sy[i];

        if (tx < 1 || tx > h || ty < 1 || ty > w || v[tx][ty - 1] == '@')
          break;
        //	if (x == 1 && y == 4) printf ("%d %d %d\\n", tx, ty, mn[tx][ty -
        //1]);
        if (mn[tx][ty - 1] <= mn[x][y - 1] + 1)
          continue;
        mn[tx][ty - 1] = mn[x][y - 1] + 1, q.push(make_pair(tx, ty));
        //	if (tx == xb && ty == yb) return;
      }
    }
  }
}
signed main() {
  scanf("%d %d %d", &h, &w, &k);
  scanf("%d %d %d %d", &xa, &ya, &xb, &yb);
  for (int i = 1; i <= h; ++i) {
    scanf("%s", ch + 1);
    for (int j = 1; j <= w; ++j)
      v[i].push_back(ch[j]);
    for (int j = 1; j <= w; ++j)
      mn[i].push_back(1e9);
  }
  Do_it();
  if (mn[xb][yb - 1] == 1e9)
    mn[xb][yb - 1] = -1;
  printf("%d\\n", mn[xb][yb - 1]);
  return 0;
}
Problem p02644 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define ll long long int

using namespace std;

const ll mod = 1000000007;
const ll inf = LONG_LONG_MAX;

int main() {

  ll h, w, k;
  cin >> h >> w >> k;

  ll sx, sy, gx, gy;
  cin >> sx >> sy >> gx >> gy;

  char e[h + 2][w + 2];
  for (int i = 0; i < w + 2; i++) {
    e[0][i] = '@';
    e[h + 1][i] = '@';
  }
  for (int i = 1; i < h + 1; i++) {
    e[i][0] = '@';
    for (int j = 1; j < w + 1; j++)
      cin >> e[i][j];
    e[i][w + 1] = '@';
  }

  ll dist[h + 2][w + 2];
  memset(dist, -1, sizeof(dist));

  dist[sx][sy] = 0;
  queue<pair<ll, ll>> que;
  que.push(make_pair(sx, sy));

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

  while (!que.empty()) {
    ll y = que.front().first;
    ll x = que.front().second;
    que.pop();

    for (int i = 0; i < 4; i++) {
      for (int j = 1; j <= k; j++) {
        if (y + dy[i] * j < 0 || y + dy[i] * j > h + 1 || x + dx[i] * j < 0 ||
            x + dx[i] * j > w + 1)
          break;
        if (e[y + dy[i] * j][x + dx[i] * j] == '@')
          break;
        if (dist[y + dy[i] * j][x + dx[i] * j] != -1) {
          if (dist[y + dy[i] * j][x + dx[i] * j] > dist[y][x])
            ;
          else
            break;
        }
        if (dist[y + dy[i] * j][x + dx[i] * j] == -1) {
          dist[y + dy[i] * j][x + dx[i] * j] = dist[y][x] + 1;
          que.push(make_pair(y + dy[i] * j, x + dx[i] * j));
        }
      }
    }
  }

  cout << dist[gx][gy] << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long int

using namespace std;

const ll mod = 1000000007;
const ll inf = LONG_LONG_MAX;

int main() {

  ll h, w, k;
  cin >> h >> w >> k;

  ll sx, sy, gx, gy;
  cin >> sx >> sy >> gx >> gy;

  char e[h + 2][w + 2];
  for (int i = 0; i < w + 2; i++) {
    e[0][i] = '@';
    e[h + 1][i] = '@';
  }
  for (int i = 1; i < h + 1; i++) {
    e[i][0] = '@';
    for (int j = 1; j < w + 1; j++)
      cin >> e[i][j];
    e[i][w + 1] = '@';
  }

  ll dist[h + 2][w + 2];
  memset(dist, -1, sizeof(dist));

  dist[sx][sy] = 0;
  queue<pair<ll, ll>> que;
  que.push(make_pair(sx, sy));

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

  while (!que.empty()) {
    ll y = que.front().first;
    ll x = que.front().second;
    que.pop();

    for (int i = 0; i < 4; i++) {
      for (int j = 1; j <= k; j++) {
        if (y + dy[i] * j < 0 || y + dy[i] * j > h + 1 || x + dx[i] * j < 0 ||
            x + dx[i] * j > w + 1)
          break;
        if (e[y + dy[i] * j][x + dx[i] * j] == '@')
          break;
        if (dist[y + dy[i] * j][x + dx[i] * j] == -1) {
          dist[y + dy[i] * j][x + dx[i] * j] = dist[y][x] + 1;
          que.push(make_pair(y + dy[i] * j, x + dx[i] * j));
        }
      }
    }
  }

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

#include <algorithm>
#include <functional>
#include <iostream>
#include <queue>
#include <tuple>
#include <vector>
using namespace std;

typedef tuple<int, int, int> t3i;
const int INF = 0x3f3f3f3f;

int solve() {
  int H, W, K;
  cin >> H >> W >> K;

  int sr, sc, tr, tc;
  cin >> sr >> sc >> tr >> tc;
  sr--;
  sc--;
  tr--;
  tc--;

  vector<string> A(H);
  for (int r = 0; r < H; r++) {
    cin >> A[r];
  }

  auto deltas = vector<t3i>();
  deltas.push_back({+1, 0, 1});
  deltas.push_back({-1, 0, 1});
  deltas.push_back({0, +1, 1});
  deltas.push_back({0, -1, 1});

  auto dis = vector<vector<int>>(H, vector<int>(W, INF));
  auto que = priority_queue<t3i, vector<t3i>, greater<t3i>>();

  dis[sr][sc] = 0;
  que.push({0, sr, sc});

  while (!que.empty()) {
    auto [d, r, c] = que.top();
    que.pop();
    if (d > dis[r][c])
      continue;
    for (auto &&[dr, dc, w] : deltas) {
      for (int k = 1; k <= K; k++) {
        int nr = r + k * dr;
        int nc = c + k * dc;
        if (nr < 0 || nr >= H)
          break;
        if (nc < 0 || nc >= W)
          break;
        if (A[nr][nc] == '@')
          break;
        if (dis[nr][nc] < d + w)
          break;
        if (dis[nr][nc] > d + w) {
          dis[nr][nc] = d + w;
          que.push({dis[nr][nc], nr, nc});
        }
      }
    }
  }

  return ((dis[tr][tc] == INF) ? -1 : dis[tr][tc]);
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  cout << solve() << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <functional>
#include <iostream>
#include <queue>
#include <tuple>
#include <vector>
using namespace std;

typedef tuple<int, int, int> t3i;
const int INF = 0x3f3f3f3f;

int solve() {
  int H, W, K;
  cin >> H >> W >> K;

  int sr, sc, tr, tc;
  cin >> sr >> sc >> tr >> tc;
  sr--;
  sc--;
  tr--;
  tc--;

  vector<string> A(H);
  for (int r = 0; r < H; r++) {
    cin >> A[r];
  }

  auto deltas = vector<t3i>();
  deltas.push_back({+1, 0, 1});
  deltas.push_back({-1, 0, 1});
  deltas.push_back({0, +1, 1});
  deltas.push_back({0, -1, 1});

  auto dis = vector<vector<int>>(H, vector<int>(W, INF));
  auto que = priority_queue<t3i, vector<t3i>, greater<t3i>>();

  dis[sr][sc] = 0;
  que.push({0, sr, sc});

  while (!que.empty()) {
    auto [d, r, c] = que.top();
    que.pop();
    if (r == tr and c == tc)
      break;
    if (d > dis[r][c])
      continue;
    for (auto &&[dr, dc, w] : deltas) {
      for (int k = 1; k <= K; k++) {
        int nr = r + k * dr;
        int nc = c + k * dc;
        if (nr < 0 || nr >= H)
          break;
        if (nc < 0 || nc >= W)
          break;
        if (A[nr][nc] == '@')
          break;
        if (dis[nr][nc] > dis[r][c] + w) {
          dis[nr][nc] = dis[r][c] + w;
          que.push({dis[nr][nc], nr, nc});
        }
      }
    }
  }

  return ((dis[tr][tc] == INF) ? -1 : dis[tr][tc]);
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  cout << solve() << endl;

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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

int n, m, k, x[2], y[2];
string adj[1000000];
vector<vector<int>> dst;
vector<vector<bool>> vis;

bool bounds(int a, int b) {
  return (a >= 0 && a < n && b >= 0 && b < m && adj[a][b] == '.' && !vis[a][b]);
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n >> m >> k;
  for (int i = 0; i < 2; i++)
    cin >> x[i] >> y[i];
  for (int i = 0; i < n; i++)
    cin >> adj[i];
  vis.resize(n, vector<bool>(m, false));
  dst.resize(n, vector<int>(m, -1));
  priority_queue<tuple<int, int, int>> que;
  dst[x[0] - 1][y[0] - 1] = 0;
  que.push(make_tuple(0, x[0] - 1, y[0] - 1));
  int xx[] = {-1, 0, 1, 0}, yy[] = {0, -1, 0, 1};
  while (!que.empty()) {
    int a, b, c;
    tie(c, a, b) = que.top();
    que.pop();
    if (vis[a][b])
      continue;
    vis[a][b] = true;
    for (int i = 0; i < 4; i++) {
      int aa = a, bb = b;
      for (int j = 0; j < k; j++) {
        aa += xx[i], bb += yy[i];
        if (bounds(aa, bb) &&
            (dst[aa][bb] == -1 || dst[aa][bb] >= dst[a][b] + 1)) {
          dst[aa][bb] = dst[a][b] + 1;
          que.push(make_tuple(-dst[aa][bb], aa, bb));
        } else
          break;
      }
    }
  }
  cout << dst[x[1] - 1][y[1] - 1] << "\\n";
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

int n, m, k, x[2], y[2];
string adj[1000000];
vector<vector<int>> dst;
vector<vector<bool>> vis;

bool bounds(int a, int b) {
  return (a >= 0 && a < n && b >= 0 && b < m && adj[a][b] == '.' && !vis[a][b]);
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n >> m >> k;
  for (int i = 0; i < 2; i++)
    cin >> x[i] >> y[i];
  for (int i = 0; i < n; i++)
    cin >> adj[i];
  vis.resize(n, vector<bool>(m, false));
  dst.resize(n, vector<int>(m, -1));
  priority_queue<tuple<int, int, int>> que;
  dst[x[0] - 1][y[0] - 1] = 0;
  que.push(make_tuple(0, x[0] - 1, y[0] - 1));
  int xx[] = {-1, 0, 1, 0}, yy[] = {0, -1, 0, 1};
  while (!que.empty()) {
    int a, b, c;
    tie(c, a, b) = que.top();
    que.pop();
    if (vis[a][b])
      continue;
    vis[a][b] = true;
    for (int i = 0; i < 4; i++) {
      int aa = a, bb = b;
      for (int j = 0; j < k; j++) {
        aa += xx[i], bb += yy[i];
        if (bounds(aa, bb) &&
            (dst[aa][bb] == -1 || dst[aa][bb] > dst[a][b] + 1)) {
          dst[aa][bb] = dst[a][b] + 1;
          que.push(make_tuple(-dst[aa][bb], aa, bb));
        } else if (!bounds(aa, bb))
          break;
      }
    }
  }
  cout << dst[x[1] - 1][y[1] - 1] << "\\n";
  return 0;
}
Problem p02644 (C++) — buggy submission, verdict: Time Limit Exceeded

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

const int N = 5000005;
int n, m, k, a[N], dis[N], xs, ys, xt, yt;
int xy[4][2] = {0, 1, 1, 0, 0, -1, -1, 0};
char str[N];

void bfs() {
  memset(dis, -1, sizeof(dis));
  queue<pair<int, int>> q;
  q.push(make_pair(xs, ys));
  dis[xs * m - m + ys] = 0;
  while (!q.empty()) {
    int xu = q.front().first;
    int yu = q.front().second;
    int u = xu * m - m + yu;
    q.pop();
    //		cout<<xu<<' '<<yu<<' '<<dis[u]<<endl;
    for (int i = 0; i < 4; i++) {
      for (int j = 1; j <= k; j++) {

        int xv = xu + xy[i][0] * j;
        int yv = yu + xy[i][1] * j;
        int v = xv * m - m + yv;
        //				cout<<xv<<' '<<yv<<' '<<v<<endl;
        if (xv < 1 || xv > n || yv < 1 || yv > m)
          break;
        if (!a[v] || dis[v] <= dis[u] && dis[v] > -1)
          break;
        if (dis[v] > -1)
          continue;
        dis[v] = dis[u] + 1, q.push(make_pair(xv, yv));
      }
    }
  }
}

int main() {
  scanf("%d%d%d", &n, &m, &k);
  scanf("%d%d%d%d", &xs, &ys, &xt, &yt);
  for (int i = 1; i <= n; i++) {
    scanf("%s", str + 1);
    for (int j = 1; j <= m; j++) {
      a[i * m - m + j] = (str[j] == '.');
    }
  }
  bfs();
  printf("%d\\n", dis[xt * m - m + yt]);
}

Error: TLE

Fixed submission:

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

const int N = 5000005;
int n, m, k, a[N], dis[N], xs, ys, xt, yt;
int xy[4][2] = {0, 1, 1, 0, 0, -1, -1, 0};
char str[N];

void bfs() {
  memset(dis, -1, sizeof(dis));
  queue<pair<int, int>> q;
  q.push(make_pair(xs, ys));
  dis[xs * m - m + ys] = 0;
  while (!q.empty()) {
    int xu = q.front().first;
    int yu = q.front().second;
    int u = xu * m - m + yu;
    q.pop();
    //		cout<<xu<<' '<<yu<<' '<<dis[u]<<endl;
    for (int i = 0; i < 4; i++) {
      for (int j = 1; j <= k; j++) {

        int xv = xu + xy[i][0] * j;
        int yv = yu + xy[i][1] * j;
        int v = xv * m - m + yv;
        //				cout<<xv<<' '<<yv<<' '<<v<<endl;
        if (xv < 1 || xv > n || yv < 1 || yv > m)
          break;
        if (!a[v] || dis[v] <= dis[u] && dis[v] > -1)
          break;
        dis[v] = dis[u] + 1, q.push(make_pair(xv, yv));
      }
    }
  }
}

int main() {
  scanf("%d%d%d", &n, &m, &k);
  scanf("%d%d%d%d", &xs, &ys, &xt, &yt);
  for (int i = 1; i <= n; i++) {
    scanf("%s", str + 1);
    for (int j = 1; j <= m; j++) {
      a[i * m - m + j] = (str[j] == '.');
    }
  }
  bfs();
  printf("%d\\n", dis[xt * m - m + yt]);
}
Problem p02644 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

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

int n, m, k;
char row[1000'010];
int xs, ys, xe, ye;
vector<vector<char>> board;
vector<vector<int>> dist;

int main() {
  scanf("%d%d%d", &n, &m, &k);
  scanf("%d%d%d%d", &xs, &ys, &xe, &ye);
  board = vector<vector<char>>(n + 2, vector<char>(m + 2, '@'));
  dist = vector<vector<int>>(n + 2, vector<int>(m + 2, INF));
  for (int i = 1; i <= n; i++) {
    scanf("%s", &row[1]);
    for (int j = 1; j <= m; j++)
      board[i][j] = row[j];
  }

  dist[xs][ys] = 0;
  queue<pair<int, int>> q;
  q.push({xs, ys});
  while (!q.empty()) {
    int x = q.front().first;
    int y = q.front().second;
    q.pop();
    for (int i = 0; i < 4; i++) {
      for (int j = 1; j <= k; j++) {
        int xx = x + dx[i] * j;
        int yy = y + dy[i] * j;
        if (board[xx][yy] == '@' || dist[xx][yy] <= dist[x][y])
          break;
        if (dist[xx][yy] == dist[x][y] + 1)
          continue;
        dist[xx][yy] = dist[x][y] + 1;
        q.push({xx, yy});
      }
    }
  }
  int ans = dist[xe][ye];
  if (ans == INF)
    ans = -1;
  printf("%d\\n", ans);
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

const int INF = 1e9;

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> c(h);
  for (int i = 0; i < h; i++)
    cin >> c[i];
  vector<vector<int>> dp(h, vector<int>(w, INF));
  queue<pair<pair<int, int>, int>> q;
  q.push({{x1, y1}, 0});
  while (!q.empty()) {
    auto e = q.front();
    q.pop();
    int x = e.first.first, y = e.first.second;
    int dist = e.second;
    if (dp[x][y] <= dist)
      continue;
    dp[x][y] = dist;
    for (int i = 1; i <= k; i++) {
      if (x + i == h || c[x + i][y] == '@')
        break;
      if (dp[x + i][y] > dist + 1)
        q.push({{x + i, y}, dist + 1});
    }
    for (int i = 1; i <= k; i++) {
      if (x - i < 0 || c[x - i][y] == '@')
        break;
      if (dp[x - i][y] > dist + 1)
        q.push({{x - i, y}, dist + 1});
    }
    for (int i = 1; i <= k; i++) {
      if (y + i == w || c[x][y + i] == '@')
        break;
      if (dp[x][y + i] > dist + 1)
        q.push({{x, y + i}, dist + 1});
    }
    for (int i = 1; i <= k; i++) {
      if (y - i < 0 || c[x][y - i] == '@')
        break;
      if (dp[x][y - i] > dist + 1)
        q.push({{x, y - i}, dist + 1});
    }
  }
  if (dp[x2][y2] == INF)
    cout << -1 << "\\n";
  else
    cout << dp[x2][y2] << "\\n";
}