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

#include <bits/stdc++.h>
using namespace std;
int H, W, K, Sx, Sy, Ex, Ey;
vector<vector<bool>> Map;
vector<bool> MapTmp;
vector<vector<int>> Pace;
vector<int> PaceTmp;
typedef pair<int, int> pii;
queue<pii> Q;

inline bool alw(int x, int y) {
  if (x <= 0 || y <= 0 || x > H || y > W)
    return 0;
  return Map[x][y];
}

inline int bfs() {
  static int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
  Q.push(pii(Sx, Sy));
  Pace[Sx][Sy] = 0;
  Map[Sx][Sy] = 0;
  while (Q.size()) {
    int x = Q.front().first, y = Q.front().second;
    Q.pop();

    for (int i = 0; i < 4; i++)
      for (int t = 1, X = x + dx[i], Y = y + dy[i]; t <= K;
           t++, X += dx[i], Y += dy[i])
        if (alw(X, Y) && (Pace[X][Y] == Pace[x][y] + 1 || Pace[X][Y] == -1)) {
          if (Pace[X][Y] != -1)
            continue;
          Q.push(pii(X, Y));
          Pace[X][Y] = Pace[x][y] + 1;
        } else
          break;
  }

  return Pace[Ex][Ey];
}

inline bool isLeaf() {
  char c = getchar();
  while (c != '@' && c != '.')
    c = getchar();
  return c == '@';
}
int main() {
  cin >> H >> W >> K;
  cin >> Sx >> Sy >> Ex >> Ey;
  for (int j = 0; j <= W; j++)
    MapTmp.push_back(0), PaceTmp.push_back(-1);
  Map.push_back(MapTmp);
  Pace.push_back(PaceTmp);
  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++)
      MapTmp[j] = !isLeaf();
    Map.push_back(MapTmp);
    Pace.push_back(PaceTmp);
  }
  cout << bfs();
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int H, W, K, Sx, Sy, Ex, Ey;
vector<vector<bool>> Map;
vector<bool> MapTmp;
vector<vector<int>> Pace;
vector<int> PaceTmp;
typedef pair<int, int> pii;
queue<pii> Q;

inline bool alw(int x, int y) {
  if (x <= 0 || y <= 0 || x > H || y > W)
    return 0;
  return Map[x][y];
}

inline int bfs() {
  static int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
  Q.push(pii(Sx, Sy));
  Pace[Sx][Sy] = 0;
  Map[Sx][Sy] = 0;
  while (Q.size()) {
    int x = Q.front().first, y = Q.front().second, P = Pace[x][y];
    Q.pop();

    for (int i = 0; i < 4; i++)
      for (int t = 1, X = x + dx[i], Y = y + dy[i]; t <= K;
           t++, X += dx[i], Y += dy[i])
        if (alw(X, Y) && (Pace[X][Y] == Pace[x][y] + 1 || Pace[X][Y] == -1)) {
          Q.push(pii(X, Y));
          Pace[X][Y] = Pace[x][y] + 1;
        } else
          break;
  }

  return Pace[Ex][Ey];
}

inline bool isLeaf() {
  char c = getchar();
  while (c != '@' && c != '.')
    c = getchar();
  return c == '@';
}
int main() {
  cin >> H >> W >> K;
  cin >> Sx >> Sy >> Ex >> Ey;
  for (int j = 0; j <= W; j++)
    MapTmp.push_back(0), PaceTmp.push_back(-1);
  Map.push_back(MapTmp);
  Pace.push_back(PaceTmp);
  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++)
      MapTmp[j] = !isLeaf();
    Map.push_back(MapTmp);
    Pace.push_back(PaceTmp);
  }
  cout << bfs();
  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<tuple<ll, ll, ll, ll>> Q;
vector<vector<pair<ll, ll>>> D;
const ll INF = 1ll << 60;

ll &at(pair<ll, ll> &ab, ll c) { return c == 0 ? ab.first : ab.second; }
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, INF});
  }
  Q.push({SX, SY, 0, 0});
  while (!Q.empty()) {
    ll x, y, c, d;
    tie(x, y, c, d) = Q.front();
    Q.pop();
    if (at(D[x][y], c) != INF)
      continue;
    at(D[x][y], c) = d;
    for (ll c2 : {0, 1}) {
      for (ll s : {1, -1}) {
        ll dx = c2 ? 0 : s, dy = c2 ? s : 0;
        ll x2 = x, y2 = y;
        for (ll i = 1; i <= K; ++i) {
          x2 += dx;
          y2 += dy;
          if (L[x2][y2])
            break;
          if (at(D[x2][y2], c2) != INF)
            break;
          Q.push({x2, y2, c2, d + 1});
        }
      }
    }
  }
  ll res = min(D[TX][TY].first, D[TX][TY].second);
  cout << (res == INF ? -1 : res) << endl;
  return 0;
}
Problem p02644 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

int32_t main() {
  ios::sync_with_stdio(0), cin.tie(0);
  int h, w, k;
  cin >> h >> w >> k;
  int a, b, c, d;
  cin >> a >> b >> c >> d;
  --a, --b, --c, --d;
  int l[h][w];
  memset(l, -1, sizeof l);
  char g[h][w];
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++)
      cin >> g[i][j];
  }
  queue<pair<int, int>> q;
  q.push({a, b}), l[a][b] = 0;
  while (!q.empty()) {
    a = q.front().first, b = q.front().second;
    q.pop();
    for (int i = 0; i < 4; i++) {
      for (int j = 1; j <= k; j++) {
        int x = a + j * dx[i], y = b + j * dy[i];
        if (x < 0 or x >= h or y < 0 or y >= w or g[x][y] == '@')
          break;
        if (l[x][y] == -1) {
          q.push({x, y});
          l[x][y] = l[a][b] + 1;
        }
        if (l[x][y] < l[a][b] + 1)
          break;
        l[x][y] = min(l[x][y], l[a][b] + 1);
      }
    }
  }
  cout << l[c][d];
}


Error: TLE

Fixed submission:

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

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

int32_t main() {
  ios::sync_with_stdio(0), cin.tie(0);
  int h, w, k;
  cin >> h >> w >> k;
  int a, b, c, d;
  cin >> a >> b >> c >> d;
  --a, --b, --c, --d;
  int l[h][w];
  memset(l, -1, sizeof l);
  char g[h][w];
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++)
      cin >> g[i][j];
  }
  queue<pair<int, int>> q;
  q.push({a, b}), l[a][b] = 0;
  while (!q.empty()) {
    a = q.front().first, b = q.front().second;
    q.pop();
    for (int i = 0; i < 4; i++) {
      for (int j = 1; j <= k; j++) {
        int x = a + j * dx[i], y = b + j * dy[i];
        if (x < 0 or x >= h or y < 0 or y >= w or g[x][y] == '@')
          break;
        if (l[x][y] == -1) {
          q.push({x, y});
          l[x][y] = l[a][b] + 1;
        }
        l[x][y] = min(l[x][y], l[a][b] + 1);
      }
    }
  }
  cout << l[c][d];
}
Problem p02644 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <queue>
#include <string>
#include <utility>
#include <vector>

typedef long long ll;
const ll INF = 1ll << 60;

int main() {
  int h, w, k;
  std::cin >> h >> w >> k;
  int dx[4] = {-1, 0, 1, 0};
  int dy[4] = {0, -1, 0, 1};
  std::pair<int, int> star, goal;
  std::cin >> star.first >> star.second >> goal.first >> goal.second;
  --star.first;
  --star.second;
  --goal.first;
  --goal.second;
  std::vector<std::string> c(h);
  for (int i = 0; i < h; i++) {
    std::cin >> c[i];
  }

  std::vector<std::vector<ll>> dist(h, std::vector<ll>(w, INF));
  dist[star.first][star.second] = 0;
  std::queue<std::pair<int, int>> q;
  q.push(star);
  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 nx = x + j * dx[i];
        int ny = y + j * dy[i];
        if (nx < 0 || nx >= h || ny < 0 || ny >= w)
          break;
        if (c[nx][ny] == '@' || dist[nx][ny] <= dist[x][y])
          break;
        if (dist[nx][ny] == INF) {
          dist[nx][ny] = dist[x][y] + 1;
          q.push({nx, ny});
        }
      }
    }
  }

  if (dist[goal.first][goal.second] == INF) {
    std::cout << -1 << std::endl;
  } else {
    std::cout << dist[goal.first][goal.second] << std::endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <queue>
#include <string>
#include <utility>
#include <vector>

typedef long long ll;
const ll INF = 1ll << 60;

int main() {
  int h, w, k;
  std::cin >> h >> w >> k;
  int dx[4] = {-1, 0, 1, 0};
  int dy[4] = {0, -1, 0, 1};
  std::pair<int, int> star, goal;
  std::cin >> star.first >> star.second >> goal.first >> goal.second;
  --star.first;
  --star.second;
  --goal.first;
  --goal.second;
  std::vector<std::string> c(h);
  for (int i = 0; i < h; i++) {
    std::cin >> c[i];
  }

  std::vector<std::vector<ll>> dist(h, std::vector<ll>(w, INF));
  dist[star.first][star.second] = 0;
  std::queue<std::pair<int, int>> q;
  q.push(star);
  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 nx = x + j * dx[i];
        int ny = y + j * dy[i];
        if (nx < 0 || nx >= h || ny < 0 || ny >= w)
          continue;
        if (c[nx][ny] == '@')
          break;
        if (dist[nx][ny] > dist[x][y] + 1) {
          dist[nx][ny] = dist[x][y] + 1;
          q.push({nx, ny});
        }
      }
    }
  }

  if (dist[goal.first][goal.second] == INF) {
    std::cout << -1 << std::endl;
  } else {
    std::cout << dist[goal.first][goal.second] << std::endl;
  }
  return 0;
}
Problem p02644 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <utility>
#include <vector>
using namespace std;
int inf = 1e9;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};

int main() {
  int h, w, k;
  cin >> h >> w >> k;
  int sx, sy, gx, gy;
  cin >> sx >> sy >> gx >> gy;
  vector<vector<char>> grid(h + 2, vector<char>(w + 2, '@'));
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      cin >> grid[i][j];
    }
  }
  vector<vector<int>> mincost(h + 2, vector<int>(w + 2, inf));
  mincost[sx][sy] = 0;
  queue<pair<pair<int, int>, int>> wait;
  wait.push({{sx, sy}, 0});
  while (!wait.empty()) {
    int x = wait.front().first.first;
    int y = wait.front().first.second;
    int cost = wait.front().second;
    wait.pop();
    if (cost > mincost[x][y])
      continue;
    for (int d = 0; d < 4; d++) {
      for (int i = 1; i <= k; i++) {
        int nx = x + i * dx[d];
        int ny = y + i * dy[d];
        if (grid[nx][ny] == '@')
          break;
        if (mincost[nx][ny] < cost + 1)
          break;
        if (mincost[nx][ny] > cost + 1) {
          mincost[nx][ny] = cost + 1;
          wait.push({{nx, ny}, cost + 1});
        }
      }
    }
  }
  if (mincost[gx][gy] == inf) {
    cout << -1 << endl;
  } else {
    cout << mincost[gx][gy] << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <utility>
#include <vector>
using namespace std;
int inf = 1e9;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};

int main() {
  int h, w, k;
  cin >> h >> w >> k;
  int sx, sy, gx, gy;
  cin >> sx >> sy >> gx >> gy;
  vector<vector<char>> grid(h + 2, vector<char>(w + 2, '@'));
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      cin >> grid[i][j];
    }
  }
  vector<vector<int>> mincost(h + 2, vector<int>(w + 2, inf));
  mincost[sx][sy] = 0;
  queue<pair<int, int>> wait;
  wait.push({sx, sy});
  while (!wait.empty()) {
    int x = wait.front().first;
    int y = wait.front().second;
    int cost = mincost[x][y];
    wait.pop();
    for (int d = 0; d < 4; d++) {
      for (int i = 1; i <= k; i++) {
        int nx = x + i * dx[d];
        int ny = y + i * dy[d];
        if (grid[nx][ny] == '@')
          break;
        if (mincost[nx][ny] < cost + 1)
          break;
        mincost[nx][ny] = cost + 1;
        wait.push({nx, ny});
      }
    }
  }
  if (mincost[gx][gy] == inf) {
    cout << -1 << endl;
  } else {
    cout << mincost[gx][gy] << endl;
  }
  return 0;
}
Problem p02644 (C++) — buggy submission, verdict: Time Limit Exceeded

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(v) (v).begin(), (v).end()
using ll = long long;
using P = pair<int, int>;
constexpr int INF = 1e9;
constexpr long long LINF = 1e18;
constexpr long long MOD = 1e9 + 7;

signed main() {
  int h, w, k;
  cin >> h >> w >> k;
  int x[2], y[2];
  cin >> x[0] >> y[0] >> x[1] >> y[1];
  x[0]--;
  x[1]--;
  y[0]--;
  y[1]--;
  string c[h];
  rep(i, h) { cin >> c[i]; }
  const int dy[] = {0, 1, 0, -1}, dx[] = {1, 0, -1, 0};
  int dist[h][w];
  fill(dist[0], dist[h], INF);
  queue<pair<int, int>> que;
  que.emplace(x[0], y[0]);
  dist[x[0]][y[0]] = 0;
  while (!que.empty()) {
    pair<int, int> p = que.front();
    que.pop();
    for (int i = 0; i < 4; i++) {
      for (int j = 1; j <= k; j++) {
        int ny = p.first + j * dy[i], nx = p.second + j * dx[i];
        if (ny < 0 || h <= ny || nx < 0 || w <= nx || c[ny][nx] == '@')
          break;
        if (dist[ny][nx] < dist[p.first][p.second] + 1)
          break;
        if (dist[ny][nx] != INF)
          continue;
        que.emplace(ny, nx);
        dist[ny][nx] = dist[p.first][p.second] + 1;
      }
    }
  }
  cout << (dist[x[1]][y[1]] != INF ? dist[x[1]][y[1]] : -1) << endl;
  return 0;
}

Error: TLE

Fixed submission:

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(v) (v).begin(), (v).end()
using ll = long long;
using P = pair<int, int>;
constexpr int INF = 1e9;
constexpr long long LINF = 1e18;
constexpr long long MOD = 1e9 + 7;

signed main() {
  int h, w, k;
  cin >> h >> w >> k;
  int x[2], y[2];
  cin >> x[0] >> y[0] >> x[1] >> y[1];
  x[0]--;
  x[1]--;
  y[0]--;
  y[1]--;
  string c[h];
  rep(i, h) { cin >> c[i]; }
  const int dy[] = {0, 1, 0, -1}, dx[] = {1, 0, -1, 0};
  int dist[h][w];
  fill(dist[0], dist[h], INF);
  queue<pair<int, int>> que;
  que.emplace(x[0], y[0]);
  dist[x[0]][y[0]] = 0;
  while (!que.empty()) {
    pair<int, int> p = que.front();
    que.pop();
    for (int i = 0; i < 4; i++) {
      for (int j = 1; j <= k; j++) {
        int ny = p.first + j * dy[i], nx = p.second + j * dx[i];
        if (ny < 0 || h <= ny || nx < 0 || w <= nx || c[ny][nx] == '@')
          break;
        if (dist[ny][nx] != INF)
          continue;
        if (dist[ny][nx] < dist[p.first][p.second] + 1)
          break;
        que.emplace(ny, nx);
        dist[ny][nx] = dist[p.first][p.second] + 1;
      }
    }
  }
  cout << (dist[x[1]][y[1]] != INF ? dist[x[1]][y[1]] : -1) << endl;
  return 0;
}
Problem p02644 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define endl '\\n'
#define pi pair<int, int>
#define ff first
#define ss second
#define boost                                                                  \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(nullptr)
#include "string"

const int N = 2e5 + 5;
int xx[4] = {0, 0, 1, -1};
int yy[4] = {1, -1, 0, 0};

int32_t main() {
  boost;
  int n, m, k;
  cin >> n >> m >> k;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  char c[n + 1][m + 1];
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++)
      cin >> c[i][j];
  }
  int dp[n + 1][m + 1];
  memset(dp, 1, sizeof dp);
  dp[x1][y1] = 0;
  queue<array<int, 2>> q;
  q.push({x1, y1});
  while (!q.empty()) {
    array<int, 2> p = q.front();
    q.pop();
    if (p[0] == x2 && p[1] == y2)
      break;
    for (int i = 0; i < 4; i++) {
      for (int j = 1; j <= k; j++) {
        int x = p[0] + xx[i] * j;
        int y = p[1] + yy[i] * j;
        if (x < 1 or x > n or y < 1 or y > m or c[x][y] == '@')
          break;
        if (dp[x][y] < dp[p[0]][p[1]] + 1)
          break;
        if (dp[x][y] > dp[p[0]][p[1]] + 1) {
          dp[x][y] = dp[p[0]][p[1]] + 1;
          q.push({x, y});
        }
      }
    }
  }
  if (dp[x2][y2] == dp[0][0])
    cout << -1 << endl;
  else
    cout << dp[x2][y2] << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define endl '\\n'
#define pi pair<int, int>
#define ff first
#define ss second
#define boost                                                                  \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(nullptr)
#include "string"

const int N = 2e5 + 5;
int xx[4] = {0, 0, 1, -1};
int yy[4] = {1, -1, 0, 0};

int32_t main() {
  boost;
  int n, m, k;
  cin >> n >> m >> k;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  char c[n + 1][m + 1];
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++)
      cin >> c[i][j];
  }
  int dp[n + 1][m + 1];
  memset(dp, 1, sizeof dp);
  dp[x1][y1] = 0;
  queue<array<int, 2>> q;
  q.push({x1, y1});
  while (!q.empty()) {
    array<int, 2> p = q.front();
    q.pop();
    if (p[0] == x2 && p[1] == y2)
      break;
    for (int i = 0; i < 4; i++) {
      for (int j = 1; j <= k; j++) {
        int x = p[0] + xx[i] * j;
        int y = p[1] + yy[i] * j;
        if (x < 1 or x > n or y < 1 or y > m or c[x][y] == '@')
          break;
        if (dp[x][y] != dp[0][0])
          continue;
        if (dp[x][y] > dp[p[0]][p[1]] + 1) {
          dp[x][y] = dp[p[0]][p[1]] + 1;
          q.push({x, y});
        }
      }
    }
  }
  if (dp[x2][y2] == dp[0][0])
    cout << -1 << endl;
  else
    cout << dp[x2][y2] << endl;
}
Problem p02644 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define endl '\\n'
#define pi pair<int, int>
#define ff first
#define ss second
#define boost                                                                  \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(nullptr)
#include "string"

const int N = 2e5 + 5;
int xx[4] = {0, 0, 1, -1};
int yy[4] = {1, -1, 0, 0};

int32_t main() {
  boost;
  int n, m, k;
  cin >> n >> m >> k;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  char c[n + 1][m + 1];
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++)
      cin >> c[i][j];
  }
  int dp[n + 1][m + 1];
  memset(dp, 1, sizeof dp);
  dp[x1][y1] = 0;
  queue<array<int, 2>> q;
  q.push({x1, y1});
  while (!q.empty()) {
    array<int, 2> p = q.front();
    q.pop();
    if (p[0] == x2 && p[1] == y2)
      break;
    for (int i = 0; i < 4; i++) {
      for (int j = 1; j <= k; j++) {
        int x = p[0] + xx[i] * j;
        int y = p[1] + yy[i] * j;
        if (x < 1 or x > n or y < 1 or y > m or c[x][y] == '@')
          break;
        if (dp[x][y] > dp[p[0]][p[1]] + 1) {
          dp[x][y] = dp[p[0]][p[1]] + 1;
          q.push({x, y});
        } else if (dp[x][y] == dp[p[0]][p[1]])
          break;
      }
    }
  }
  if (dp[x2][y2] == dp[0][0])
    cout << -1 << endl;
  else
    cout << dp[x2][y2] << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define endl '\\n'
#define pi pair<int, int>
#define ff first
#define ss second
#define boost                                                                  \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(nullptr)
#include "string"

const int N = 2e5 + 5;
int xx[4] = {0, 0, 1, -1};
int yy[4] = {1, -1, 0, 0};

int32_t main() {
  boost;
  int n, m, k;
  cin >> n >> m >> k;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  char c[n + 1][m + 1];
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++)
      cin >> c[i][j];
  }
  int dp[n + 1][m + 1];
  memset(dp, 1, sizeof dp);
  dp[x1][y1] = 0;
  queue<array<int, 2>> q;
  q.push({x1, y1});
  while (!q.empty()) {
    array<int, 2> p = q.front();
    q.pop();
    if (p[0] == x2 && p[1] == y2)
      break;
    for (int i = 0; i < 4; i++) {
      for (int j = 1; j <= k; j++) {
        int x = p[0] + xx[i] * j;
        int y = p[1] + yy[i] * j;
        if (x < 1 or x > n or y < 1 or y > m or c[x][y] == '@')
          break;
        if (dp[x][y] >= dp[p[0]][p[1]] + 1) {
          dp[x][y] = dp[p[0]][p[1]] + 1;
          q.push({x, y});
        } else
          break;
      }
    }
  }
  if (dp[x2][y2] == dp[0][0])
    cout << -1 << endl;
  else
    cout << dp[x2][y2] << endl;
}
Problem p02644 (C++) — buggy submission, verdict: Time Limit Exceeded

//
// Created by 10034 on 2020/7/30.
//

#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;
const int N = 1000010;
typedef pair<int, int> PII;

int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
int main() {
  int n, m, k, x1, x2, y1, y2;
  cin >> n >> m >> k;
  vector<string> g(n);
  vector<vector<int>> d(n, vector<int>(m, 1e9));
  queue<PII> q;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--, y1--, x2--, y2--;
  d[x1][y1] = 0;
  for (int i = 0; i < n; ++i) {
    cin >> g[i];
  }
  q.push({x1, y1});
  while (q.size()) {
    int xx = q.front().first, yy = q.front().second;
    q.pop();
    for (int i = 0; i < 4; ++i) {
      for (int j = 1; j <= k; ++j) {
        int nx = xx + dx[i] * j, ny = yy + dy[i] * j;
        if (nx < 0 || ny < 0 || nx >= n || ny >= m ||
            g[nx][ny] == '@' | d[nx][ny] <= d[xx][yy])
          break;
        if (d[nx][ny] > d[xx][yy] + 1) {
          d[nx][ny] = d[xx][yy] + 1;
          q.push({nx, ny});
        }
        if (nx == x2 && ny == y2) {
          cout << d[nx][ny] << endl;
          return 0;
        }
      }
    }
  }
  puts("-1");

  return 0;
}

Error: TLE

Fixed submission:

//
// Created by 10034 on 2020/7/30.
//

#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;
const int N = 1000010;
typedef pair<int, int> PII;

int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
int main() {
  int n, m, k, x1, x2, y1, y2;
  cin >> n >> m >> k;
  vector<string> g(n);
  vector<vector<int>> d(n, vector<int>(m, 1e9));
  queue<PII> q;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--, y1--, x2--, y2--;
  d[x1][y1] = 0;
  for (int i = 0; i < n; ++i) {
    cin >> g[i];
  }
  q.push({x1, y1});
  while (q.size()) {
    int xx = q.front().first, yy = q.front().second;
    q.pop();
    for (int i = 0; i < 4; ++i) {
      for (int j = 1; j <= k; ++j) {
        int nx = xx + dx[i] * j, ny = yy + dy[i] * j;
        if (nx < 0 || ny < 0 || nx >= n || ny >= m ||
            g[nx][ny] == '@' | d[nx][ny] <= d[xx][yy])
          break;
        if (d[nx][ny] >= d[xx][yy] + 1) {
          d[nx][ny] = d[xx][yy] + 1;
          q.push({nx, ny});
        }
        if (nx == x2 && ny == y2) {
          cout << d[nx][ny] << endl;
          return 0;
        }
      }
    }
  }
  if (d[x2][y2] == 1e9)
    puts("-1");
  else
    cout << d[x2][y2] << endl;

  return 0;
}