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

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
typedef long double ld;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define F first
#define S second
constexpr char ln = '\\n';

const int mx = 200010;
const ll mod = 1e9 + 7;

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

int main() {
  int h, w, k;
  cin >> h >> w >> k;
  int sx, sy, gx, gy;
  cin >> sx >> sy >> gx >> gy;
  sx--;
  sy--;
  gx--;
  gy--;
  vector<string> c(h);
  rep(i, h) { cin >> c[i]; }

  vector<vector<int>> d(h, vector<int>(w, inf));
  queue<P> q;
  q.push(P(sx, sy));
  d[sx][sy] = 0;
  c[sx][sy] = '@';
  while (!q.empty()) {
    auto p = q.front();
    q.pop();
    c[p.F][p.S] = '@';
    if (p == P(gx, gy)) {
      break;
    }
    rep(i, 4) {
      REP(j, 1, k + 1) {
        int nx = p.F + dx[i] * j, ny = p.S + dy[i] * j;
        if (nx < 0 || h <= nx || ny < 0 || w <= ny)
          break;
        if (c[nx][ny] == '@') {
          break;
        }
        if (d[nx][ny] <= d[p.F][p.S])
          break;
        if (d[nx][ny] > d[p.F][p.S] + 1) {
          d[nx][ny] = d[p.F][p.S] + 1;
          q.push(P(nx, ny));
        }
      }
    }
  }
  int ans = d[gx][gy];
  if (ans == inf)
    ans = -1;
  cout << ans << ln;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
typedef long double ld;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define F first
#define S second
constexpr char ln = '\\n';

const int mx = 200010;
const ll mod = 1e9 + 7;

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

int main() {
  int h, w, k;
  cin >> h >> w >> k;
  int sx, sy, gx, gy;
  cin >> sx >> sy >> gx >> gy;
  sx--;
  sy--;
  gx--;
  gy--;
  vector<string> c(h);
  rep(i, h) { cin >> c[i]; }

  vector<vector<int>> d(h, vector<int>(w, inf));
  queue<P> q;
  q.push(P(sx, sy));
  d[sx][sy] = 0;
  c[sx][sy] = '@';
  while (!q.empty()) {
    auto p = q.front();
    q.pop();
    c[p.F][p.S] = '@';
    if (p == P(gx, gy)) {
      break;
    }
    rep(i, 4) {
      REP(j, 1, k + 1) {
        int nx = p.F + dx[i] * j, ny = p.S + dy[i] * j;
        if (nx < 0 || h <= nx || ny < 0 || w <= ny)
          break;
        if (c[nx][ny] == '@') {
          break;
        }
        if (d[nx][ny] <= d[p.F][p.S])
          break;
        if (d[nx][ny] < d[p.F][p.S] + 1)
          continue;

        d[nx][ny] = d[p.F][p.S] + 1;
        q.push(P(nx, ny));
      }
    }
  }
  int ans = d[gx][gy];
  if (ans == inf)
    ans = -1;
  cout << ans << ln;
  return 0;
}
Problem p02644 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define ll long long
#define ss second
#define ff first
#define pb push_back
using namespace std;

const int N = 1e6 + 6;

int n, m, k;
int x1, x2, Y1, y2;

int X[4] = {0, 0, 1, -1};
int Y[4] = {1, -1, 0, 0};

int main() {

  cin >> n >> m >> k;
  cin >> x1 >> Y1 >> x2 >> y2;

  char A[n + 1][m + 1];
  int D[n + 1][m + 1], P[n + 1][m + 1][4];
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      D[i][j] = 1e9;
      for (int t = 0; t < 4; t++)
        P[i][j][t] = 0;
      cin >> A[i][j];
    }
  }

  queue<pair<int, int>> q;
  q.push({x1, Y1});
  D[x1][Y1] = 0;

  while (q.size()) {

    auto p = q.front();
    q.pop();
    int x = p.ff;
    int y = p.ss;
    // if(fix[x][y])continue;
    // cout << x << " " << y << endl;
    for (int j = 0; j < 4; j++) {
      for (int i = 1; i <= k; i++) {
        int nx = x + X[j] * i;
        int ny = y + Y[j] * i;
        if (nx < 1 || nx > n || ny < 1 || ny > m)
          break;
        if ((A[nx][ny] == '@'))
          break;
        if (D[nx][ny] < D[x][y] + 1)
          break;
        if (D[nx][ny] == 1e9)
          q.push({nx, ny});
        if (P[nx][ny][j])
          break;
        D[nx][ny] = D[x][y] + 1;
        P[nx][ny][j] = 1;
      }
    }
  }

  if (D[x2][y2] == 1e9)
    D[x2][y2] = -1;

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


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define ss second
#define ff first
#define pb push_back
using namespace std;

const int N = 1e6 + 6;

int n, m, k;
int x1, x2, Y1, y2;

int X[4] = {0, 0, 1, -1};
int Y[4] = {1, -1, 0, 0};

int main() {

  cin >> n >> m >> k;
  cin >> x1 >> Y1 >> x2 >> y2;

  char A[n + 1][m + 1];
  int D[n + 1][m + 1];
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      D[i][j] = 1e9;
      cin >> A[i][j];
    }
  }

  queue<pair<int, int>> q;
  q.push({x1, Y1});
  D[x1][Y1] = 0;

  while (q.size()) {

    auto p = q.front();
    q.pop();
    int x = p.ff;
    int y = p.ss;
    // cout << x << " " << y << endl;
    for (int j = 0; j < 4; j++) {
      for (int i = 1; i <= k; i++) {
        int nx = x + X[j] * i;
        int ny = y + Y[j] * i;
        if ((A[nx][ny] == '@') || nx < 1 || nx > n || ny < 1 || ny > m)
          break;
        if (D[nx][ny] <= D[x][y] + 1)
          break;
        D[nx][ny] = D[x][y] + 1;
        q.push({nx, ny});
      }
    }
  }

  if (D[x2][y2] == 1e9)
    D[x2][y2] = -1;

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

#include <iostream>
#include <queue>
using namespace std;

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

struct node {
  int x, y;
  node(int a, int b) {
    x = a;
    y = b;
  }
};

int n, m, k;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> m >> k;
  vector<vector<char>> maze(n + 1, vector<char>(m + 1));
  vector<vector<int>> vis(n + 1, vector<int>(m + 1));
  vector<vector<int>> dist(n + 1, vector<int>(m + 1));
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> maze[i][j];
      dist[i][j] = 1e9;
    }
  }
  queue<node> q;
  q.push(node(x1, y1));
  vis[x1][y1] = 1;
  dist[x1][y1] = 0;
  while (!q.empty()) {
    node t = q.front();
    q.pop();
    vector<node> temp;
    for (int i = 0; i < 4; i++) {
      for (int j = 1; j <= k; j++) {
        int newx = t.x + d[i][0] * j;
        int newy = t.y + d[i][1] * j;
        if (newx < 1 || newx > n || newy < 1 || newy > m ||
            dist[newx][newy] < dist[t.x][t.y] + 1 || maze[newx][newy] == '@')
          break;
        if (dist[newx][newy] == 1e9) {
          dist[newx][newy] = dist[t.x][t.y] + 1;
          // temp.push_back(node(newx,newy));
          q.push(node(newx, newy));
        }
      }
    }
    // for (int i = 0; i < temp.size(); i++) vis[temp[i].x][temp[i].y] = 1;
  }
  if (dist[x2][y2] == 1e9)
    cout << -1 << '\\n';
  else
    cout << dist[x2][y2] << '\\n';
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <queue>
using namespace std;

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

struct node {
  int x, y;
  node(int a, int b) {
    x = a;
    y = b;
  }
};

int n, m, k;
int main() {
  // ios::sync_with_stdio(false);
  // cin.tie(0);
  cin >> n >> m >> k;
  vector<vector<char>> maze(n + 1, vector<char>(m + 1));
  vector<vector<int>> vis(n + 1, vector<int>(m + 1));
  vector<vector<int>> dist(n + 1, vector<int>(m + 1));
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> maze[i][j];
      dist[i][j] = 1e9;
    }
  }
  queue<node> q;
  q.push(node(x1, y1));
  vis[x1][y1] = 1;
  dist[x1][y1] = 0;
  while (!q.empty()) {
    node t = q.front();
    q.pop();
    vector<node> temp;
    for (int i = 0; i < 4; i++) {
      for (int j = 1; j <= k; j++) {
        int newx = t.x + d[i][0] * j;
        int newy = t.y + d[i][1] * j;
        if (newx < 1 || newx > n || newy < 1 || newy > m ||
            dist[newx][newy] < dist[t.x][t.y] + 1 || maze[newx][newy] == '@')
          break;
        dist[newx][newy] = dist[t.x][t.y] + 1;
        // temp.push_back(node(newx,newy));
        q.push(node(newx, newy));
      }
    }
    // for (int i = 0; i < temp.size(); i++) vis[temp[i].x][temp[i].y] = 1;
  }
  if (dist[x2][y2] == 1e9)
    cout << -1 << '\\n';
  else
    cout << dist[x2][y2] << '\\n';
  return 0;
}
Problem p02644 (C++) — buggy submission, verdict: Time Limit Exceeded

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

const long inf = 1e18;
int h, w, m;
int si, sj, ti, tj;
vector<string> s;

const int di[] = {1, 0, -1, 0};
const int dj[] = {0, 1, 0, -1};
using PP = pair<long, long>;
using TT = pair<PP, PP>;
long sub() {
  vector<vector<vector<long>>> dist(
      h + 2, vector<vector<long>>(w + 2, vector<long>(4, inf)));
  priority_queue<TT, vector<TT>, greater<TT>> q;
  for (int k = 0; k < 4; ++k) {
    q.push(TT(PP(0, k), PP(si, sj)));
  }
  while (!q.empty()) {
    auto t = q.top();
    q.pop();
    long d = t.first.first;
    int k = t.first.second;
    int i = t.second.first;
    int j = t.second.second;
    if (dist[i][j][k] != inf)
      continue;
    if (s[i][j] == '@')
      continue;
    dist[i][j][k] = d;
    q.push(TT(PP(d + 1, k), PP(i + di[k], j + dj[k])));
    for (int nk = 0; nk < 4; ++nk) {
      if (nk == k)
        continue;
      long nd = (d + m - 1) / m * m;
      q.push(TT(PP(nd, nk), PP(i, j)));
    }
  }

  long mn = inf;
  for (int k = 0; k < 4; ++k) {
    mn = min(mn, dist[ti][tj][k]);
  }
  return mn;
}

int main() {
  cin >> h >> w >> m;
  cin >> si >> sj >> ti >> tj;
  s.push_back(string(w + 2, '@'));
  for (int i = 0; i < h; ++i) {
    string t;
    cin >> t;
    s.push_back('@' + t + '@');
  }
  s.push_back(string(w + 2, '@'));
  long ans = sub();
  cout << (ans == inf ? -1 : (ans + m - 1) / m) << endl;
}

Error: TLE

Fixed submission:

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

const int inf = 1e9;
int h, w, m;
int si, sj, ti, tj;
vector<string> s;

const int di[] = {1, 0, -1, 0};
const int dj[] = {0, 1, 0, -1};
using PP = pair<int, int>;
using TT = pair<int, PP>;
int sub() {
  vector<vector<int>> dist(h + 2, vector<int>(w + 2, inf));
  vector<vector<bool>> hused(h + 2, vector<bool>(w + 2));
  vector<vector<bool>> wused(h + 2, vector<bool>(w + 2));
  queue<TT> q;
  q.push(TT(0, PP(si, sj)));
  while (!q.empty()) {
    auto t = q.front();
    q.pop();
    int d = t.first;
    int i = t.second.first;
    int j = t.second.second;
    if (dist[i][j] != inf)
      continue;
    dist[i][j] = d;
    for (int k = 0; k < 4; ++k) {
      int ni = i;
      int nj = j;
      for (int z = 0; z < m; ++z) {
        ni += di[k];
        nj += dj[k];
        if (s[ni][nj] == '@')
          break;
        if ((k % 2 == 0 ? hused : wused)[ni][nj])
          break;
        q.push(TT(d + 1, PP(ni, nj)));
      }
    }
  }
  return dist[ti][tj];
}

int main() {
  cin >> h >> w >> m;
  cin >> si >> sj >> ti >> tj;
  s.push_back(string(w + 2, '@'));
  for (int i = 0; i < h; ++i) {
    string t;
    cin >> t;
    s.push_back('@' + t + '@');
  }
  s.push_back(string(w + 2, '@'));
  int ans = sub();
  cout << (ans == inf ? -1 : ans) << endl;
}
Problem p02644 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  // 读入数据
  int h, w, k;
  scanf("%d%d%d", &h, &w, &k);
  int x1, y1, x2, y2;
  scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
  // 迷宫描述
  char maze[h][w];
  // 距离描述
  int dis[h][w];
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      scanf(" %c", &maze[i][j]);
      dis[i][j] = INT_MAX;
    }
  }

  // 数据处理
  // 由于我们是从0开始
  x1--;
  y1--;
  x2--;
  y2--;

  const int dir[4][2] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}}; // 方向定义次序无所谓
  queue<pair<int, int>> q;
  // 插入起点
  dis[x1][y1] = 0;
  q.push({x1, y1});
  while (!q.empty()) {
    // 获取队首数据
    int x = q.front().first;
    int y = q.front().second;
    q.pop();

    // 按照规定方向移动
    for (int i = 0; i < 4; i++) {
      // 新坐标和距离
      int nx = x;
      int ny = y;
      int nd = dis[x][y] + 1;
      // 1 ~ k步
      for (int j = 1; j <= k; j++) {
        // 计算新的坐标
        nx += dir[i][0];
        ny += dir[i][1];

        // 合法性判断
        if (nx < 0 || nx >= h || ny < 0 || ny >= w || '@' == maze[nx][ny] ||
            dis[nx][ny] < nd) {
          break;
        }
        if (dis[nx][ny] > nd) {
          dis[nx][ny] = nd;
          q.push({nx, ny});
        }
      }
    }
  }

  int ans = dis[x2][y2];
  printf("%d\\n", (INT_MAX == ans ? -1 : ans));

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int maxn = 1e6 + 5;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, m, k;
  cin >> n >> m >> k;
  std::vector<string> buff(n + 1);
  static int dis[maxn];
  int x1, x2, y1, y2;
  cin >> y1 >> x1 >> y2 >> x2;
  auto cgpos = [&](int y, int x) -> int { return y * m + x; };
  x1--;
  x2--;
  y1--;
  y2--;
  for (int i = 0; i < n; i++)
    cin >> buff[i];
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (buff[i][j] == '@')
        dis[cgpos(i, j)] = 1;
  queue<pii> Q;
  dis[cgpos(y1, x1)] = 1;
  Q.emplace(y1, x1);
  static int dx[] = {0, 0, 1, -1};
  static int dy[] = {1, -1, 0, 0};
  while (!Q.empty()) {
    auto [y, x] = Q.front();
    Q.pop();
    for (int i = 0; i < 4; i++) {
      for (int j = 1; j <= k; j++) {
        auto [ny, nx] = pii{y + dy[i] * j, x + dx[i] * j};
        if (ny < 0 || ny >= n || nx < 0 || nx >= m ||
            (dis[cgpos(ny, nx)] && dis[cgpos(ny, nx)] <= dis[cgpos(y, x)]))
          break;
        dis[cgpos(ny, nx)] = dis[cgpos(y, x)] + 1;
        Q.emplace(ny, nx);
        if (ny == y2 && nx == x2)
          return cout << dis[cgpos(y2, x2)] - 1 << endl, 0;
      }
    }
  }
  cout << "-1";
  return 0;
}
Problem p02644 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#include <stdio.h>
using namespace std;
#define mx 10000008

#define pii pair<int, int>
#define pi acos(-1.0)
#define pb push_back
#define ss second
#define ff first
typedef int ll;
int fx[4] = {0, +1, 0, -1};
int fy[4] = {+1, 0, -1, 0};
int main() {
  int n, m, k;
  cin >> n >> m >> k;
  char a[n + 6][m + 7];
  bool vis[n + 7][m + 8];
  int d[n + 7][m + 8];
  for (int i = 1; i <= n + 8; i++) {
    for (int j = 1; j <= m + 8; j++)
      d[i][j] = 123456789;
  }
  int sx, sy, dx, dy;
  cin >> sx >> sy >> dx >> dy;
  memset(vis, false, sizeof(vis));
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++)
      cin >> a[i][j];
  }

  queue<pii> q;
  q.push({sx, sy});

  d[sx][sy] = 0;
  while (!q.empty()) {
    int x = q.front().ff;
    int y = q.front().ss;

    q.pop();
    for (int i = 0; i < 4; i++) {
      for (int j = 1; j <= k; j++) {
        int p = x + fx[i] * j;
        int pp = y + fy[i] * j;
        if (a[p][pp] == '@' || p < 1 || p > n || pp < 1 || pp > m ||
            d[p][pp] <= d[x][y])
          break;

        else if (p >= 1 && p <= n && pp >= 1 && pp <= m &&
                 d[p][pp] > d[x][y] + 1) {

          d[p][pp] = d[x][y] + 1;
          q.push({p, pp});
        }
      }
    }
  }
  if (d[dx][dy] == 123456789) {
    cout << -1 << endl;
    return 0;
  }
  cout << d[dx][dy] << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <stdio.h>
using namespace std;
#define mx 10000008

#define pii pair<ll, ll>
#define pi acos(-1.0)
#define pb push_back
#define ss second
#define ff first
typedef long long int ll;
ll fx[4] = {0, +1, 0, -1};
ll fy[4] = {+1, 0, -1, 0};
int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  char a[n + 6][m + 7];
  bool vis[n + 7][m + 8];
  ll d[n + 7][m + 8];
  for (ll i = 1; i <= n + 8; i++) {
    for (ll j = 1; j <= m + 8; j++)
      d[i][j] = 1234567899999999;
  }
  ll sx, sy, dx, dy;
  cin >> sx >> sy >> dx >> dy;
  memset(vis, false, sizeof(vis));
  for (ll i = 1; i <= n; i++) {
    for (ll j = 1; j <= m; j++)
      cin >> a[i][j];
  }

  queue<pii> q;
  q.push({sx, sy});

  d[sx][sy] = 0;
  while (!q.empty()) {
    ll x = q.front().ff;
    ll y = q.front().ss;

    q.pop();
    for (ll i = 0; i < 4; i++) {
      for (ll j = 1; j <= k; j++) {
        ll p = x + fx[i] * j;
        ll pp = y + fy[i] * j;
        if (a[p][pp] == '@' || p < 1 || p > n || pp < 1 || pp > m ||
            d[p][pp] <= d[x][y] + 1)
          break;

        else if (p >= 1 && p <= n && pp >= 1 && pp <= m &&
                 d[p][pp] > d[x][y] + 1) {

          d[p][pp] = d[x][y] + 1;
          q.push({p, pp});
        }
      }
    }
  }
  if (d[dx][dy] == 1234567899999999) {
    cout << -1 << endl;
    exit(0);
  }
  cout << d[dx][dy] << endl;
}
Problem p02644 (C++) — buggy submission, verdict: Time Limit Exceeded

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

typedef long long ll;
const int N = 2e5 + 77;
const int mod = (int)1e9 + 7;
const int inf = (int)1e9 + 77;

int main() {
  int h, w, k, a, b, u, v;
  scanf("%d%d%d%d%d%d%d", &h, &w, &k, &a, &b, &u, &v);
  int dx[] = {1, 0, -1, 0};
  int dy[] = {0, 1, 0, -1};
  vector<string> c(h);
  vector<vector<int>> dp(h + 1, vector<int>(w + 1, inf));
  for (int i = 0; i < h; i++)
    cin >> c[i];
  queue<pair<int, int>> q;
  q.push({a, b});
  dp[a][b] = 0;
  while (q.size()) {
    auto node = q.front();
    q.pop();
    int x = node.first;
    int y = node.second;
    int xx, yy;
    for (int i = 0; i < 4; i++) {
      for (int j = 1; j <= k; j++) {
        xx = x + dx[i] * j, yy = y + dy[i] * j;
        if (xx > 0 and xx <= h and yy > 0 and yy <= w) {
          if (c[xx - 1][yy - 1] == '@' || dp[xx][yy] <= dp[x][y])
            break;
          if (dp[xx][yy] > dp[x][y] + 1) {
            dp[xx][yy] = dp[x][y] + 1;
            q.push({xx, yy});
          }
        } else
          break;
      }
    }
  }
  printf("%d", dp[u][v] == inf ? -1 : dp[u][v]);
  return 0;
}

Error: TLE

Fixed submission:

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

typedef long long ll;
const int N = 2e5 + 77;
const int mod = (int)1e9 + 7;
const int inf = (int)1e9 + 77;

int main() {
  int h, w, k, a, b, u, v;
  scanf("%d%d%d%d%d%d%d", &h, &w, &k, &a, &b, &u, &v);
  int dx[] = {1, 0, -1, 0};
  int dy[] = {0, 1, 0, -1};
  vector<string> c(h);
  vector<vector<int>> dp(h + 1, vector<int>(w + 1, inf));
  for (int i = 0; i < h; i++)
    cin >> c[i];
  queue<pair<int, int>> q;
  q.push({a, b});
  dp[a][b] = 0;
  while (q.size()) {
    auto node = q.front();
    q.pop();
    int x = node.first;
    int y = node.second;
    // cout << x << " " << y << " " << dp[x][y] << endl;
    int xx, yy;
    for (int i = 0; i < 4; i++) {
      for (int j = 1; j <= k; j++) {
        xx = x + dx[i] * j, yy = y + dy[i] * j;
        if (xx > 0 and xx <= h and yy > 0 and yy <= w) {
          if (c[xx - 1][yy - 1] == '@')
            break;
          if (dp[xx][yy] >= dp[x][y] + 1) {
            dp[xx][yy] = dp[x][y] + 1;
            // cout << "push: " << xx << " " << yy << " " << dp[xx][yy] << endl;
            q.push({xx, yy});
          } else
            break;
        } else
          break;
      }
    }
  }
  printf("%d", dp[u][v] == inf ? -1 : dp[u][v]);
  return 0;
}
Problem p02644 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int maxn = 1e6 + 5;
int main() {
  int n, m, k;
  cin >> n >> m >> k;
  std::vector<string> buff(n + 1);
  std::vector<std::vector<int>> dis(n + 1, vector<int>(m + 1));
  int x1, x2, y1, y2;
  cin >> y1 >> x1 >> y2 >> x2;
  x1--;
  x2--;
  y1--;
  y2--;
  for (int i = 0; i < n; i++)
    cin >> buff[i];
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (buff[i][j] == '@')
        dis[i][j] = 1;
  queue<pii> Q;
  dis[y1][x1] = 1;
  Q.emplace(y1, x1);
  static int dx[] = {0, 0, 1, -1};
  static int dy[] = {1, -1, 0, 0};
  while (!Q.empty()) {
    auto [y, x] = Q.front();
    Q.pop();
    for (int i = 0; i < 4; i++) {
      for (int j = 1; j <= k; j++) {
        auto [ny, nx] = pii{y + dy[i] * j, x + dx[i] * j};
        if (ny < 0 || ny >= n || nx < 0 || nx >= m ||
            (dis[ny][nx] && dis[ny][nx] <= dis[y][x]))
          break;
        if (!dis[ny][nx] || dis[y][x] + 1 < dis[ny][nx]) {
          dis[ny][nx] = dis[y][x] + 1;
          Q.emplace(ny, nx);
        }
      }
    }
  }
  // for(int i=1;i<=n;i++){for(int j=1;j<=m;j++) cout<<dis[i-1][j-1]<<'
  // ';cout<<endl;}
  cout << dis[y2][x2] - 1 << endl;
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int maxn = 1e6 + 5;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, m, k;
  cin >> n >> m >> k;
  static char buff[maxn];
  static int dis[maxn];
  cin >> buff;
  int x1, x2, y1, y2;
  cin >> y1 >> x1 >> y2 >> x2;
  auto cgpos = [&](int y, int x) -> int { return y * m + x; };
  x1--;
  x2--;
  y1--;
  y2--;
  for (int i = 0; i < n; i++)
    cin >> buff[i];
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (buff[cgpos(i, j)] == '@')
        dis[cgpos(i, j)] = 1;
  queue<pii> Q;
  dis[cgpos(y1, x1)] = 1;
  Q.emplace(y1, x1);
  static int dx[] = {0, 0, 1, -1};
  static int dy[] = {1, -1, 0, 0};
  while (!Q.empty()) {
    auto [y, x] = Q.front();
    Q.pop();
    for (int i = 0; i < 4; i++) {
      for (int j = 1; j <= k; j++) {
        auto [ny, nx] = pii{y + dy[i] * j, x + dx[i] * j};
        if (ny < 0 || ny >= n || nx < 0 || nx >= m ||
            (dis[cgpos(ny, nx)] && dis[cgpos(ny, nx)] <= dis[cgpos(y, x)]))
          break;
        if (dis[cgpos(y, x)] + 1 < dis[cgpos(ny, nx)]) {
          dis[cgpos(ny, nx)] = dis[cgpos(y, x)] + 1;
          Q.emplace(ny, nx);
        }
        if (ny == y2 && nx == x2)
          return cout << dis[cgpos(y2, x2)] - 1 << endl, 0;
      }
    }
  }
  cout << "-1";
  return 0;
}