prithivMLmods/Coder-Stat huggingface dataset:
Problem p02644 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define repo(i, n) for (int i = 1; i <= n; i++)
#define INF 1001001001
#define INFll 100100100100100
// debug用
#define PrintVec(x)                                                            \
  for (auto elementPrintVec : x) {                                             \
    cout << elementPrintVec << " ";                                            \
  }                                                                            \
  cout << endl;

using namespace std;
using ull = unsigned long long;
using ll = long long;
using P = pair<int, int>;
const int mod = 1000000007;

int up[4] = {0, -1, 0, 1};
int lr[4] = {1, 0, -1, 0};

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> s(h);
  rep(i, h) { cin >> s[i]; }
  queue<P> Q;
  Q.push(P(x1, y1));
  vector<vector<int>> ans(h, vector<int>(w, -1));
  ans[x1][y1] = 0;
  while (!Q.empty()) {
    P p = Q.front();
    Q.pop();
    int qx = p.first, qy = p.second;
    rep(i, 4) {
      repo(j, k) {
        int nx = qx + lr[i] * j, ny = qy + up[i] * j;
        if (nx < 0 || nx >= h || ny < 0 || ny >= w || s[nx][ny] == '@')
          break;
        if (ans[nx][ny] != -1)
          continue;
        ans[nx][ny] = ans[qx][qy] + 1;
        Q.push(P(nx, ny));
      }
    }
  }
  cout << ans[x2][y2] << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define repo(i, n) for (int i = 1; i <= n; i++)
#define INF 1001001001
#define INFll 100100100100100
// debug用
#define PrintVec(x)                                                            \
  for (auto elementPrintVec : x) {                                             \
    cout << elementPrintVec << " ";                                            \
  }                                                                            \
  cout << endl;

using namespace std;
using ull = unsigned long long;
using ll = long long;
using P = pair<int, int>;
const int mod = 1000000007;

int up[4] = {0, -1, 0, 1};
int lr[4] = {1, 0, -1, 0};

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> s(h);
  rep(i, h) { cin >> s[i]; }
  queue<P> Q;
  Q.push(P(x1, y1));
  vector<vector<int>> ans(h, vector<int>(w, -1));
  ans[x1][y1] = 0;
  while (!Q.empty()) {
    P p = Q.front();
    Q.pop();
    int qx = p.first, qy = p.second;
    rep(i, 4) {
      repo(j, k) {
        int nx = qx + lr[i] * j, ny = qy + up[i] * j;
        if (nx < 0 || nx >= h || ny < 0 || ny >= w || s[nx][ny] == '@')
          break;
        if (ans[nx][ny] != -1 && ans[nx][ny] <= ans[qx][qy])
          break;
        if (ans[nx][ny] != -1)
          continue;
        ans[nx][ny] = ans[qx][qy] + 1;
        Q.push(P(nx, ny));
      }
    }
  }
  cout << ans[x2][y2] << endl;
  return 0;
}
Problem p02644 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using piii = pair<ll, pii>;

ll solve() {
  ll H, W, K;
  ll x1, y1, x2, y2;
  cin >> H >> W >> K;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--, y1--, x2--, y2--;
  vector<string> C(H);
  for (int i = 0; i < H; i++) {
    cin >> C[i];
  }
  ll INF = 1LL << 60;
  vector<vector<ll>> d(H, vector<ll>(W, INF));
  pii st(x1, y1), ed(x2, y2);

  vector<int> dx = {0, 1, 0, -1}, dy = {1, 0, -1, 0};
  priority_queue<pair<ll, pii>, vector<pair<ll, pii>>, greater<>> q;
  d[st.first][st.second] = 0;
  q.push(piii(0LL, st));
  while (!q.empty()) {
    auto p = q.top();
    ll c = p.first;
    int px = p.second.first, py = p.second.second;
    q.pop();
    if (d[px][py] < c)
      continue;
    for (int ii = 0; ii < 4; ii++) {
      int x = px, y = py;
      for (int j = 0; j < K; j++) {
        x += dx[ii], y += dy[ii];
        if (x < 0 || x >= H || y < 0 || y >= W)
          break;
        if (C[x][y] == '@')
          break;
        if (d[x][y] <= d[px][py])
          continue;
        if (d[x][y] > d[px][py] + 1) {
          d[x][y] = d[px][py] + 1;
          q.push(piii(d[x][y], pii(x, y)));
        }
      }
    }
  }

  ll ans = d[x2][y2];
  if (ans == INF)
    return -1;
  return ans;
}

int main() {
  auto ans = solve();
  cout << ans << "\n";
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using piii = pair<ll, pii>;

ll solve() {
  ll H, W, K;
  ll x1, y1, x2, y2;
  cin >> H >> W >> K;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--, y1--, x2--, y2--;
  vector<string> C(H);
  for (int i = 0; i < H; i++) {
    cin >> C[i];
  }
  ll INF = 1LL << 60;
  vector<vector<ll>> d(H, vector<ll>(W, INF));
  pii st(x1, y1), ed(x2, y2);

  vector<int> dx = {0, 1, 0, -1}, dy = {1, 0, -1, 0};
  priority_queue<pair<ll, pii>, vector<pair<ll, pii>>, greater<>> q;
  d[st.first][st.second] = 0;
  q.push(piii(0LL, st));
  while (!q.empty()) {
    auto p = q.top();
    ll c = p.first;
    int px = p.second.first, py = p.second.second;
    q.pop();
    if (d[px][py] < c)
      continue;
    for (int ii = 0; ii < 4; ii++) {
      int x = px, y = py;
      for (int j = 0; j < K; j++) {
        x += dx[ii], y += dy[ii];
        if (x < 0 || x >= H || y < 0 || y >= W)
          break;
        if (C[x][y] == '@')
          break;
        if (d[x][y] <= d[px][py])
          break;
        if (d[x][y] > d[px][py] + 1) {
          d[x][y] = d[px][py] + 1;
          q.push(piii(d[x][y], pii(x, y)));
        }
      }
    }
  }

  ll ans = d[x2][y2];
  if (ans == INF)
    return -1;
  return ans;
}

int main() {
  auto ans = solve();
  cout << ans << "\n";
  return 0;
}
Problem p02644 (C++)

Buggy submission (status: 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;
          }
          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);
}


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;
          }
          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);
}
Problem p02644 (C++)

Buggy submission (status: 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])
          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;
}


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] && 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;
}
Problem p02644 (C++)

Buggy submission (status: 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;
        }
        dis[x][y] = cd + 1;
        que.push_back({x, y});
      }
    }
  }
  cout << dis[x2][y2] << endl;
}


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;
          else
            continue;
        }
        dis[x][y] = cd + 1;
        que.push_back({x, y});
      }
    }
  }
  cout << dis[x2][y2] << endl;
}
Problem p02644 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int H, W, K;
int sx, sy, ex, ey;
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};

int main(void) {
  cin >> H >> W >> K;
  vector<vector<char>> c(H, vector<char>(W));
  vector<vector<int>> dp(H, vector<int>(W));
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      dp[i][j] = INT_MAX;
    }
  }
  cin >> sx >> sy >> ex >> ey;
  sx--;
  sy--;
  ex--;
  ey--;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> c[i][j];
    }
  }
  queue<pair<int, int>> q;
  q.push({sx, sy});
  dp[sx][sy] = 0;
  while (!q.empty()) {
    pair<int, int> pi = q.front();
    q.pop();
    for (int i = 0; i < 4; i++) {
      for (int j = 1; j <= K; j++) {
        int nx = pi.first + j * dx[i];
        int ny = pi.second + j * dy[i];
        if (nx < 0 || nx >= H || ny < 0 || ny >= W)
          continue;
        if (c[nx][ny] == '@')
          break;
        if (dp[nx][ny] > dp[pi.first][pi.second] + 1) {
          q.push({nx, ny});
          dp[nx][ny] = dp[pi.first][pi.second] + 1;
        }
      }
    }
  }
  if (dp[ex][ey] == INT_MAX)
    cout << -1 << endl;
  else
    cout << dp[ex][ey] << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int H, W, K;
int sx, sy, ex, ey;
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};

int main(void) {
  cin >> H >> W >> K;
  vector<vector<char>> c(H, vector<char>(W));
  vector<vector<int>> dp(H, vector<int>(W));
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      dp[i][j] = INT_MAX;
    }
  }
  cin >> sx >> sy >> ex >> ey;
  sx--;
  sy--;
  ex--;
  ey--;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> c[i][j];
    }
  }
  queue<pair<int, int>> q;
  q.push({sx, sy});
  dp[sx][sy] = 0;
  while (!q.empty()) {
    pair<int, int> pi = q.front();
    q.pop();
    for (int i = 0; i < 4; i++) {
      for (int j = 1; j <= K; j++) {
        int nx = pi.first + j * dx[i];
        int ny = pi.second + j * dy[i];
        if (nx < 0 || nx >= H || ny < 0 || ny >= W)
          continue;
        if (c[nx][ny] == '@' || dp[nx][ny] <= dp[pi.first][pi.second])
          break;
        if (dp[nx][ny] > dp[pi.first][pi.second] + 1) {
          q.push({nx, ny});
          dp[nx][ny] = dp[pi.first][pi.second] + 1;
        }
      }
    }
  }
  if (dp[ex][ey] == INT_MAX)
    cout << -1 << endl;
  else
    cout << dp[ex][ey] << endl;

  return 0;
}
Problem p02644 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
#define DUMP(x) cout << #x << " = " << (x) << endl;
#define FOR(i, m, n) for (ll i = m; i < n; i++)
#define IFOR(i, m, n) for (ll i = n - 1; i >= m; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)
#define FOREACH(x, a) for (auto &(x) : (a))
#define ALL(v) (v).begin(), (v).end()
#define SZ(x) ll(x.size())

int main() {
  ll h, w, k;
  cin >> h >> w >> k;
  ll x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--, x2--, y1--, y2--;
  vector<string> s(h);
  REP(i, h) { cin >> s[i]; }
  vector<vector<ll>> dist(h, vector<ll>(w, -1));
  queue<P> q;
  q.push({x1, y1});
  dist[x1][y1] = 0;
  vector<ll> d = {0, 1, 0, -1};

  while (!q.empty()) {
    P p = q.front();
    q.pop();
    ll cx = p.first, cy = p.second;

    REP(i, 4) {
      FOR(j, 1, k + 1) {
        ll nx = cx + j * d[i], ny = cy + j * d[i ^ 1];
        if (nx < 0 || nx >= h || ny < 0 || ny >= w) {
          break;
        }
        if (s[nx][ny] == '@') {
          break;
        }
        if (dist[nx][ny] != -1 && dist[nx][ny] <= dist[cx][cy]) {
          break;
        }
        dist[nx][ny] = dist[cx][cy] + 1;
        q.push({nx, ny});
      }
    }
  }
  cout << dist[x2][y2] << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
#define DUMP(x) cout << #x << " = " << (x) << endl;
#define FOR(i, m, n) for (ll i = m; i < n; i++)
#define IFOR(i, m, n) for (ll i = n - 1; i >= m; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)
#define FOREACH(x, a) for (auto &(x) : (a))
#define ALL(v) (v).begin(), (v).end()
#define SZ(x) ll(x.size())

int main() {
  ll h, w, k;
  cin >> h >> w >> k;
  ll x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--, x2--, y1--, y2--;
  vector<string> s(h);
  REP(i, h) { cin >> s[i]; }
  vector<vector<ll>> dist(h, vector<ll>(w, -1));
  queue<P> q;
  q.push({x1, y1});
  dist[x1][y1] = 0;
  vector<ll> d = {0, 1, 0, -1};

  while (!q.empty()) {
    P p = q.front();
    q.pop();
    ll cx = p.first, cy = p.second;

    REP(i, 4) {
      FOR(j, 1, k + 1) {
        ll nx = cx + j * d[i], ny = cy + j * d[i ^ 1];
        if (nx < 0 || nx >= h || ny < 0 || ny >= w) {
          break;
        }
        if (s[nx][ny] == '@') {
          break;
        }
        if (dist[nx][ny] != -1 && dist[nx][ny] <= dist[cx][cy]) {
          break;
        }

        if (dist[nx][ny] == -1) {
          dist[nx][ny] = dist[cx][cy] + 1;
          q.push({nx, ny});
        }
      }
    }
  }
  cout << dist[x2][y2] << endl;
}
Problem p02644 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
const int dx[4] = {-1, 1, 0, 0}, dy[4] = {0, 0, -1, 1};
int n, m, k, x1, x2, h1, y2;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m >> k >> x1 >> h1 >> x2 >> y2;
  x1--;
  h1--;
  x2--;
  y2--;
  char grid[n + 10][m + 10];
  int dist[n + 10][m + 10];
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      cin >> grid[i][j];
  queue<pair<int, int>> q;
  q.push(make_pair(x1, h1));
  memset(dist, 0x3f, sizeof(dist));
  dist[x1][h1] = 0;
  while (!q.empty()) {
    int nx = q.front().first;
    int ny = q.front().second;
    if (nx == x2 && ny == y2) {
      cout << dist[nx][ny] << endl;
      return 0;
    }
    q.pop();
    for (int i = 0; i < 4; i++) {
      for (int j = 1; j <= k; j++) {
        int tx = nx + dx[i] * j;
        int ty = ny + dy[i] * j;
        if (tx >= 0 && tx < n && ty >= 0 && ty < m) {
          if (grid[tx][ty] == '@')
            break;
          if (dist[tx][ty] > 1e7) {
            dist[tx][ty] = dist[nx][ny] + 1;
            q.push(make_pair(tx, ty));
          }
        } else
          break;
      }
    }
  }
  cout << -1 << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int dx[4] = {-1, 1, 0, 0}, dy[4] = {0, 0, -1, 1};
int n, m, k, x1, x2, h1, y2;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m >> k >> x1 >> h1 >> x2 >> y2;
  x1--;
  h1--;
  x2--;
  y2--;
  char grid[n + 10][m + 10];
  int dist[n + 10][m + 10];
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      cin >> grid[i][j];
  queue<pair<int, int>> q;
  q.push(make_pair(x1, h1));
  memset(dist, 0x3f, sizeof(dist));
  dist[x1][h1] = 0;
  while (!q.empty()) {
    int nx = q.front().first;
    int ny = q.front().second;
    if (nx == x2 && ny == y2) {
      cout << dist[nx][ny] << endl;
      return 0;
    }
    q.pop();
    for (int i = 0; i < 4; i++) {
      for (int j = 1; j <= k; j++) {
        int tx = nx + dx[i] * j;
        int ty = ny + dy[i] * j;
        if (tx >= 0 && tx < n && ty >= 0 && ty < m) {
          if (grid[tx][ty] == '@')
            break;
          if (dist[tx][ty] > 1e7) {
            dist[tx][ty] = dist[nx][ny] + 1;
            q.push(make_pair(tx, ty));
          }
          if (dist[tx][ty] <= dist[nx][ny])
            break;
        } else
          break;
      }
    }
  }
  cout << -1 << endl;
  return 0;
}
Problem p02644 (C++)

Buggy submission (status: 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;
        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;
}


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;
        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;
}
Problem p02644 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

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

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

  vector<vector<char>> c(H, vector<char>(W));
  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j)
      cin >> c[i][j];
  }

  vector<vector<int>> dist(H, vector<int>(W, INF));
  dist[x1][y1] = 0;
  queue<pair<int, int>> Q;
  Q.emplace(x1, y1);

  while (!Q.empty()) {
    auto [x, y] = Q.front();
    Q.pop();
    for (int i = 0; i < 4; ++i) {
      for (int j = 1; j <= K; ++j) {
        int nx = x + dx[i] * j, ny = y + dy[i] * j;
        if (nx < 0 || nx >= H || ny < 0 || ny >= W || c[nx][ny] == '@' ||
            dist[nx][ny] <= dist[x][y])
          break;
        dist[nx][ny] = dist[x][y] + 1;
        Q.emplace(nx, ny);
      }
    }
  }
  cout << (dist[x2][y2] == INF ? -1 : dist[x2][y2]) << '\n';
}


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

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

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

  vector<vector<char>> c(H, vector<char>(W));
  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j)
      cin >> c[i][j];
  }

  vector<vector<int>> dist(H, vector<int>(W, INF));
  dist[x1][y1] = 0;
  queue<pair<int, int>> Q;
  Q.emplace(x1, y1);

  while (!Q.empty()) {
    auto [x, y] = Q.front();
    Q.pop();
    for (int i = 0; i < 4; ++i) {
      for (int j = 1; j <= K; ++j) {
        int nx = x + dx[i] * j, ny = y + dy[i] * j;
        if (nx < 0 || nx >= H || ny < 0 || ny >= W || c[nx][ny] == '@' ||
            dist[nx][ny] <= dist[x][y])
          break;
        if (dist[nx][ny] == INF) {
          dist[nx][ny] = dist[x][y] + 1;
          Q.emplace(nx, ny);
        }
      }
    }
  }
  cout << (dist[x2][y2] == INF ? -1 : dist[x2][y2]) << '\n';
}
Problem p02644 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using ll = long long;
using ull = unsigned long long;
using namespace std;
constexpr int MAX = 1000000;
constexpr int inf = 1 << 30;

int h, w, k;
int main() {
  cin.tie(nullptr);
  std::ios::sync_with_stdio(false);

  cin >> h >> w >> k;
  vector<string> m(h);
  int sx, sy, gx, gy;
  cin >> sy >> sx >> gy >> gx;
  sx--;
  sy--;
  gx--;
  gy--;
  for (auto &a : m)
    cin >> a;
  vector<vector<int>> f(h + 5, vector<int>(w + 5, inf));
  priority_queue<tuple<int, int, int>> q;
  q.push({0, sy, sx});
  f[sy][sx] = 0;
  while (!q.empty()) {
    auto cur = q.top();
    q.pop();
    int y = get<1>(cur);
    int x = get<2>(cur);
    int cost = -get<0>(cur);
    for (auto d :
         initializer_list<pair<int, int>>{{-1, 0}, {0, 1}, {1, 0}, {0, -1}}) {
      for (int i = 1; i <= k; ++i) {
        int dy = y + d.first * i;
        int dx = x + d.second * i;
        if (dy < 0 or dy > h - 1 or dx < 0 or dx > w - 1 or m[dy][dx] == '@')
          break;
        if (f[dy][dx] > cost + 1) {
          f[dy][dx] = cost + 1;
          q.push({-f[dy][dx], dy, dx});
        }
      }
    }
  }

  if (f[gy][gx] == inf)
    cout << -1 << endl;
  else
    cout << f[gy][gx] << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using ll = long long;
using ull = unsigned long long;
using namespace std;
constexpr int MAX = 1000000;
constexpr int inf = 1 << 30;

int h, w, k;
int main() {
  cin.tie(nullptr);
  std::ios::sync_with_stdio(false);

  cin >> h >> w >> k;
  vector<string> m(h);
  int sx, sy, gx, gy;
  cin >> sy >> sx >> gy >> gx;
  sx--;
  sy--;
  gx--;
  gy--;
  for (auto &a : m)
    cin >> a;
  vector<vector<int>> f(h + 5, vector<int>(w + 5, inf));
  priority_queue<tuple<int, int, int>> q;
  q.push({0, sy, sx});
  f[sy][sx] = 0;
  while (!q.empty()) {
    auto cur = q.top();
    q.pop();
    int y = get<1>(cur);
    int x = get<2>(cur);
    int cost = -get<0>(cur);
    for (auto d :
         initializer_list<pair<int, int>>{{-1, 0}, {0, 1}, {1, 0}, {0, -1}}) {
      for (int i = 1; i <= k; ++i) {
        int dy = y + d.first * i;
        int dx = x + d.second * i;
        if (dy < 0 or dy > h - 1 or dx < 0 or dx > w - 1 or m[dy][dx] == '@')
          break;
        if (f[dy][dx] > cost + 1) {
          f[dy][dx] = cost + 1;
          q.push({-f[dy][dx], dy, dx});
        } else if (f[dy][dx] < cost + 1)
          break;
      }
    }
  }

  if (f[gy][gx] == inf)
    cout << -1 << endl;
  else
    cout << f[gy][gx] << endl;
  return 0;
}
Problem p02644 (C++)

Buggy submission (status: 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)));
        }
      }
    }
  }
  if (D[gy][gx] == INF) {
    cout << -1 << '\n';
  } else {
    cout << D[gy][gx] << '\n';
  }
  return 0;
}


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 (D[ny][nx] < D[y][x] + 1)
          break;
        if (chmin(D[ny][nx], d + 1)) {
          que.push(make_pair(D[ny][nx], make_pair(ny, nx)));
        }
      }
    }
  }
  if (D[gy][gx] == INF) {
    cout << -1 << '\n';
  } else {
    cout << D[gy][gx] << '\n';
  }
  return 0;
}
Problem p02644 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int h, w, k;
  cin >> h >> w >> k;
  ++k;
  int xb, yb, xe, ye;
  cin >> xb >> yb >> xe >> ye;
  --xb;
  --yb;
  --xe;
  --ye;
  vector<vector<char>> rec(h, vector<char>(w));
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      cin >> rec[i][j];
    }
  }
  vector<vector<int>> dis(h, vector<int>(w, 1 << 30));
  int dx[4] = {0, 0, -1, 1};
  int dy[4] = {1, -1, 0, 0};
  queue<vector<int>> que;
  que.push({xb, yb});
  dis[xb][yb] = 0;
  while (!que.empty()) {
    int x = que.front()[0], y = que.front()[1];
    que.pop();
    for (int r = 0; r < 4; ++r) {
      int u = x, v = y, s = dis[x][y] + 1;
      for (int d = 1; d < k; ++d) {
        u += dx[r];
        v += dy[r];
        if (0 <= u && u < h && 0 <= v && v < w && rec[u][v] == '.') {
          if (dis[u][v] > s) {
            dis[u][v] = s;
            que.push({u, v});
          }
        } else {
          break;
        }
      }
    }
  }
  if (dis[xe][ye] == 1 << 30) {
    cout << -1 << endl;
  } else {
    cout << dis[xe][ye] << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int h, w, k;
  cin >> h >> w >> k;
  ++k;
  int xb, yb, xe, ye;
  cin >> xb >> yb >> xe >> ye;
  --xb;
  --yb;
  --xe;
  --ye;
  vector<vector<char>> rec(h, vector<char>(w));
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      cin >> rec[i][j];
    }
  }
  vector<vector<int>> dis(h, vector<int>(w, 1 << 30));
  int dx[4] = {0, 0, -1, 1};
  int dy[4] = {1, -1, 0, 0};
  queue<vector<int>> que;
  que.push({xb, yb});
  dis[xb][yb] = 0;
  while (!que.empty()) {
    int x = que.front()[0], y = que.front()[1];
    que.pop();
    for (int r = 0; r < 4; ++r) {
      int u = x, v = y, s = dis[x][y] + 1;
      for (int d = 1; d < k; ++d) {
        u += dx[r];
        v += dy[r];
        if (0 <= u && u < h && 0 <= v && v < w && rec[u][v] == '.') {
          if (dis[u][v] > s) {
            dis[u][v] = s;
            que.push({u, v});
          } else if (dis[u][v] < s) {
            break;
          }
        } else {
          break;
        }
      }
    }
  }
  if (dis[xe][ye] == 1 << 30) {
    cout << -1 << endl;
  } else {
    cout << dis[xe][ye] << endl;
  }
  return 0;
}
Problem p02644 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

const int inf = 1e9;
int h, w, k, x1, Y1, x2, y2, mx[] = {0, 1, 0, -1}, my[] = {1, 0, -1, 0};
string mp[100005];

struct node {
  int x, y, v;
} q[5000005];

int main() {
  cin >> h >> w >> k >> x1 >> Y1 >> x2 >> y2;
  for (int i = 1; i <= h; i++)
    cin >> mp[i];
  int d[h + 10][w + 10];
  for (int i = 1; i <= h; i++)
    for (int j = 1; j <= w; j++)
      d[i][j] = inf;
  d[x1][Y1] = 0;
  int l = 1, r = 1;
  node x;
  x.x = x1;
  x.y = Y1;
  x.v = 0;
  q[1] = x;
  while (l <= r) {
    node x = q[l++];
    if (x.v != d[x.x][x.y])
      continue;
    if (x.x == x2 && x.y == y2) {
      cout << d[x2][y2] << endl;
      return 0;
    }
    for (int i = 0; i < 4; i++) {
      for (int j = 1; j <= k; j++) {
        int nx = x.x + j * mx[i], ny = x.y + j * my[i];
        if (nx >= 1 && nx <= h && ny >= 1 && ny <= w && mp[nx][ny - 1] == '.') {
          if (d[nx][ny] > d[x.x][x.y] + 1) {
            d[nx][ny] = d[x.x][x.y] + 1;
            node xx;
            xx.x = nx;
            xx.y = ny;
            xx.v = d[nx][ny];
            q[++r] = xx;
          }
          if (d[nx][ny] < d[x.x][x.y])
            break;
        } else
          break;
      }
    }
  }
  cout << -1 << '\n';
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

const int inf = 1e9;
int h, w, k, x1, Y1, x2, y2, mx[] = {0, 1, 0, -1}, my[] = {1, 0, -1, 0};
string mp[100005];

struct node {
  int x, y, v;
} q[5000005];

int main() {
  cin >> h >> w >> k >> x1 >> Y1 >> x2 >> y2;
  for (int i = 1; i <= h; i++)
    cin >> mp[i];
  int d[h + 10][w + 10];
  for (int i = 1; i <= h; i++)
    for (int j = 1; j <= w; j++)
      d[i][j] = inf;
  d[x1][Y1] = 0;
  int l = 1, r = 1;
  node x;
  x.x = x1;
  x.y = Y1;
  x.v = 0;
  q[1] = x;
  while (l <= r) {
    node x = q[l++];
    if (x.v != d[x.x][x.y])
      continue;
    if (x.x == x2 && x.y == y2) {
      cout << d[x2][y2] << endl;
      return 0;
    }
    for (int i = 0; i < 4; i++) {
      for (int j = 1; j <= k; j++) {
        int nx = x.x + j * mx[i], ny = x.y + j * my[i];
        if (nx >= 1 && nx <= h && ny >= 1 && ny <= w && mp[nx][ny - 1] == '.') {
          if (d[nx][ny] > d[x.x][x.y] + 1) {
            d[nx][ny] = d[x.x][x.y] + 1;
            node xx;
            xx.x = nx;
            xx.y = ny;
            xx.v = d[nx][ny];
            q[++r] = xx;
          }
          if (d[nx][ny] <= d[x.x][x.y])
            break;
        } else
          break;
      }
    }
  }
  cout << -1 << '\n';
  return 0;
}
Problem p02645 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> a(n + 1);
  int b, c;
  rep(i, m) {
    cin >> b >> c;
    a[b].push_back(c);
    a[c].push_back(b);
  }
  vector<int> o(n + 1), ma(n + 1, -1);
  queue<int> q;
  int qq, l, ans = 0, k;

  rep(i, n) {
    k = 0;
    if (ma[i + 1] == -1) {
      ma[i + 1] = 0;
      q.push(i + 1);
      while (q.size() != 0) {
        qq = q.front();
        q.pop();
        l = a[qq].size();
        rep(j, l) {
          if (ma[a[qq][j]] == -1) {
            q.push(a[qq][j]);
            ma[a[qq][j]] = 3;
            o[a[qq][j]] = qq;
          } else {
            if (a[qq][j] != o[qq]) {
              // cout << qq << endl;
              k = 1;
            }
          }
        }
      }
      if (k == 0) {
        ++ans;
      }
    }
  }

  cout << ans << endl;

  return 0;
}

Fixed submission:
#include "bits/stdc++.h"
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  string s;
  cin >> s;
  string s2;
  s2 = s.substr(0, 3);
  cout << s2 << endl;

  return 0;
}
Problem p02645 (C++)

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

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> d(k);
  int tmp;
  vector<int> a(n);
  rep(i, k) {
    cin >> d.at(i);
    rep(j, d.at(i)) {
      cin >> tmp;
      a.at((int)tmp - 1) = 1;
    }
  }
  int count = 0;
  rep(i, n) {
    if (a.at(i) == 0) {
      count++;
    }
  }
  cout << count << endl;
}


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

int main() {
  string s;
  cin >> s;
  cout << s[0] << s[1] << s[2] << endl;
}
Problem p02645 (Python)

Buggy submission (status: Runtime Error):
import random

S = input()

A = random.randint(0, len(S) - 2)

print(S[A] + S[A + 1] + S[A + 2])


Fixed submission:
import random

S = input()

A = random.randint(0, len(S) - 3)

print(S[A] + S[A + 1] + S[A + 2])
Problem p02645 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  string S;
  cin >> S;
  cout << S.at(1) << S.at(2) << S.at(3) << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  string S;
  cin >> S;
  cout << S.at(0) << S.at(1) << S.at(2) << endl;
}
Problem p02645 (Python)

Buggy submission (status: Runtime Error):
import random

S = input()
rand = random.randrange(0, len(S) - 1)
print(S[rand] + S[rand + 1] + S[rand + 2])


Fixed submission:
import random

S = input()
rand = random.randrange(0, len(S) - 2)
print(S[rand] + S[rand + 1] + S[rand + 2])
Problem p02645 (C++)

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

int main() {
  string s;
  cin >> s;
  string k;
  int N = s.size();
  int p = rand() % N;
  k = s.at(p) + s.at(p + 1) + s.at(p + 2);
  cout << k << endl;
}

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

int main() {
  string s;
  cin >> s;
  string a, b, c;
  a = s.at(0);
  b = s.at(1);
  c = s.at(2);

  cout << a << b << c << endl;
}
Problem p02645 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  string s;
  int n;
  cin >> s >> n;
  cout << s.substr(n, 3) << endl;
  return 0;
}

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

int main() {
  string s;
  int n;
  cin >> s >> n;
  cout << s.substr(0, 3) << endl;
  return 0;
}
Problem p02645 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
int main(void) {
  char s[3];
  cin >> s;
  cout << s[0] << s[1] << s[2] << endl;
}


Fixed submission:
#include <iostream>
using namespace std;
int main(void) {
  char s[20];
  cin >> s;
  cout << s[0] << s[1] << s[2] << endl;
}
Problem p02645 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

int main() {
  string s;
  cin >> s;
  string ans = 0;
  for (int i = 0; i < s.length() - 2; i++) {
    if (s[i] != s[i + 1] and s[i + 1] != s[i + 2] and s[i] != s[i + 2]) {
      ans += s[i];
      ans += s[i + 1];
      ans += s[i + 2];
      break;
    }
  }
  if (ans.length() != 0)
    cout << ans << endl;
  else {
    cout << s[0] << s[1] << s[2] << endl;
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

int main() {
  string s;
  cin >> s;
  string ans = "";
  for (int i = 0; i < s.length() - 2; i++) {
    if (s[i] != s[i + 1] and s[i + 1] != s[i + 2] and s[i] != s[i + 2]) {
      ans += s[i];
      ans += s[i + 1];
      ans += s[i + 2];
      break;
    }
  }
  if (ans.length() != 0)
    cout << ans << endl;
  else {
    cout << s[0] << s[1] << s[2] << endl;
  }
  return 0;
}
Problem p02645 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;

// i: index, n: size, a: start, b: end
#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define REPD(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = a; i <= (ll)(b); i++)
#define FORD(i, a, b) for (ll i = a; i >= (ll)(b); i--)

// x: container(array, vector etc..)
#define ALL(x) (x).begin(), (x).end()
#define SIZE(x) ((ll)(x).size())
#define MAX(x) *max_element(ALL(x))
#define MIN(x) *min_element(ALL(x))

#define INF 1000000000000 // 10^12
#define MOD 10000007      // 10^9+7
#define MAXR 100000       // 10^5

#define PB push_back
#define MP make_pair
#define F first
#define S second

signed main() {
  string s;
  cin >> s;
  string nickName;
  int maxStartIndex = s.length() - 3;
  srand((unsigned int)time(NULL));
  int startIndex = rand() % maxStartIndex;
  FOR(i, startIndex, startIndex + 2) { nickName += s[i]; }
  cout << nickName << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;

// i: index, n: size, a: start, b: end
#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define REPD(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = a; i <= (ll)(b); i++)
#define FORD(i, a, b) for (ll i = a; i >= (ll)(b); i--)

// x: container(array, vector etc..)
#define ALL(x) (x).begin(), (x).end()
#define SIZE(x) ((ll)(x).size())
#define MAX(x) *max_element(ALL(x))
#define MIN(x) *min_element(ALL(x))

#define INF 1000000000000 // 10^12
#define MOD 10000007      // 10^9+7
#define MAXR 100000       // 10^5

#define PB push_back
#define MP make_pair
#define F first
#define S second

signed main() {
  string s;
  cin >> s;
  string nickName;
  int maxStartIndex = s.length() - 3;
  srand((unsigned int)time(NULL));
  int startIndex = 0;
  if (maxStartIndex != 0) {
    startIndex = rand() % maxStartIndex;
  }
  FOR(i, startIndex, startIndex + 2) { nickName += s[i]; }
  cout << nickName << endl;
  return 0;
}
Problem p02645 (Python)

Buggy submission (status: Runtime Error):
s = input()
print(s[3])


Fixed submission:
s = input()
print(s[:3])
Problem p02645 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#include <random>
#include <string>

int main() {
  string s, ans;
  getline(cin, s);
  int a = s.size();
  int i = rand() % a - 2;
  ans = s.substr(i, 3);
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#include <random>
#include <string>

int main() {
  string s, ans;
  getline(cin, s);
  int a = s.size();
  int i = rand() % (a - 2);
  ans = s.substr(i, 3);
  cout << ans << endl;
}
Problem p02645 (Python)

Buggy submission (status: Runtime Error):
print(input().split().index("0") + 1)


Fixed submission:
print(input()[:3])
Problem p02646 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
typedef long long ll;

int main() {
  ll a, b, v, w, t;

  cin >> a >> v >> b >> w >> t;

  ll dist = max(a - b, b - a);
  ll sekkin = v - w;

  bool kotae = false;

  if (dist % sekkin == 0) {

    for (int i = 0; i < t; i++) {
      dist -= sekkin;

      if (dist == 0) {
        kotae = true;
      }
    }
  }

  if (kotae) {
    cout << "YES" << endl;
  }

  else {
    cout << "NO" << endl;
  }
}


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

int main() {
  ll a, b, v, w, t;

  cin >> a >> v >> b >> w >> t;

  ll dist = max(a - b, b - a);
  ll sekkin = v - w;

  bool kotae = false;

  for (int i = 0; i < t; i++) {
    dist -= sekkin;
    if (dist <= 0) {
      kotae = true;
    }
  }

  if (kotae) {
    cout << "YES" << endl;
  }

  else {
    cout << "NO" << endl;
  }
}