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

Buggy submission (status: Time Limit Exceeded):
// F.

#include <algorithm>
#include <iostream>
#include <queue>
#include <sstream>
#include <vector>

using namespace std;
typedef long long LL;
typedef pair<int, int> II;
typedef pair<LL, II> III;

int main(int argc, char *argv[]) {
#ifdef _MSC_VER
  while (true)
#endif
  {
    int h = -1, w, k, sx, sy, gx, gy;
    LL ans = -1;
    cin >> h >> w >> k >> sy >> sx >> gy >> gx;
    --sx, --sy, --gx, --gy;
    if (h < 0)
      return 0;
    vector<string> b(h);
    for (int i = 0; i < h; ++i) {
      cin >> b[i];
    }
    priority_queue<III, vector<III>, greater<>> q;
    vector<int> dist(h * w, 1 << 30);
    q.emplace(III(0, II(sy, sx)));
    dist[sy * w + sx] = 0;
    while (!q.empty()) {
      III top = q.top();
      q.pop();
      int y = top.second.first, x = top.second.second;
      if (x == gx && y == gy) {
        ans = top.first;
        break;
      }
      const int dx[4] = {-1, 1, 0, 0};
      const int dy[4] = {0, 0, -1, 1};
      for (int dir = 0; dir < 4; ++dir) {
        for (int d = 1; d <= k; ++d) {
          int nx = x + dx[dir] * d, ny = y + dy[dir] * d;
          if (nx < 0 || nx >= w || ny < 0 || ny >= h)
            break;
          if (b[ny][nx] != '.')
            break;
          int cost = top.first + 1;
          if (cost > dist[ny * w + nx])
            break;
          dist[ny * w + nx] = cost;
          q.emplace(III(cost, II(ny, nx)));
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}


Fixed submission:
// F.

#include <algorithm>
#include <iostream>
#include <queue>
#include <sstream>
#include <vector>

using namespace std;
typedef long long LL;
typedef pair<int, int> II;
typedef pair<LL, II> III;

int main(int argc, char *argv[]) {
#ifdef _MSC_VER
  while (true)
#endif
  {
    int h = -1, w, k, sx, sy, gx, gy;
    LL ans = -1;
    cin >> h >> w >> k >> sy >> sx >> gy >> gx;
    --sx, --sy, --gx, --gy;
    if (h < 0)
      return 0;
    vector<string> b(h);
    for (int i = 0; i < h; ++i) {
      cin >> b[i];
    }
    priority_queue<III, vector<III>, greater<>> q;
    vector<int> dist(h * w, 1 << 30);
    q.emplace(III(0, II(sy, sx)));
    dist[sy * w + sx] = 0;
    while (!q.empty()) {
      III top = q.top();
      q.pop();
      int y = top.second.first, x = top.second.second;
      if (x == gx && y == gy) {
        ans = top.first;
        break;
      }
      const int dx[4] = {-1, 1, 0, 0};
      const int dy[4] = {0, 0, -1, 1};
      for (int dir = 0; dir < 4; ++dir) {
        for (int d = 1; d <= k; ++d) {
          int nx = x + dx[dir] * d, ny = y + dy[dir] * d;
          if (nx < 0 || nx >= w || ny < 0 || ny >= h)
            break;
          if (b[ny][nx] != '.')
            break;
          int cost = top.first + 1;
          if (cost > dist[ny * w + nx])
            break;
          if (cost < dist[ny * w + nx]) {
            dist[ny * w + nx] = cost;
            q.emplace(III(cost, II(ny, nx)));
          }
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p02644 (C++)

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

using namespace std;

#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
#define pb push_back
#define endl '\n'
#define snd second
#define fst first
#define fastio cin.tie(NULL), cout.sync_with_stdio(true)

typedef long long int ll;
typedef unsigned long long int ull;
typedef vector<int> vi;
typedef pair<ll, int> ii;
typedef pair<ii, int> iii;

const int mod = 1e9 + 7;
const int N = 100005;

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

bool valid(int x, int y) { return x >= 1 and x <= n and y >= 1 and y <= m; }

void solve() {
  scanf("%d%d%d", &n, &m, &k);
  queue<ii> q;
  char v[n + 1][m + 1];
  int see[n + 1][m + 1], x1, y1, x2, y2;
  scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
  for (int i = 1; i <= n; i++) {
    getchar();
    for (int j = 1; j <= m; j++) {
      scanf("%c", &v[i][j]);
      see[i][j] = mod;
    }
  }
  q.push({x1, y1});
  see[x1][y1] = 0;
  while (!q.empty()) {
    int i = q.front().fst, j = q.front().snd;
    q.pop();
    for (int t = 0; t < 4; t++) {
      for (int l = 1; l <= k; l++) {
        int ni = i + dx[t] * l, nj = j + dy[t] * l;
        if (!valid(ni, nj) or v[ni][nj] == '@' or see[ni][nj] < see[i][j] + 1)
          break;
        see[ni][nj] = see[i][j] + 1;
        q.push({ni, nj});
      }
    }
  }
  if (see[x2][y2] == mod)
    printf("-1\n");
  else
    printf("%d\n", see[x2][y2]);
}

int main() {
  // fastio;
  int t = 1;
  // cin >> t;
  while (t--)
    solve();
  return 0;
}


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

using namespace std;

#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
#define pb push_back
#define endl '\n'
#define snd second
#define fst first
#define fastio cin.tie(NULL), cout.sync_with_stdio(true)

typedef long long int ll;
typedef unsigned long long int ull;
typedef vector<int> vi;
typedef pair<ll, int> ii;
typedef pair<ii, int> iii;

const int mod = 1e9 + 7;
const int N = 100005;

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

bool valid(int x, int y) { return x >= 1 and x <= n and y >= 1 and y <= m; }

void solve() {
  scanf("%d%d%d", &n, &m, &k);
  queue<ii> q;
  char v[n + 1][m + 1];
  int see[n + 1][m + 1], x1, y1, x2, y2;
  scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
  for (int i = 1; i <= n; i++) {
    getchar();
    for (int j = 1; j <= m; j++) {
      scanf("%c", &v[i][j]);
      see[i][j] = mod;
    }
  }
  q.push({x1, y1});
  see[x1][y1] = 0;
  while (!q.empty()) {
    int i = q.front().fst, j = q.front().snd;
    q.pop();
    for (int t = 0; t < 4; t++) {
      for (int l = 1; l <= k; l++) {
        int ni = i + dx[t] * l, nj = j + dy[t] * l;
        if (!valid(ni, nj) or v[ni][nj] == '@' or see[ni][nj] < see[i][j] + 1)
          break;
        if (see[ni][nj] > see[i][j] + 1) {
          see[ni][nj] = see[i][j] + 1;
          q.push({ni, nj});
        }
      }
    }
  }
  if (see[x2][y2] == mod)
    printf("-1\n");
  else
    printf("%d\n", see[x2][y2]);
}

int main() {
  // fastio;
  int t = 1;
  // cin >> t;
  while (t--)
    solve();
  return 0;
}
Problem p02644 (C++)

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

const int INF = 1 << 30;
const int MaxRC = 1e3;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};

struct DATA {
  int x, y;
  DATA(int x = 0, int y = 0) : x(x), y(y) {}
} st, ed, now, nxt;

int main() {
  int R, C, K;
  while (scanf("%d %d %d\n", &R, &C, &K) != EOF) {
    scanf("%d %d %d %d\n", &st.x, &st.y, &ed.x, &ed.y);
    st.x--, st.y--, ed.x--, ed.y--;

    vector<vector<char>> mat(R, vector<char>(C));
    for (int i = 0; i < R; i++, getchar())
      for (int j = 0; j < C; j++)
        mat[i][j] = getchar();
    vector<vector<int>> step(R, vector<int>(C, INF));
    queue<DATA> Q;
    Q.push(DATA(st.x, st.y));
    step[st.x][st.y] = 0;
    while (Q.empty() == 0) {
      now = Q.front();
      Q.pop();
      if (now.x == ed.x and now.y == ed.y)
        break;
      for (int i = 0; i < 4; i++)
        for (int k = 1; k <= K; k++) {
          nxt = DATA(now.x + k * dx[i], now.y + k * dy[i]);
          if (nxt.x == -1 or nxt.x == R or nxt.y == -1 or nxt.y == C or
              mat[nxt.x][nxt.y] == '@' or
              step[nxt.x][nxt.y] <= step[now.x][now.y])
            break;
          step[nxt.x][nxt.y] = step[now.x][now.y] + 1;
          Q.push(nxt);
        }
    }
    printf("%d\n", (step[ed.x][ed.y] == INF) ? -1 : step[ed.x][ed.y]);
  }
}


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

const int INF = 1 << 30;
const int MaxRC = 1e3;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};

struct DATA {
  int x, y;
  DATA(int x = 0, int y = 0) : x(x), y(y) {}
} st, ed, now, nxt;

int main() {
  int R, C, K;
  while (scanf("%d %d %d\n", &R, &C, &K) != EOF) {
    scanf("%d %d %d %d\n", &st.x, &st.y, &ed.x, &ed.y);
    st.x--, st.y--, ed.x--, ed.y--;

    vector<vector<char>> mat(R, vector<char>(C));
    for (int i = 0; i < R; i++, getchar())
      for (int j = 0; j < C; j++)
        mat[i][j] = getchar();
    vector<vector<int>> step(R, vector<int>(C, INF));
    queue<DATA> Q;
    Q.push(DATA(st.x, st.y));
    step[st.x][st.y] = 0;
    while (Q.empty() == 0) {
      now = Q.front();
      Q.pop();
      if (now.x == ed.x and now.y == ed.y)
        break;
      for (int i = 0; i < 4; i++)
        for (int k = 1; k <= K; k++) {
          nxt = DATA(now.x + k * dx[i], now.y + k * dy[i]);
          if (nxt.x == -1 or nxt.x == R or nxt.y == -1 or nxt.y == C or
              mat[nxt.x][nxt.y] == '@' or
              step[nxt.x][nxt.y] <= step[now.x][now.y])
            break;
          else if (step[nxt.x][nxt.y] > step[now.x][now.y] + 1) {
            step[nxt.x][nxt.y] = step[now.x][now.y] + 1;
            Q.push(nxt);
          }
        }
    }
    printf("%d\n", (step[ed.x][ed.y] == INF) ? -1 : step[ed.x][ed.y]);
  }
}
Problem p02644 (C++)

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

const int INF = 1e9;

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

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int h, w, kk, x1, y1, x2, y2;
  cin >> h >> w >> kk >> x1 >> y1 >> x2 >> y2;
  vector<vector<int>> dp(h + 1, vector<int>(w + 1, INF));
  vector<string> a(h);
  for (int i = 0; i < h; i++)
    cin >> a[i];

  queue<pair<int, int>> q;
  q.push({x1, y1});
  dp[x1][y1] = 0;
  while (!q.empty()) {
    int x = q.front().first;
    int y = q.front().second;
    q.pop();
    for (int k = 0; k < 4; k++) {
      for (int i = 1; i <= kk; i++) {
        int xx = x + i * d[k][0];
        int yy = y + i * d[k][1];

        if (xx <= 0 || xx > h || yy <= 0 || yy > w)
          break;
        if (a[xx - 1][yy - 1] == '@')
          break;

        // if (dp[xx][yy] == dp[x][y]) break;
        if (dp[xx][yy] == INF) {
          dp[xx][yy] = dp[x][y] + 1;
          q.push({xx, yy});
        }
      }
    }
  }
  if (dp[x2][y2] == INF)
    cout << "-1\n";
  else {
    cout << dp[x2][y2] << '\n';
  }

  return 0;
}

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

const int INF = 1e9;

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

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int h, w, kk, x1, y1, x2, y2;
  cin >> h >> w >> kk >> x1 >> y1 >> x2 >> y2;
  vector<vector<int>> dp(h + 1, vector<int>(w + 1, INF));
  vector<string> a(h);
  for (int i = 0; i < h; i++)
    cin >> a[i];

  queue<pair<int, int>> q;
  q.push({x1, y1});
  dp[x1][y1] = 0;
  while (!q.empty()) {
    int x = q.front().first;
    int y = q.front().second;
    q.pop();
    for (int k = 0; k < 4; k++) {
      for (int i = 1; i <= kk; i++) {
        int xx = x + i * d[k][0];
        int yy = y + i * d[k][1];

        if (xx <= 0 || xx > h || yy <= 0 || yy > w)
          break;
        if (a[xx - 1][yy - 1] == '@')
          break;

        if (dp[xx][yy] == dp[x][y])
          break;
        if (dp[xx][yy] == INF) {
          dp[xx][yy] = dp[x][y] + 1;
          q.push({xx, yy});
        }
      }
    }
  }
  if (dp[x2][y2] == INF)
    cout << "-1\n";
  else {
    cout << dp[x2][y2] << '\n';
  }

  return 0;
}
Problem p02644 (C++)

Buggy submission (status: Time Limit Exceeded):
#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>;

const ll INF = 1e18;
const int di[4] = {-1, 0, 1, 0};
const int dj[4] = {0, 1, 0, -1};

int main() {
  int h, w, k, si, sj, gi, gj;
  cin >> h >> w >> k >> si >> sj >> gi >> gj;
  si--;
  sj--;
  gi--;
  gj--;
  vector<string> g(h);
  rep(i, h) cin >> g[i];

  // 各マスについてスタートから到達までのコスト（かく回数）
  vector<vector<ll>> cost(h, vector<ll>(w, INF));
  cost[si][sj] = 0; // スタートマスはコスト0

  // 調べるマスを入れるキュー
  queue<P> q;

  // 調べるマスをキューに追加する関数
  auto add_g = [&](int i, int j) { q.push(make_pair(i, j)); };

  // スタート地点から調べる
  add_g(si, sj);

  while (!q.empty()) { // キューが空でない限りループ
    // キューから調べるマスの座標を取り出す
    P p = q.front();
    int cur_i = p.first;
    int cur_j = p.second;
    const ll c = cost[cur_i][cur_j];
    q.pop();

    // キューに調べるマスを追加
    rep(i, 4) rep(j, k) {
      // 行先のマスの座標
      int to_i = cur_i + di[i] * (j + 1);
      int to_j = cur_j + dj[i] * (j + 1);

      if (h <= to_i || to_i < 0 || w <= to_j || to_j < 0)
        break; // 範囲外だったら追加しない
      if (g[to_i][to_j] == '@')
        break; // ハスがあったら追加しない

      if (c + 1 <
          cost[to_i][to_j]) { // 行先のコストをもっと小さくできそうなら追加する
        add_g(to_i, to_j);
        cost[to_i][to_j] = c + 1; // 行先のコストを更新
      }
    }
  }

  ll ans = cost[gi][gj];
  if (ans == INF)
    cout << -1 << endl;
  else
    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>;

const ll INF = 1e18;
const int di[4] = {-1, 0, 1, 0};
const int dj[4] = {0, 1, 0, -1};

int main() {
  int h, w, k, si, sj, gi, gj;
  cin >> h >> w >> k >> si >> sj >> gi >> gj;
  si--;
  sj--;
  gi--;
  gj--;
  vector<string> g(h);
  rep(i, h) cin >> g[i];

  // 各マスについてスタートから到達までのコスト（かく回数）
  vector<vector<ll>> cost(h, vector<ll>(w, INF));
  cost[si][sj] = 0; // スタートマスはコスト0

  // 調べるマスを入れるキュー
  queue<P> q;

  // 調べるマスをキューに追加する関数
  auto add_g = [&](int i, int j) { q.push(make_pair(i, j)); };

  // スタート地点から調べる
  add_g(si, sj);

  while (!q.empty()) { // キューが空でない限りループ
    // キューから調べるマスの座標を取り出す
    P p = q.front();
    int cur_i = p.first;
    int cur_j = p.second;
    const ll c = cost[cur_i][cur_j];
    q.pop();

    // キューに調べるマスを追加
    rep(i, 4) rep(j, k) {
      // 行先のマスの座標
      int to_i = cur_i + di[i] * (j + 1);
      int to_j = cur_j + dj[i] * (j + 1);

      if (h <= to_i || to_i < 0 || w <= to_j || to_j < 0)
        break; // 範囲外だったら追加しない
      if (c >= cost[to_i][to_j])
        break; // 行先のコストの方が小さかったら追加しない
      if (g[to_i][to_j] == '@')
        break; // ハスがあったら追加しない

      if (c + 1 <
          cost[to_i][to_j]) { // 行先のコストをもっと小さくできそうなら追加する
        add_g(to_i, to_j);
        cost[to_i][to_j] = c + 1; // 行先のコストを更新
      }
    }
  }

  ll ans = cost[gi][gj];
  if (ans == INF)
    cout << -1 << endl;
  else
    cout << ans << endl;

  return 0;
}
Problem p02644 (C++)

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

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

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int h, w, k;
  cin >> h >> w >> k;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--, y1--, x2--, y2--;
  string S[h];
  for (int i = 0; i < h; i++)
    cin >> S[i];
  constexpr int inf = 1e9;
  vector<vector<int>> D(h, vector<int>(w, inf));
  D[x1][y1] = 0;
  queue<tuple<int, int>> Q;
  Q.emplace(x1, y1);
  auto in = [&](int x, int y) {
    return 0 <= x && x < h && 0 <= y && y < w && S[x][y] != '@';
  };
  while (!Q.empty()) {
    auto [x, y] = Q.front();
    Q.pop();
    int d = D[x][y] + 1;
    for (int i = 0; i < 4; i++) {
      int s = x, t = y;
      for (int j = 0; j < k; j++) {
        s += dx[i], t += dy[i];
        if (!in(s, t))
          break;
        if (D[s][t] > d) {
          D[s][t] = d;
          Q.emplace(s, t);
        }
      }
    }
  }
  if (D[x2][y2] == inf)
    D[x2][y2] = -1;
  cout << D[x2][y2] << "\n";
  return 0;
}

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

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

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int h, w, k;
  cin >> h >> w >> k;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--, y1--, x2--, y2--;
  string S[h];
  for (int i = 0; i < h; i++)
    cin >> S[i];
  constexpr int inf = 1e9;
  vector<vector<int>> D(h, vector<int>(w, inf));
  D[x1][y1] = 0;
  queue<tuple<int, int>> Q;
  Q.emplace(x1, y1);
  auto in = [&](int x, int y) {
    return 0 <= x && x < h && 0 <= y && y < w && S[x][y] != '@';
  };
  while (!Q.empty()) {
    auto [x, y] = Q.front();
    Q.pop();
    int d = D[x][y] + 1;
    for (int i = 0; i < 4; i++) {
      int s = x, t = y;
      for (int j = 0; j < k; j++) {
        s += dx[i], t += dy[i];
        if (!in(s, t))
          break;
        if (D[s][t] < d)
          break;
        if (D[s][t] > d) {
          D[s][t] = d;
          Q.emplace(s, t);
        }
      }
    }
  }
  if (D[x2][y2] == inf)
    D[x2][y2] = -1;
  cout << D[x2][y2] << "\n";
  return 0;
}
Problem p02644 (C++)

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

typedef long long ll;

int main() {
  int H, W, K, x1, y1, x2, y2, dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
  cin >> H >> W >> K >> x1 >> y1 >> x2 >> y2;
  vector<string> c(H);
  rep(i, H) cin >> c[i];
  vector<vector<int>> d(H + 2, vector<int>(W + 2, 1e9));
  queue<vector<int>> q;
  q.push({x1, y1, 0});
  d[x1][y1] = 0;
  while (!q.empty()) {
    auto p = q.front();
    q.pop();
    int x0 = p[0], y0 = p[1], d0 = p[2];
    rep(i, 4) {
      rep(j, K) {
        int nx = x0 + dx[i] * (j + 1), ny = y0 + dy[i] * (j + 1);
        if (nx < 1 || nx > H || ny < 1 || ny > W)
          break;
        if (c[nx - 1][ny - 1] == '@')
          break;
        if (d[nx][ny] < d[x0][y0])
          break;
        if (d[nx][ny] != 1e9)
          continue;
        q.push({nx, ny, d0 + 1});
        d[nx][ny] = d0 + 1;
      }
    }
  }
  if (d[x2][y2] == 1e9)
    cout << "-1\n";
  else
    cout << d[x2][y2] << "\n";
}

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

typedef long long ll;

int main() {
  int H, W, K, x1, y1, x2, y2, dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
  cin >> H >> W >> K >> x1 >> y1 >> x2 >> y2;
  vector<string> c(H);
  rep(i, H) cin >> c[i];
  vector<vector<int>> d(H + 2, vector<int>(W + 2, 1e9));
  queue<vector<int>> q;
  q.push({x1, y1, 0});
  d[x1][y1] = 0;
  while (!q.empty()) {
    auto p = q.front();
    q.pop();
    int x0 = p[0], y0 = p[1], d0 = p[2];
    rep(i, 4) {
      rep(j, K) {
        int nx = x0 + dx[i] * (j + 1), ny = y0 + dy[i] * (j + 1);
        if (nx < 1 || nx > H || ny < 1 || ny > W)
          break;
        if (c[nx - 1][ny - 1] == '@')
          break;
        if (d[nx][ny] <= d[x0][y0])
          break;
        if (d[nx][ny] != 1e9)
          continue;
        q.push({nx, ny, d0 + 1});
        d[nx][ny] = d0 + 1;
      }
    }
  }
  if (d[x2][y2] == 1e9)
    cout << "-1\n";
  else
    cout << d[x2][y2] << "\n";
}
Problem p02644 (C++)

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


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

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

#define rep(i, n) for (int i = 0; i < n; ++i)
#define repit(it, li) for (auto it = li.begin(); it != li.end(); it++)
#define ll long long int

#define _I(i, j) (i * w + j)

int main() {
  int h, w, k;
  cin >> h >> w >> k;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  y1--;
  x2--;
  y2--;
  char c[1000000];
  rep(i, h) rep(j, w) cin >> c[_I(i, j)];

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

  bool mita[1000000] = {0};

  queue<int> q, nq;
  q.emplace(_I(x1, y1));
  mita[_I(x1, y1)] = true;
  unordered_map<int, bool> nmita;
  int cnt = 0;
  while (!q.empty()) {
    cnt++;
    while (!q.empty()) {
      int tx = q.front() / w, ty = q.front() % w;
      q.pop();

      rep(r, 4) {
        for (int s = 1; s <= k; s++) {
          int x = tx + dx[r] * s, y = ty + dy[r] * s;
          if (x == x2 && y == y2) {
            cout << cnt << endl;
            return 0;
          }

          if (x >= 0 && x < h && y >= 0 && y < w && c[_I(x, y)] != '@' &&
              !mita[_I(x, y)]) {
            nmita[_I(x, y)] = true;
            nq.emplace(_I(x, y));
          } else
            break;
        }
      }
    }
    repit(it, nmita) { mita[it->first] = true; }
    nmita.clear();
    auto tmp = q;
    q = nq;
    nq = tmp;
  }

  cout << (-1) << endl;
  return 0;
}


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

#define rep(i, n) for (int i = 0; i < n; ++i)
#define repit(it, li) for (auto it = li.begin(); it != li.end(); it++)
#define ll long long int

#define _I(i, j) (i * w + j)

int main() {
  int h, w, k;
  cin >> h >> w >> k;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  y1--;
  x2--;
  y2--;
  char c[1000000];
  rep(i, h) rep(j, w) cin >> c[_I(i, j)];

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

  bool mita[1000000] = {0};

  queue<int> q, nq;
  q.emplace(_I(x1, y1));
  mita[_I(x1, y1)] = true;
  unordered_map<int, bool> nmita;
  int cnt = 0;
  while (!q.empty()) {
    cnt++;
    while (!q.empty()) {
      int tx = q.front() / w, ty = q.front() % w;
      q.pop();

      rep(r, 4) {
        for (int s = 1; s <= k; s++) {
          int x = tx + dx[r] * s, y = ty + dy[r] * s;
          if (x == x2 && y == y2) {
            cout << cnt << endl;
            return 0;
          }

          if (x >= 0 && x < h && y >= 0 && y < w && c[_I(x, y)] != '@' &&
              !mita[_I(x, y)]) {
            if (!nmita[_I(x, y)]) {
              nmita[_I(x, y)] = true;
              nq.emplace(_I(x, y));
            }
          } else
            break;
        }
      }
    }
    repit(it, nmita) { mita[it->first] = true; }
    nmita.clear();
    auto tmp = q;
    q = nq;
    nq = tmp;
  }

  cout << (-1) << endl;
  return 0;
}
Problem p02644 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
const int dir[4][2] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}};
const int INF = 1e9;

int h, w, k;
int sx, sy, gx, gy;
vector<vector<char>> MP;
vector<vector<int>> dis;

bool outside(int x, int y) { return x < 0 or x >= h or y < 0 or y >= w; }

bool bad(int x, int y, int nd) { return MP[x][y] == '@' or dis[x][y] < nd; }

void bfs() {
  queue<pair<int, int>> que;
  que.push({sx, sy});
  dis[sx][sy] = 0;
  while (!que.empty()) {
    int x = que.front().first;
    int y = que.front().second;
    que.pop();
    for (int i = 0; i < 4; i++) {
      int nx = x, ny = y;
      int nd = dis[x][y] + 1;
      for (int j = 1; j <= k; j++) {
        nx += dir[i][0];
        ny += dir[i][1];
        if (outside(nx, ny) or bad(nx, ny, nd))
          break;
        que.push({nx, ny});
        dis[nx][ny] = nd;
      }
    }
  }
}

int main() {
  cin >> h >> w >> k >> sx >> sy >> gx >> gy;
  --sx, --sy, --gx, --gy;
  MP.resize(h, vector<char>(w));
  dis.resize(h, vector<int>(w));
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      cin >> MP[i][j], dis[i][j] = INF;
  bfs();
  cout << (dis[gx][gy] == INF ? -1 : dis[gx][gy]);
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int dir[4][2] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}};
const int INF = 1e9;

int h, w, k;
int sx, sy, gx, gy;
vector<vector<char>> MP;
vector<vector<int>> dis;

bool outside(int x, int y) { return x < 0 or x >= h or y < 0 or y >= w; }

bool bad(int x, int y, int nd) { return MP[x][y] == '@' or dis[x][y] < nd; }

void bfs() {
  queue<pair<int, int>> que;
  que.push({sx, sy});
  dis[sx][sy] = 0;
  while (!que.empty()) {
    int x = que.front().first;
    int y = que.front().second;
    que.pop();
    for (int i = 0; i < 4; i++) {
      int nx = x, ny = y;
      int nd = dis[x][y] + 1;
      for (int j = 1; j <= k; j++) {
        nx += dir[i][0];
        ny += dir[i][1];
        if (outside(nx, ny) or bad(nx, ny, nd))
          break;
        if (dis[nx][ny] > nd) {
          que.push({nx, ny});
          dis[nx][ny] = nd;
        }
      }
    }
  }
}

int main() {
  cin >> h >> w >> k >> sx >> sy >> gx >> gy;
  --sx, --sy, --gx, --gy;
  MP.resize(h, vector<char>(w));
  dis.resize(h, vector<int>(w));
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      cin >> MP[i][j], dis[i][j] = INF;
  bfs();
  cout << (dis[gx][gy] == INF ? -1 : dis[gx][gy]);
}
Problem p02644 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using Graph_c = vector<vector<ll>>;
struct Z {
  ll y;
  ll x;
  ll depth;
};

ll H, W, K, y, x, depth, oo = 1e12, xs, ys, xg, yg;
vector<ll> y_vec = {1, 0, -1, 0};
vector<ll> x_vec = {0, 1, 0, -1};
Graph_c A, B;
queue<Z> q;

ll bfs() {
  ll ans = oo;
  while (!q.empty()) {
    Z now = q.front();
    q.pop();
    y = now.y, x = now.x, depth = now.depth;
    if (y == yg && x == xg) {
      ans = min(ans, depth);
      return ans;
    }
    for (ll i = 0; i < 4; i++) {
      for (ll k = 1; k <= K; k++) {
        Z next = {y + y_vec[i] * k, x + x_vec[i] * k, depth + 1};
        // cout<<"ny:"<<next.y<<" nx:"<<next.x<<endl;
        if (next.y <= 0 || next.y > H || next.x <= 0 || next.x > W ||
            A[next.y][next.x] == -1)
          break;
        if (A[next.y][next.x] < next.depth)
          break;
        A[next.y][next.x] = next.depth;
        q.push(next);
        // cout<<" d:"<<next.depth<<endl;
      }
    }
  }
  return oo;
}

int main() {
  cin >> H >> W >> K;
  cin >> ys >> xs >> yg >> xg;
  A.assign(H + 2, vector<ll>(W + 2, -1));
  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      char a;
      cin >> a;
      if (a == '@')
        continue;
      A[i][j] = oo;
    }
  }
  Z start = {ys, xs, 0};
  A[ys][xs] = 0;
  q.emplace(start);
  ll ans = bfs();
  if (ans == oo)
    ans = -1;
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using Graph_c = vector<vector<ll>>;
struct Z {
  ll y;
  ll x;
  ll depth;
};

ll H, W, K, y, x, depth, oo = 1e12, xs, ys, xg, yg;
vector<ll> y_vec = {1, 0, -1, 0};
vector<ll> x_vec = {0, 1, 0, -1};
Graph_c A, B;
queue<Z> q;

ll bfs() {
  ll ans = oo;
  while (!q.empty()) {
    Z now = q.front();
    q.pop();
    y = now.y, x = now.x, depth = now.depth;
    if (y == yg && x == xg) {
      ans = min(ans, depth);
      return ans;
    }
    for (ll i = 0; i < 4; i++) {
      for (ll k = 1; k <= K; k++) {
        Z next = {y + y_vec[i] * k, x + x_vec[i] * k, depth + 1};
        // cout<<"ny:"<<next.y<<" nx:"<<next.x<<endl;
        if (next.y <= 0 || next.y > H || next.x <= 0 || next.x > W ||
            A[next.y][next.x] == -1)
          break;
        if (A[next.y][next.x] < next.depth)
          break;
        if (A[next.y][next.x] == next.depth)
          continue;
        A[next.y][next.x] = next.depth;
        q.push(next);
        // cout<<" d:"<<next.depth<<endl;
      }
    }
  }
  return oo;
}

int main() {
  cin >> H >> W >> K;
  cin >> ys >> xs >> yg >> xg;
  A.assign(H + 2, vector<ll>(W + 2, -1));
  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      char a;
      cin >> a;
      if (a == '@')
        continue;
      A[i][j] = oo;
    }
  }
  Z start = {ys, xs, 0};
  A[ys][xs] = 0;
  q.emplace(start);
  ll ans = bfs();
  if (ans == oo)
    ans = -1;
  cout << ans << endl;
}
Problem p02644 (C++)

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

using namespace std;

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

int main() {

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

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

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

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

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

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

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

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

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

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

using namespace std;

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

int main() {

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

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

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

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

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

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

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

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

  cout << dist[gx][gy] << endl;
}
Problem p02644 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
#include <map>
#include <queue>
#include <set>
#include <vector>

#define INF (1 << 28)

using namespace std;

int H, W, K;
int x1, y1, x2, y2;
vector<vector<char>> c;
typedef pair<int, int> P;

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

void solve() {
  queue<P> q;
  q.push(P(x1, y1));
  int d[H][W];
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      d[i][j] = INF;
    }
  }
  d[x1][y1] = 0;

  while (!q.empty()) {
    P cc = q.front();
    q.pop();
    int cx = cc.first, cy = cc.second;
    // printf("c = (%d, %d)\n", cx, cy);
    if (cx == x2 && cy == y2) {
      break;
    }

    for (int i = 0; i < 4; i++) {
      for (int j = 1; j <= K; j++) {
        int nx = cx + dx[i] * j, ny = cy + dy[i] * j;
        // printf("n = (%d, %d) = %c\n", nx, ny, (0 <= nx && nx < H && 0 <= ny
        // && ny < W ? c[nx][ny] : '-'));
        if (0 <= nx && nx < H && 0 <= ny && ny < W && c[nx][ny] == '.' &&
            d[nx][ny] > d[cx][cy]) {
          d[nx][ny] = d[cx][cy] + 1;
          q.push(P(nx, ny));
        } else {
          break;
        }
      }
    }
  }

  printf("%d\n", d[x2][y2] == INF ? -1 : d[x2][y2]);
}

int main() {
  scanf("%d %d %d", &H, &W, &K);
  scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
  x1--;
  y1--;
  x2--;
  y2--;

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

  solve();
}


Fixed submission:
#include <cstdio>
#include <map>
#include <queue>
#include <set>
#include <vector>

#define INF (1 << 28)

using namespace std;

int H, W, K;
int x1, y1, x2, y2;
vector<vector<char>> c;
typedef pair<int, int> P;

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

void solve() {
  queue<P> q;
  q.push(P(x1, y1));
  int d[H][W];
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      d[i][j] = INF;
    }
  }
  d[x1][y1] = 0;

  while (!q.empty()) {
    P cc = q.front();
    q.pop();
    int cx = cc.first, cy = cc.second;
    // printf("c = (%d, %d)\n", cx, cy);
    if (cx == x2 && cy == y2) {
      break;
    }

    for (int i = 0; i < 4; i++) {
      for (int j = 1; j <= K; j++) {
        int nx = cx + dx[i] * j, ny = cy + dy[i] * j;
        // printf("n = (%d, %d) = %c\n", nx, ny, (0 <= nx && nx < H && 0 <= ny
        // && ny < W ? c[nx][ny] : '-'));
        if (0 <= nx && nx < H && 0 <= ny && ny < W && c[nx][ny] == '.' &&
            d[nx][ny] > d[cx][cy]) {
          if (d[nx][ny] > d[cx][cy] + 1) {
            d[nx][ny] = d[cx][cy] + 1;
            q.push(P(nx, ny));
          }
        } else {
          break;
        }
      }
    }
  }

  printf("%d\n", d[x2][y2] == INF ? -1 : d[x2][y2]);
}

int main() {
  scanf("%d %d %d", &H, &W, &K);
  scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
  x1--;
  y1--;
  x2--;
  y2--;

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

  solve();
}
Problem p02644 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <queue>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rep1(i, n) for (int i = 1; i <= n; ++i)
using namespace std;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
int main() {
  int h, w, k;
  cin >> h >> w >> k;
  vector<int> s(4);
  rep(i, 4) {
    cin >> s[i];
    s[i]--;
  }
  vector<vector<char>> c(h, vector<char>(w));
  rep(i, h) rep(j, w) cin >> c[i][j];

  using pi = pair<int, int>;
  using pii = pair<int, pi>;
  queue<pii> q;
  q.push({0, {s[0], s[1]}});
  vector<vector<int>> d(h, vector<int>(w, -1));
  d[s[0]][s[1]] = 0;
  vector<int> dx = {1, 0, -1, 0};
  vector<int> dy = {0, 1, 0, -1};
  while (!q.empty()) {
    pii p = q.front();
    q.pop();
    int dd = p.first;
    int x = p.second.first, y = p.second.second;
    rep(i, 4) {
      rep1(j, k) {
        int nx = x + dx[i] * j;
        int ny = y + dy[i] * j;
        if (0 <= nx && nx < h && 0 <= ny && ny < w) {
          if (c[nx][ny] == '@')
            break;
          if (0 <= d[nx][ny] && d[nx][ny] <= d[x][y])
            break;
          d[nx][ny] = d[x][y] + 1;
          q.push({d[nx][ny], {nx, ny}});
        } else
          break;
      }
    }
  }

  // rep(i,h) {
  //   rep(j,w) cout << d[i][j] << " ";
  //   cout << "\n";
  // }

  cout << d[s[2]][s[3]] << "\n";

  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <queue>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rep1(i, n) for (int i = 1; i <= n; ++i)
using namespace std;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
int main() {
  int h, w, k;
  cin >> h >> w >> k;
  vector<int> s(4);
  rep(i, 4) {
    cin >> s[i];
    s[i]--;
  }
  vector<vector<char>> c(h, vector<char>(w));
  rep(i, h) rep(j, w) cin >> c[i][j];

  using pi = pair<int, int>;
  using pii = pair<int, pi>;
  queue<pii> q;
  q.push({0, {s[0], s[1]}});
  vector<vector<int>> d(h, vector<int>(w, -1));
  d[s[0]][s[1]] = 0;
  vector<int> dx = {1, 0, -1, 0};
  vector<int> dy = {0, 1, 0, -1};
  while (!q.empty()) {
    pii p = q.front();
    q.pop();
    int dd = p.first;
    int x = p.second.first, y = p.second.second;
    rep(i, 4) {
      rep1(j, k) {
        int nx = x + dx[i] * j;
        int ny = y + dy[i] * j;
        if (0 <= nx && nx < h && 0 <= ny && ny < w) {
          if (c[nx][ny] == '@')
            break;
          if (d[nx][ny] == d[x][y] + 1)
            continue;
          if (0 <= d[nx][ny])
            break;
          d[nx][ny] = d[x][y] + 1;
          q.push({d[nx][ny], {nx, ny}});
        } else
          break;
      }
    }
  }

  // rep(i,h) {
  //   rep(j,w) cout << d[i][j] << " ";
  //   cout << "\n";
  // }

  cout << d[s[2]][s[3]] << "\n";

  return 0;
}
Problem p02644 (C++)

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

#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define INF 1e9
typedef long long ll;

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

int main() {
  int H, W, K;
  cin >> H >> W >> K;
  pair<int, int> start, goal;
  cin >> start.first >> start.second >> goal.first >> goal.second;

  vector<vector<char>> pond(H + 2, vector<char>(W + 2, '@'));
  vector<vector<int>> G(H + 2, vector<int>(W + 2, -1));
  FOR(i, 1, H + 1) {
    string S;
    cin >> S;
    FOR(j, 1, W + 1) { pond[i][j] = S[j - 1]; }
  }
  G[start.first][start.second] = 0;

  queue<pair<int, int>> q;
  q.push(start);

  while (!q.empty()) {
    auto pos = q.front();
    q.pop();

    if (pos == goal) {
      cout << G[pos.first][pos.second] << endl;
      return 0;
    }
    REP(i, 4) {
      FOR(k, 1, K + 1) {
        int next_x = pos.first + k * dx[i];
        int next_y = pos.second + k * dy[i];
        if (pond[next_x][next_y] == '.' && G[next_x][next_y] == -1) {
          // 水上かつ未探索なら
          G[next_x][next_y] = G[pos.first][pos.second] + 1;
          q.push(make_pair(next_x, next_y));
        } else if (pond[next_x][next_y] == '@') {
          break;
        }
      }
    }
  }

  cout << -1 << endl;
  return 0;
}

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

#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define INF 1e9
typedef long long ll;

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

int main() {
  int H, W, K;
  cin >> H >> W >> K;
  pair<int, int> start, goal;
  cin >> start.first >> start.second >> goal.first >> goal.second;

  vector<vector<char>> pond(H + 2, vector<char>(W + 2, '@'));
  vector<vector<int>> G(H + 2, vector<int>(W + 2, -1));
  FOR(i, 1, H + 1) {
    string S;
    cin >> S;
    FOR(j, 1, W + 1) { pond[i][j] = S[j - 1]; }
  }
  G[start.first][start.second] = 0;

  queue<pair<int, int>> q;
  q.push(start);

  while (!q.empty()) {
    auto pos = q.front();
    q.pop();

    if (pos == goal) {
      cout << G[pos.first][pos.second] << endl;
      return 0;
    }
    REP(i, 4) {
      FOR(k, 1, K + 1) {
        int next_x = pos.first + k * dx[i];
        int next_y = pos.second + k * dy[i];
        if (pond[next_x][next_y] == '.' && G[next_x][next_y] == -1) {
          // 水上かつ未探索なら
          G[next_x][next_y] = G[pos.first][pos.second] + 1;
          q.push(make_pair(next_x, next_y));
        } else if (pond[next_x][next_y] == '@' ||
                   G[next_x][next_y] <= G[pos.first][pos.second]) {
          break;
        }
      }
    }
  }

  cout << -1 << endl;
  return 0;
}
Problem p02644 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <array>
#include <cmath>
#include <iostream>
#include <queue>
#include <vector>

const std::array<int32_t, 4> dx = {1, 0, -1, 0};
const std::array<int32_t, 4> dy = {0, 1, 0, -1};

int main() {
  uint32_t H = 0, W = 0, K = 0;
  std::cin >> H >> W >> K;
  std::pair<uint32_t, uint32_t> start, goal;
  std::cin >> start.first >> start.second >> goal.first >> goal.second;
  std::vector<std::vector<int32_t>> G(H + 2, std::vector<int32_t>(W + 2, -1));
  for (size_t i = 0; i < H; i++) {
    std::string str;
    std::cin >> str;
    for (size_t j = 0; j < W; j++) {
      if (str[j] == '.')
        G[i + 1][j + 1] = 0;
    }
  }

  std::queue<std::pair<uint32_t, uint32_t>> Q;
  Q.push(start);
  while (!Q.empty()) {
    uint32_t x = Q.front().first, y = Q.front().second;
    Q.pop();
    for (size_t i = 0; i < dx.size(); i++) {
      for (size_t k = 1; k <= K; k++) {
        uint32_t to_x = x + dx[i] * k, to_y = y + dy[i] * k;
        if ((to_x == start.first && to_y == start.second) ||
            G[to_x][to_y] == -1)
          break;
        if (G[to_x][to_y] != 0) {
          if (G[to_x][to_y] < G[x][y])
            break;
          continue;
        }
        G[to_x][to_y] = G[x][y] + 1;
        Q.push({to_x, to_y});
      }
    }
  }

  std::cout << (G[goal.first][goal.second] == 0 ? -1
                                                : G[goal.first][goal.second])
            << std::endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <array>
#include <cmath>
#include <iostream>
#include <queue>
#include <vector>

const std::array<int32_t, 4> dx = {1, 0, -1, 0};
const std::array<int32_t, 4> dy = {0, 1, 0, -1};

int main() {
  uint32_t H = 0, W = 0, K = 0;
  std::cin >> H >> W >> K;
  std::pair<uint32_t, uint32_t> start, goal;
  std::cin >> start.first >> start.second >> goal.first >> goal.second;
  std::vector<std::vector<int32_t>> G(H + 2, std::vector<int32_t>(W + 2, -1));
  for (size_t i = 0; i < H; i++) {
    std::string str;
    std::cin >> str;
    for (size_t j = 0; j < W; j++) {
      if (str[j] == '.')
        G[i + 1][j + 1] = 0;
    }
  }

  std::queue<std::pair<uint32_t, uint32_t>> Q;
  Q.push(start);
  while (!Q.empty()) {
    uint32_t x = Q.front().first, y = Q.front().second;
    Q.pop();
    for (size_t i = 0; i < dx.size(); i++) {
      for (size_t k = 1; k <= K; k++) {
        uint32_t to_x = x + dx[i] * k, to_y = y + dy[i] * k;
        if ((to_x == start.first && to_y == start.second) ||
            G[to_x][to_y] == -1)
          break;
        if (G[to_x][to_y] != 0) {
          if (G[to_x][to_y] <= G[x][y])
            break;
          continue;
        }
        G[to_x][to_y] = G[x][y] + 1;
        Q.push({to_x, to_y});
      }
    }
  }

  std::cout << (G[goal.first][goal.second] == 0 ? -1
                                                : G[goal.first][goal.second])
            << std::endl;

  return 0;
}
Problem p02644 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int n, m, k;
int a1, a2, b1, b2;
char c[1000010];
char s[1000010];
int dis[1000010];
struct node {
  int x, y;
};
queue<node> q;
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
int f(int x, int y) { return (x - 1) * m + y; }
bool check(int x, int y) {
  if (x < 1 || x > n)
    return false;
  if (y < 1 || y > m)
    return false;
  if (c[(x - 1) * m + y] == '@')
    return false;
  return true;
}
int main() {
  memset(dis, 0x3f, sizeof(dis));
  cin >> n >> m >> k;
  cin >> a1 >> b1 >> a2 >> b2;
  for (register int i = 1; i <= n; ++i) {
    scanf("%s", s + 1);
    for (register int j = 1; j <= m; ++j) {
      c[(i - 1) * m + j] = s[j];
    }
  }
  q.push(node{a1, b1});
  dis[(a1 - 1) * m + b1] = 0;
  while (!q.empty()) {
    node now = q.front();
    q.pop();
    int num = dis[f(now.x, now.y)] + 1;
    for (register int i = 0; i < 4; ++i) {
      int xx = now.x, yy = now.y;
      for (register int j = 1; j <= k; ++j) {
        xx += dx[i];
        yy += dy[i];
        if (!check(xx, yy))
          break;
        if (dis[f(xx, yy)] > num) {
          dis[f(xx, yy)] = num;
          q.push(node{xx, yy});
        }
      }
    }
  }
  if (dis[f(a2, b2)] < 0x3f3f3f3f)
    cout << dis[f(a2, b2)];
  else
    puts("-1");
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int n, m, k;
int a1, a2, b1, b2;
char c[1000010];
char s[1000010];
int dis[1000010];
struct node {
  int x, y;
};
queue<node> q;
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
int f(int x, int y) { return (x - 1) * m + y; }
bool check(int x, int y) {
  if (x < 1 || x > n)
    return false;
  if (y < 1 || y > m)
    return false;
  if (c[(x - 1) * m + y] == '@')
    return false;
  return true;
}
int main() {
  memset(dis, 0x3f, sizeof(dis));
  cin >> n >> m >> k;
  cin >> a1 >> b1 >> a2 >> b2;
  for (register int i = 1; i <= n; ++i) {
    scanf("%s", s + 1);
    for (register int j = 1; j <= m; ++j) {
      c[(i - 1) * m + j] = s[j];
    }
  }
  q.push(node{a1, b1});
  dis[(a1 - 1) * m + b1] = 0;
  while (!q.empty()) {
    node now = q.front();
    q.pop();
    int num = dis[f(now.x, now.y)] + 1;
    for (register int i = 0; i < 4; ++i) {
      int xx = now.x, yy = now.y;
      for (register int j = 1; j <= k; ++j) {
        xx += dx[i];
        yy += dy[i];
        if (!check(xx, yy) || dis[f(xx, yy)] < dis[f(now.x, now.y)] + 1)
          break;
        if (dis[f(xx, yy)] > num) {
          dis[f(xx, yy)] = num;
          q.push(node{xx, yy});
        }
      }
    }
  }
  if (dis[f(a2, b2)] < 0x3f3f3f3f)
    cout << dis[f(a2, b2)];
  else
    puts("-1");
  return 0;
}