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

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

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

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

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

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

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

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

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

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

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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <queue>
#include <stdio.h>
using namespace std;

struct phase {
  int x, y, d;
  pair<int, int> c;
  bool operator<(const phase &t) const {
    if (c.first == t.c.first)
      return c.second < t.c.second;
    return c.first > t.c.first;
  }
};

int n, m, k;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int sx, sy, ex, ey;

char map[10001001];
pair<int, int> e[1001001][2];

priority_queue<phase> q;
void enque(int x, int y, int d, pair<int, int> c) {
  if (e[x * m + y][d] > c) {
    e[x * m + y][d] = c;
    q.push({x, y, d, c});
  }
}

int main() {
  scanf("%d %d %d", &n, &m, &k);
  scanf("%d %d %d %d", &sx, &sy, &ex, &ey);
  for (int i = 0; i < n; i++)
    scanf("%s", map + i * m);
  for (int i = 0; i < n * m; i++)
    e[i][0] = e[i][1] = {100000000, 0};
  enque(sx, sy, 0, {0, 0});
  enque(sx, sy, 1, {0, 0});
  while (!q.empty()) {
    int x = q.top().x;
    int y = q.top().y;
    int d = q.top().d;
    auto c = q.top().c;
    q.pop();

    if (e[x * m + y][d] < c)
      continue;
    for (int f = 0; f < 4; f++) {
      int px = x + dx[f];
      int py = y + dy[f];
      if (px < 0 || px >= n || py < 0 || py >= m || map[px * m + py] == '@')
        continue;
      if (c.second < 0 && f % 2 == d)
        enque(px, py, f % 2, {c.first, c.second + 1});
      else
        enque(px, py, f % 2, {c.first + 1, 1 - k});
    }
  }

  int ans = min(e[ex * m + ey][0].first, e[ex * m + ey][1].first);
  if (ans == 100000000)
    ans = -1;
  printf("%d\n", ans);

  return 0;
}


Fixed submission:
#include <algorithm>
#include <queue>
#include <stdio.h>
using namespace std;

struct phase {
  int x, y, d;
  pair<int, int> c;
  bool operator<(const phase &t) const {
    if (c.first == t.c.first)
      return c.second < t.c.second;
    return c.first > t.c.first;
  }
};

int n, m, k;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int sx, sy, ex, ey;

char map[10001001];
pair<int, int> e[1001001][2];

priority_queue<phase> q;
void enque(int x, int y, int d, pair<int, int> c) {
  if (e[x * m + y][d] > c) {
    e[x * m + y][d] = c;
    q.push({x, y, d, c});
  }
}

int main() {
  scanf("%d %d %d", &n, &m, &k);
  scanf("%d %d %d %d", &sx, &sy, &ex, &ey);
  sx--;
  sy--;
  ex--;
  ey--;
  for (int i = 0; i < n; i++)
    scanf("%s", map + i * m);
  for (int i = 0; i < n * m; i++)
    e[i][0] = e[i][1] = {100000000, 0};
  enque(sx, sy, 0, {0, 0});
  enque(sx, sy, 1, {0, 0});
  while (!q.empty()) {
    int x = q.top().x;
    int y = q.top().y;
    int d = q.top().d;
    auto c = q.top().c;
    q.pop();

    if (e[x * m + y][d] < c)
      continue;
    for (int f = 0; f < 4; f++) {
      int px = x + dx[f];
      int py = y + dy[f];
      if (px < 0 || px >= n || py < 0 || py >= m || map[px * m + py] == '@')
        continue;
      if (c.second < 0 && f % 2 == d)
        enque(px, py, f % 2, {c.first, c.second + 1});
      else
        enque(px, py, f % 2, {c.first + 1, 1 - k});
    }
  }

  int ans = min(e[ex * m + ey][0].first, e[ex * m + ey][1].first);
  if (ans == 100000000)
    ans = -1;
  printf("%d\n", ans);

  return 0;
}
Problem p02644 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll inf = 1e18;
#define rep(i, a, b) for (int i = a; i < b; i++)
#define per(i, a, b) for (int i = b - 1; i >= a; i--)
#define int ll
using pint = pair<int, int>;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};

signed main() {
  int h, w, k;
  cin >> h >> w >> k;
  int xs, ys, xt, yt;
  cin >> ys >> xs >> yt >> xt;
  --xs, --xt, --ys, --yt;
  char c[h][w];
  rep(i, 0, h) rep(j, 0, w) cin >> c[i][j];
  int dist[h][w];
  rep(i, 0, h) rep(j, 0, w) dist[i][j] = inf;
  queue<pint> q;
  q.push({ys, xs});
  dist[ys][xs] = 0;
  while (!q.empty()) {
    pint now = q.front();
    q.pop();
    int ny = now.first, nx = now.second;
    rep(i, 0, 4) {
      rep(j, 1, k + 1) {
        int nexty = ny + dy[i] * j, nextx = nx + dx[i] * j;
        if (nexty < 0 || nexty >= h || nextx < 0 || nextx >= w)
          break;
        if (c[nexty][nextx] == '@')
          break;
        if (dist[nexty][nextx] <= dist[ny][nx])
          break;
        dist[nexty][nextx] = dist[ny][nx] + 1;
        q.push({nexty, nextx});
      }
    }
  }
  /*
  rep (i, 0, h) {
      rep (j, 0, w) cout << dist[i][j] << " ";
      cout << "\n";
  }
  */
  if (dist[yt][xt] == inf) {
    cout << "-1\n";
  } else {
    cout << dist[yt][xt] << "\n";
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll inf = 1e18;
#define rep(i, a, b) for (int i = a; i < b; i++)
#define per(i, a, b) for (int i = b - 1; i >= a; i--)
#define int ll
using pint = pair<int, int>;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};

signed main() {
  int h, w, k;
  cin >> h >> w >> k;
  int xs, ys, xt, yt;
  cin >> ys >> xs >> yt >> xt;
  --xs, --xt, --ys, --yt;
  char c[h][w];
  rep(i, 0, h) rep(j, 0, w) cin >> c[i][j];
  int dist[h][w];
  rep(i, 0, h) rep(j, 0, w) dist[i][j] = inf;
  queue<pint> q;
  q.push({ys, xs});
  dist[ys][xs] = 0;
  while (!q.empty()) {
    pint now = q.front();
    q.pop();
    int ny = now.first, nx = now.second;
    rep(i, 0, 4) {
      rep(j, 1, k + 1) {
        int nexty = ny + dy[i] * j, nextx = nx + dx[i] * j;
        if (nexty < 0 || nexty >= h || nextx < 0 || nextx >= w)
          break;
        if (c[nexty][nextx] == '@')
          break;
        if (dist[nexty][nextx] <= dist[ny][nx])
          break;
        if (dist[nexty][nextx] > dist[ny][nx] + 1) {
          dist[nexty][nextx] = dist[ny][nx] + 1;
          q.push({nexty, nextx});
        }
      }
    }
  }
  /*
  rep (i, 0, h) {
      rep (j, 0, w) cout << dist[i][j] << " ";
      cout << "\n";
  }
  */
  if (dist[yt][xt] == inf) {
    cout << "-1\n";
  } else {
    cout << dist[yt][xt] << "\n";
  }
}
Problem p02644 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
using pii = pair<int, int>;
const int INF = 1e7 + 7;
int H, W, K, st_x, st_y, end_x, end_y;

int main() {
  freopen("killer_01.txt", "r", stdin);
  freopen("killer_01_out.txt", "w", stdout);

  cin >> H >> W >> K;
  cin >> st_x >> st_y >> end_x >> end_y;
  --st_x, --st_y, --end_x, --end_y;
  vector<vector<char>> g(H, vector<char>(W));

  for (int i = 0; i < H; i++)
    for (int j = 0; j < W; j++)
      cin >> g[i][j];

  vector<vector<int>> dist(H, vector<int>(W, INF));
  dist[st_x][st_y] = 0;
  queue<pii> q;
  vector<int> dir1 = {-1, 1, 0, 0};
  vector<int> dir2 = {0, 0, 1, -1};
  q.push({st_x, st_y});

  while (!q.empty()) {
    auto f = q.front();
    q.pop();
    int x = f.F, y = f.S, d = dist[x][y];
    if (x == end_x && y == end_y) {
      cout << d << endl;
      return 0;
    }
    for (int pos = 0; pos < 4; pos++)
      for (int i = 1; i <= K; i++) {
        int xx = x + i * dir1[pos], yy = y + i * dir2[pos];
        if (xx < 0 || yy < 0 || xx >= H || yy >= W || dist[xx][yy] <= d ||
            g[xx][yy] == '@')
          break;
        if (dist[xx][yy] > d + 1) {
          dist[xx][yy] = d + 1;
          q.push({xx, yy});
        }
      }
  }

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

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
using pii = pair<int, int>;
const int INF = 1e7 + 7;
int H, W, K, st_x, st_y, end_x, end_y;

int main() {
  // freopen("killer_01.txt" , "r" , stdin);
  //  freopen("killer_01_out.txt" , "w" , stdout);

  cin >> H >> W >> K;
  cin >> st_x >> st_y >> end_x >> end_y;
  --st_x, --st_y, --end_x, --end_y;
  vector<vector<char>> g(H, vector<char>(W));

  for (int i = 0; i < H; i++)
    for (int j = 0; j < W; j++)
      cin >> g[i][j];

  vector<vector<int>> dist(H, vector<int>(W, INF));
  dist[st_x][st_y] = 0;
  queue<pii> q;
  vector<int> dir1 = {-1, 1, 0, 0};
  vector<int> dir2 = {0, 0, 1, -1};
  q.push({st_x, st_y});

  while (!q.empty()) {
    auto f = q.front();
    q.pop();
    int x = f.F, y = f.S, d = dist[x][y];
    if (x == end_x && y == end_y) {
      cout << d << endl;
      return 0;
    }
    for (int pos = 0; pos < 4; pos++)
      for (int i = 1; i <= K; i++) {
        int xx = x + i * dir1[pos], yy = y + i * dir2[pos];
        if (xx < 0 || yy < 0 || xx >= H || yy >= W || dist[xx][yy] <= d ||
            g[xx][yy] == '@')
          break;
        if (dist[xx][yy] > d + 1) {
          dist[xx][yy] = d + 1;
          q.push({xx, yy});
        }
      }
  }

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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define IOS                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0)
/* start */

const int inf = 1e8;

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

signed main() {
  IOS;
  int h, w, k;
  int x1, y1, x2, y2;
  cin >> h >> w >> k;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--, y1--, x2--, y2--;
  vector<string> g(h);
  for (auto &it : g)
    cin >> it;
  vector<vector<int>> d(h, vector<int>(w, inf));
  d[x1][y1] = 0;
  priority_queue<pair<int, pair<int, int>>> q;
  q.push(make_pair(0, make_pair(x1, y1)));
  while (!q.empty()) {
    pair<int, int> now = q.top().second;
    int dist = -q.top().first;
    q.pop();
    if (dist > d[now.first][now.second])
      continue;
    for (int i = 0; i < 4; i++) {
      for (int j = 1; j <= k; j++) {
        int x = now.first + dx[i] * j;
        int y = now.second + dy[i] * j;
        if (x >= 0 and x < h and y >= 0 and y < w) {
          if (g[x][y] == '@')
            break;
          if (dist + 1 < d[x][y]) {
            d[x][y] = dist + 1;
            q.push(make_pair(-(dist + 1), make_pair(x, y)));
          }
        } else
          break;
      }
    }
  }
  cout << ((d[x2][y2] == inf) ? -1 : d[x2][y2]) << "\n";
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define IOS                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0)
/* start */

const int inf = 1e8;

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

signed main() {
  IOS;
  int h, w, k;
  int x1, y1, x2, y2;
  cin >> h >> w >> k;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--, y1--, x2--, y2--;
  vector<string> g(h);
  for (auto &it : g)
    cin >> it;
  vector<vector<int>> d(h, vector<int>(w, inf));
  d[x1][y1] = 0;
  priority_queue<pair<int, pair<int, int>>> q;
  q.push(make_pair(0, make_pair(x1, y1)));
  while (!q.empty()) {
    pair<int, int> now = q.top().second;
    int dist = -q.top().first;
    q.pop();
    if (dist > d[now.first][now.second])
      continue;
    for (int i = 0; i < 4; i++) {
      for (int j = 1; j <= k; j++) {
        int x = now.first + dx[i] * j;
        int y = now.second + dy[i] * j;
        if (x >= 0 and x < h and y >= 0 and y < w) {
          if (g[x][y] == '@')
            break;
          if (dist + 1 > d[x][y])
            break;
          if (dist + 1 < d[x][y]) {
            d[x][y] = dist + 1;
            q.push(make_pair(-(dist + 1), make_pair(x, y)));
          }
        } else
          break;
      }
    }
  }
  cout << ((d[x2][y2] == inf) ? -1 : d[x2][y2]) << "\n";
}
Problem p02644 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr ll mod = 1e9 + 7;

const int dx[] = {1, 0, -1, 0};
const 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;
  cin >> goal.first >> goal.second;
  start.first--;
  start.second--;
  goal.first--;
  goal.second--;
  vector<string> field(h);
  for (int i = 0; i < h; ++i) {
    cin >> field[i];
  }
  vector<vector<bool>> seen(h, vector<bool>(w, false));
  vector<vector<int>> dist(h, vector<int>(w, -1));
  dist[start.first][start.second] = 0;
  seen[start.first][start.second] = true;
  queue<pair<int, int>> next;
  next.push(start);
  while (!next.empty()) {
    int x = next.front().first;
    int y = next.front().second;
    next.pop();
    for (int i = 0; i < 4; ++i) {
      for (int j = 1; j <= k; ++j) {
        int nx = x + dx[i] * j;
        int ny = y + dy[i] * j;
        if (nx < 0 || nx >= h || ny < 0 || ny >= w)
          break;
        if (field[nx][ny] == '@')
          break;
        if (seen[nx][ny] && dist[nx][ny] < dist[x][y] + 1)
          break;
        dist[nx][ny] = dist[x][y] + 1;
        next.push({nx, ny});
        seen[nx][ny] = true;
      }
    }
  }
  cout << dist[goal.first][goal.second];
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr ll mod = 1e9 + 7;

const int dx[] = {1, 0, -1, 0};
const 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;
  cin >> goal.first >> goal.second;
  start.first--;
  start.second--;
  goal.first--;
  goal.second--;
  vector<string> field(h);
  for (int i = 0; i < h; ++i) {
    cin >> field[i];
  }
  vector<vector<bool>> seen(h, vector<bool>(w, false));
  vector<vector<int>> dist(h, vector<int>(w, -1));
  dist[start.first][start.second] = 0;
  seen[start.first][start.second] = true;
  queue<pair<int, int>> next;
  next.push(start);
  while (!next.empty()) {
    int x = next.front().first;
    int y = next.front().second;
    next.pop();
    for (int i = 0; i < 4; ++i) {
      for (int j = 1; j <= k; ++j) {
        int nx = x + dx[i] * j;
        int ny = y + dy[i] * j;
        if (nx < 0 || nx >= h || ny < 0 || ny >= w)
          break;
        if (field[nx][ny] == '@')
          break;
        if (seen[nx][ny] && dist[nx][ny] < dist[x][y] + 1)
          break;
        dist[nx][ny] = dist[x][y] + 1;
        if (!seen[nx][ny])
          next.push({nx, ny});
        seen[nx][ny] = true;
      }
    }
  }
  cout << dist[goal.first][goal.second];
}
Problem p02644 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr ll mod = 1e9 + 7;

const int dx[] = {1, 0, -1, 0};
const 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;
  start.first--;
  start.second--;
  cin >> goal.first >> goal.second;
  goal.first--;
  goal.second--;
  vector<string> field(h);
  for (int i = 0; i < h; ++i) {
    cin >> field[i];
  }
  vector<vector<ll>> distance(h, vector<ll>(w, -1));
  vector<vector<bool>> seen(h, vector<bool>(w, false));
  distance[start.first][start.second] = 0;
  queue<pair<int, int>> next;
  next.push(start);
  seen[start.first][start.second] = true;
  while (!next.empty()) {
    int x = next.front().first;
    int y = next.front().second;
    next.pop();
    for (int i = 0; i < 4; ++i) {
      for (int j = 1; j <= k; ++j) {
        int nx = x + j * dx[i], ny = y + j * dy[i];
        if (nx < 0 || nx >= h || ny < 0 || ny >= w)
          break;
        if (field[nx][ny] == '@')
          break;
        if (seen[nx][ny]) {
          distance[nx][ny] = min(distance[nx][ny], distance[x][y] + 1);
          continue;
        } else
          distance[nx][ny] = distance[x][y] + 1;
        seen[nx][ny] = true;
        next.push({nx, ny});
      }
    }
  }

  cout << distance[goal.first][goal.second] << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr ll mod = 1e9 + 7;

const int dx[] = {1, 0, -1, 0};
const 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;
  start.first--;
  start.second--;
  cin >> goal.first >> goal.second;
  goal.first--;
  goal.second--;
  vector<string> field(h);
  for (int i = 0; i < h; ++i) {
    cin >> field[i];
  }
  vector<vector<ll>> distance(h, vector<ll>(w, -1));
  vector<vector<bool>> seen(h, vector<bool>(w, false));
  distance[start.first][start.second] = 0;
  queue<pair<int, int>> next;
  next.push(start);
  seen[start.first][start.second] = true;
  while (!next.empty()) {
    int x = next.front().first;
    int y = next.front().second;
    next.pop();
    for (int i = 0; i < 4; ++i) {
      for (int j = 1; j <= k; ++j) {
        int nx = x + j * dx[i], ny = y + j * dy[i];
        if (nx < 0 || nx >= h || ny < 0 || ny >= w)
          break;
        if (field[nx][ny] == '@')
          break;
        if (seen[nx][ny]) {
          if (distance[nx][ny] < distance[x][y] + 1)
            break;
          distance[nx][ny] = min(distance[nx][ny], distance[x][y] + 1);
          continue;
        } else
          distance[nx][ny] = distance[x][y] + 1;
        seen[nx][ny] = true;
        next.push({nx, ny});
      }
    }
  }

  cout << distance[goal.first][goal.second] << endl;
}
Problem p02644 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using P = pair<int, int>;
int INF = 1e9;

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

  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;

  vector<vector<int>> dist(H + 2, vector<int>(W + 2, INF));
  vector<vector<char>> vec(H + 2, vector<char>(W + 2, '@'));
  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      cin >> vec[i][j];
    }
  }

  vector<int> dx = {0, 1, 0, -1};
  vector<int> dy = {1, 0, -1, 0};
  P start = {x1, y1};
  queue<P> Q;
  Q.push(start);
  dist[x1][y1] = 0;
  while (Q.size()) {
    P p = Q.front();
    Q.pop();
    int nx = p.first;
    int ny = p.second;
    for (int i = 0; i < 4; i++) {
      for (int j = 1; j <= K; j++) {
        if (vec[nx + dx[i] * j][ny + dy[i] * j] == '@') {
          break;
        } else if (dist[nx + dx[i] * j][ny + dy[i] * j] <= dist[nx][ny]) {
          break;
        } else {
          dist[nx + dx[i] * j][ny + dy[i] * j] = dist[nx][ny] + 1;
          P q = {nx + dx[i] * j, ny + dy[i] * j};
          Q.push(q);
        }
      }
    }
  }
  if (dist[x2][y2] == INF) {
    dist[x2][y2] = -1;
  }
  cout << dist[x2][y2] << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using P = pair<int, int>;
int INF = 1e9;

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

  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;

  vector<vector<int>> dist(H + 2, vector<int>(W + 2, INF));
  vector<vector<char>> vec(H + 2, vector<char>(W + 2, '@'));
  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      cin >> vec[i][j];
    }
  }

  vector<int> dx = {0, 1, 0, -1};
  vector<int> dy = {1, 0, -1, 0};
  P start = {x1, y1};
  queue<P> Q;
  Q.push(start);
  dist[x1][y1] = 0;
  while (Q.size()) {
    P p = Q.front();
    Q.pop();
    int nx = p.first;
    int ny = p.second;
    for (int i = 0; i < 4; i++) {
      for (int j = 1; j <= K; j++) {
        if (vec[nx + dx[i] * j][ny + dy[i] * j] == '@') {
          break;
        } else if (dist[nx + dx[i] * j][ny + dy[i] * j] <= dist[nx][ny]) {
          break;
        } else if (dist[nx + dx[i] * j][ny + dy[i] * j] > dist[nx][ny] + 1) {
          dist[nx + dx[i] * j][ny + dy[i] * j] = dist[nx][ny] + 1;
          P q = {nx + dx[i] * j, ny + dy[i] * j};
          Q.push(q);
        }
      }
    }
  }
  if (dist[x2][y2] == INF) {
    dist[x2][y2] = -1;
  }
  cout << dist[x2][y2] << endl;
}
Problem p02644 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef int64_t ll;
const int INF = (int)1e9;
int H, W, K;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};

int main() {
  int x1, y1, x2, y2;
  cin >> H >> W >> K;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--, y1--, x2--, y2--;
  vector<string> S(H);
  for (int i = 0; i < H; i++)
    cin >> S[i];
  vector<vector<int>> D(H, vector<int>(W, INF));
  queue<pair<int, int>> P;
  D[x1][y1] = 0;
  P.push({x1, y1});
  while (!P.empty()) {
    int x = P.front().first, y = P.front().second;
    P.pop();
    int nd = D[x][y] + 1;
    for (int r = 0; r < 4; r++) {
      int tx = x, ty = y;
      for (int i = 0; i < K; i++) {
        tx += dx[r], ty += dy[r];
        if (tx < 0 || ty < 0 || tx >= H || ty >= W || S[tx][ty] == '@')
          break;
        if (D[tx][ty] > nd) {
          D[tx][ty] = nd;
          P.push(make_pair(tx, ty));
        }
      }
    }
  }
  int ans = D[x2][y2];
  cout << (ans < INF ? ans : -1) << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef int64_t ll;
const int INF = (int)1e9;
int H, W, K;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};

int main() {
  int x1, y1, x2, y2;
  cin >> H >> W >> K;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--, y1--, x2--, y2--;
  vector<string> S(H);
  for (int i = 0; i < H; i++)
    cin >> S[i];
  vector<vector<int>> D(H, vector<int>(W, INF));
  queue<pair<int, int>> P;
  D[x1][y1] = 0;
  P.push({x1, y1});
  while (!P.empty()) {
    int x = P.front().first, y = P.front().second;
    P.pop();
    int nd = D[x][y] + 1;
    for (int r = 0; r < 4; r++) {
      int tx = x, ty = y;
      for (int i = 0; i < K; i++) {
        tx += dx[r], ty += dy[r];
        if (tx < 0 || ty < 0 || tx >= H || ty >= W || S[tx][ty] == '@' ||
            D[tx][ty] < nd)
          break;
        if (D[tx][ty] > nd) {
          D[tx][ty] = nd;
          P.push(make_pair(tx, ty));
        }
      }
    }
  }
  int ans = D[x2][y2];
  cout << (ans < INF ? ans : -1) << endl;
}
Problem p02644 (Python)

Buggy submission (status: Time Limit Exceeded):
from collections import deque

h, w, k = map(int, input().split())
x1, y1, x2, y2 = map(int, input().split())
x1 -= 1
x2 -= 1
y1 -= 1
y2 -= 1
# C[h][w]
C = [list(input()) for _ in range(h)]

dist = [[10**10] * w for _ in range(h)]

dist[x1][y1] = 0
que = deque([])
que.append((x1, y1))

dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]

while que:
    a, b = que.popleft()
    d = dist[a][b]
    if a == x2 and b == y2:
        print(dist[x2][y2])
        exit()
    for i in range(4):
        cx = a + dx[i]
        cy = b + dy[i]
        if cx < 0 or cx >= h or cy < 0 or cy >= w:
            continue
        if C[cx][cy] == "@":
            continue
        for j in range(1, k + 1):
            nx = a + dx[i] * j
            ny = b + dy[i] * j
            if nx < 0 or nx >= h or ny < 0 or ny >= w:
                break
            if C[nx][ny] == "@":
                break
            if dist[nx][ny] == 10**10:
                dist[nx][ny] = d + 1
                que.append((nx, ny))
print(-1)


Fixed submission:
from collections import deque

h, w, k = map(int, input().split())
x1, y1, x2, y2 = map(int, input().split())
x1 -= 1
x2 -= 1
y1 -= 1
y2 -= 1
# C[h][w]
C = [list(input()) for _ in range(h)]

dist = [[10**10] * w for _ in range(h)]

dist[x1][y1] = 0
que = deque([])
que.append((x1, y1))

dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]

while que:
    a, b = que.popleft()
    d = dist[a][b]
    if a == x2 and b == y2:
        print(dist[x2][y2])
        exit()
    for i in range(4):
        cx = a + dx[i]
        cy = b + dy[i]
        if cx < 0 or cx >= h or cy < 0 or cy >= w:
            continue
        if C[cx][cy] == "@":
            continue
        for j in range(1, k + 1):
            nx = a + dx[i] * j
            ny = b + dy[i] * j
            if nx < 0 or nx >= h or ny < 0 or ny >= w:
                break
            if C[nx][ny] == "@":
                break
            if dist[nx][ny] < d + 1:
                break
            if dist[nx][ny] == 10**10:
                dist[nx][ny] = d + 1
                que.append((nx, ny))
print(-1)
Problem p02644 (Python)

Buggy submission (status: Time Limit Exceeded):
import sys
from collections import deque


def bfs(x1, y1, d):
    q = deque([])
    q.append((d, x1, y1))
    M[x1][y1] = d

    while q:
        d, x1, y1 = q.popleft()

        if [x1, y1] == [xg, yg]:
            print(d)
            return

        for dx, dy in ((0, 1), (1, 0), (0, -1), (-1, 0)):
            for k in range(1, K + 1):
                x2 = x1 + dx * k
                y2 = y1 + dy * k
                # if m[x2][y2] == '@':
                #     break
                # if x2 < 0 or x2 >= H:
                #     break
                # if y2 < 0 or y2 >= W:
                #     break
                # if M[x2][y2] < d + 1:
                #     break
                # if M[x2][y2] == d + 1:
                #     continue

                if (0 <= x2 < H) and (0 <= y2 < W):
                    if m[x2][y2] == "@":
                        break
                    elif M[x2][y2] == -1:
                        M[x2][y2] = d + 1
                        q.append((d + 1, x2, y2))
                    elif M[x2][y2] <= d:
                        break
                    elif M[x2][y2] == d + 1:
                        continue
                    else:
                        break
    print(-1)


H, W, K = map(int, sys.stdin.readline().strip().split())
xs, ys, xg, yg = map(int, sys.stdin.readline().strip().split())
xg -= 1
yg -= 1

m = []
for _ in range(H):
    m.append(list(map(str, sys.stdin.readline().strip())))

M = [[-1] * W for _ in range(H)]

bfs(xs - 1, ys - 1, 0)


Fixed submission:
import sys
from collections import deque


def bfs(x1, y1, d):
    q = deque([])
    q.append((d, x1, y1))
    M[x1][y1] = d

    while q:
        d, x1, y1 = q.popleft()

        if [x1, y1] == [xg, yg]:
            print(d)
            return

        for dx, dy in ((0, 1), (1, 0), (0, -1), (-1, 0)):
            for k in range(1, K + 1):
                x2 = x1 + dx * k
                y2 = y1 + dy * k
                # if m[x2][y2] == '@':
                #     break
                # if x2 < 0 or x2 >= H:
                #     break
                # if y2 < 0 or y2 >= W:
                #     break
                # if M[x2][y2] < d + 1:
                #     break
                # if M[x2][y2] == d + 1:
                #     continue

                if (0 <= x2 < H) and (0 <= y2 < W):
                    if m[x2][y2] == "@":
                        break
                    elif M[x2][y2] == -1:
                        M[x2][y2] = d + 1
                        q.append((d + 1, x2, y2))
                    elif M[x2][y2] <= d:
                        break
                    elif M[x2][y2] == d + 1:
                        continue
                    else:
                        break
                else:
                    break
    print(-1)


H, W, K = map(int, sys.stdin.readline().strip().split())
xs, ys, xg, yg = map(int, sys.stdin.readline().strip().split())
xg -= 1
yg -= 1

m = []
for _ in range(H):
    m.append(list(map(str, sys.stdin.readline().strip())))

M = [[-1] * W for _ in range(H)]

bfs(xs - 1, ys - 1, 0)
Problem p02644 (Python)

Buggy submission (status: Runtime Error):
from collections import deque
import sys


def bfs(x1, y1, d):
    q = deque()
    q.append((d, x1, y1))

    while q:
        d, x1, y1 = q.popleft()

        M[x1][y1] = d

        if [x1, y1] == [xg, yg]:
            return

        for dx, dy in ((0, 1), (1, 0), (0, -1), (-1, 0)):
            for k in range(1, K + 1):
                x2 = x1 + dx * k
                y2 = y1 + dy * k

                if (0 <= x2 < H) and (0 <= y2 < W):
                    if m[x2][y2] == "@":
                        break
                    elif M[x2][y2] == -1:
                        M[x2][y2] = d + 1
                        q.append((x2, y2, d + 1))  # 新しい点を足す。
                    elif M[x2][y2] < d + 1:
                        break
                else:
                    break


H, W, K = map(int, input().split())
# K = min(K, max(H, W))
xs, ys, xg, yg = map(int, input().split())
xs, ys, xg, yg = xs - 1, ys - 1, xg - 1, yg - 1

m = []
for i in range(H):
    m.append(list(map(str, sys.stdin.readline().strip())))

M = [[-1] * W for i in range(H)]

bfs(xs, ys, 0)

print(M[xg][yg])


Fixed submission:
from collections import deque
import sys


def bfs(x1, y1, d):
    q = deque()
    q.append((d, x1, y1))

    while q:
        d, x1, y1 = q.popleft()

        M[x1][y1] = d

        if [x1, y1] == [xg, yg]:
            return

        for dx, dy in ((0, 1), (1, 0), (0, -1), (-1, 0)):
            for k in range(1, K + 1):
                x2 = x1 + dx * k
                y2 = y1 + dy * k

                if (0 <= x2 < H) and (0 <= y2 < W):
                    if m[x2][y2] == "@":
                        break
                    elif M[x2][y2] == -1:
                        M[x2][y2] = d + 1
                        q.append((d + 1, x2, y2))  # 新しい点を足す。
                    elif M[x2][y2] < d + 1:
                        break
                else:
                    break


H, W, K = map(int, input().split())
# K = min(K, max(H, W))
xs, ys, xg, yg = map(int, input().split())
xs, ys, xg, yg = xs - 1, ys - 1, xg - 1, yg - 1

m = []
for i in range(H):
    m.append(list(map(str, sys.stdin.readline().strip())))

M = [[-1] * W for i in range(H)]

bfs(xs, ys, 0)

print(M[xg][yg])
Problem p02644 (Python)

Buggy submission (status: Time Limit Exceeded):
from collections import deque

H, W, K = map(int, input().split())
sh, sw, gh, gw = map(int, input().split())
sh, sw, gh, gw = sh - 1, sw - 1, gh - 1, gw - 1
G = [list(input()) for _ in range(H)]
INF = 10**9


D = [[INF] * W for _ in range(H)]
D[sh][sw] = 0
directions = [(0, 1), (0, -1), (1, 0), (-1, 0)]
que = deque([(sh, sw)])

while que:
    nh, nw = que.pop()
    for dh, dw in directions:
        for k in range(1, K + 1):
            nx_h, nx_w = nh + k * dh, nw + k * dw
            if not (0 <= nx_h < H and 0 <= nx_w < W):
                break

            if G[nx_h][nx_w] == "@":
                break

            if D[nx_h][nx_w] <= D[nh][nw]:
                break

            D[nx_h][nx_w] = D[nh][nw] + 1
            que.appendleft((nx_h, nx_w))

print(D[gh][gw] if D[gh][gw] != INF else -1)


Fixed submission:
from collections import deque

H, W, K = map(int, input().split())
sh, sw, gh, gw = map(int, input().split())
sh, sw, gh, gw = sh - 1, sw - 1, gh - 1, gw - 1
G = [list(input()) for _ in range(H)]
INF = 10**9


D = [[INF] * W for _ in range(H)]
D[sh][sw] = 0
directions = [(0, 1), (0, -1), (1, 0), (-1, 0)]
que = deque([(sh, sw)])

while que:
    nh, nw = que.pop()
    for dh, dw in directions:
        for k in range(1, K + 1):
            nx_h, nx_w = nh + k * dh, nw + k * dw
            if not (0 <= nx_h < H and 0 <= nx_w < W):
                break

            if G[nx_h][nx_w] == "@":
                break

            if D[nx_h][nx_w] <= D[nh][nw]:
                break

            if D[nx_h][nx_w] > D[nh][nw] + 1:
                D[nx_h][nx_w] = D[nh][nw] + 1
                que.appendleft((nx_h, nx_w))

print(D[gh][gw] if D[gh][gw] != INF else -1)
Problem p02644 (C++)

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

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

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

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

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

        d[nx][ny] = d[p.F][p.S] + 1;
        q.push(P(nx, ny));
      }
    }
  }
  int ans = d[gx][gy];
  if (ans == inf)
    ans = -1;
  cout << ans << ln;
  return 0;
}

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

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

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

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

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

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <utility>
#include <vector>

using namespace std;

// int main() {
//     ios::sync_with_stdio(false);
//     cin.tie(0);
//
//     return 0;
// }

const int dir[4][2] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}};
const int INF = 1e9;

int h, w, k;
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] == '@'; }

void bfs(int x1, int y1) {
  queue<pair<int, int>> que;
  que.push({x1, y1});
  dis[x1][y1] = 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() {
  int x1, y1, x2, y2;
  cin >> h >> w >> k >> x1 >> y1 >> x2 >> y2;
  --x1, --y1, --x2, --y2;
  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(x1, y1);
  cout << (dis[x2][y2] == INF ? -1 : dis[x2][y2]);
}

Fixed submission:
#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <utility>
#include <vector>

using namespace std;

// int main() {
//     ios::sync_with_stdio(false);
//     cin.tie(0);
//
//     return 0;
// }

const int dir[4][2] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}};
const int INF = 1e9;

int h, w, k;
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(int x1, int y1) {
  queue<pair<int, int>> que;
  que.push({x1, y1});
  dis[x1][y1] = 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() {
  int x1, y1, x2, y2;
  cin >> h >> w >> k >> x1 >> y1 >> x2 >> y2;
  --x1, --y1, --x2, --y2;
  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(x1, y1);
  cout << (dis[x2][y2] == INF ? -1 : dis[x2][y2]);
}
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 repn(i, n) for (int i = 1; i <= n; i++)
#define LL long long
#define pii pair<int, int>
#define fi first
#define se second
#define pb push_back
#define mpr make_pair

using namespace std;

const LL MOD = 1e9 + 7;

LL n, m, t, x1, yy1, x2, y2, dist[1000010], vis[1000010][4],
    dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1};
string s[1000010];
queue<pii> q;

bool out(int x, int y) { return x < 0 || x >= n || y < 0 || y >= m; }

int main() {
  cin >> n >> m >> t >> x1 >> yy1 >> x2 >> y2;
  x1--;
  yy1--;
  x2--;
  y2--;
  rep(i, n) cin >> s[i];
  rep(i, 1000010) dist[i] = 1e18;
  dist[x1 * m + yy1] = 0;
  rep(i, 4) {
    q.push(mpr(x1 * m + yy1, i));
    // vis[x1*m+yy1][i]=1;
  }
  while (!q.empty()) {
    pii p = q.front();
    q.pop();
    vis[p.fi][p.se] = 1;
    int y = p.fi % m, x = p.fi / m;
    rep(i, t) {
      x += dx[p.se];
      y += dy[p.se];
      if (out(x, y) || s[x][y] == '@' || vis[x * m + y][p.se] == 1 ||
          dist[x * m + y] < dist[p.fi] + 1)
        break;
      dist[x * m + y] = dist[p.fi] + 1;
      // cout<<x<<' '<<y<<' '<<dist[x*m+y]<<endl;
      rep(j, 4) {
        if (vis[x * m + y][j] == 1)
          continue;
        q.push(mpr(x * m + y, j));
      }
    }
  }
  if (dist[x2 * m + y2] == 1e18)
    puts("-1");
  else
    cout << dist[x2 * m + y2] << endl; /*
        rep(i,n)
        {
                rep(j,m) cout<<dist[i*m+j]<<'\t';cout<<endl;
        }*/
  return 0;
}

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

#define rep(i, n) for (int i = 0; i < n; i++)
#define repn(i, n) for (int i = 1; i <= n; i++)
#define LL long long
#define pii pair<int, int>
#define fi first
#define se second
#define pb push_back
#define mpr make_pair

using namespace std;

const LL MOD = 1e9 + 7;

LL n, m, t, x1, yy1, x2, y2, dist[1000010], vis[1000010][4],
    dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1};
string s[1000010];
queue<pii> q;

bool out(int x, int y) { return x < 0 || x >= n || y < 0 || y >= m; }

int main() {
  cin >> n >> m >> t >> x1 >> yy1 >> x2 >> y2;
  x1--;
  yy1--;
  x2--;
  y2--;
  rep(i, n) cin >> s[i];
  rep(i, 1000010) dist[i] = 1e18;
  dist[x1 * m + yy1] = 0;
  rep(i, 4) {
    q.push(mpr(x1 * m + yy1, i));
    // vis[x1*m+yy1][i]=1;
  }
  while (!q.empty()) {
    pii p = q.front();
    q.pop();
    if (vis[p.fi][p.se] == 1)
      continue;
    vis[p.fi][p.se] = 1;
    int y = p.fi % m, x = p.fi / m;
    rep(i, t) {
      x += dx[p.se];
      y += dy[p.se];
      if (out(x, y) || s[x][y] == '@' || vis[x * m + y][p.se] == 1 ||
          dist[x * m + y] < dist[p.fi] + 1)
        break;
      dist[x * m + y] = dist[p.fi] + 1;
      // cout<<x<<' '<<y<<' '<<dist[x*m+y]<<endl;
      rep(j, 4) {
        if (vis[x * m + y][j] == 1)
          continue;
        q.push(mpr(x * m + y, j));
      }
    }
  }
  if (dist[x2 * m + y2] == 1e18)
    puts("-1");
  else
    cout << dist[x2 * m + y2] << endl; /*
        rep(i,n)
        {
                rep(j,m) cout<<dist[i*m+j]<<'\t';cout<<endl;
        }*/
  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 < (int)n; i++)
using ll = long long;
using P = pair<int, int>;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
const int INF = 1e9;

int main() {
  int h, w, k;
  cin >> h >> w >> k;
  int a, b, gx, gy;
  cin >> a >> b >> gx >> gy;
  a--;
  b--;
  gx--;
  gy--;
  string s[h];
  rep(i, h) cin >> s[i];
  vector<vector<int>> d(h, vector<int>(w, INF));
  d[a][b] = 0;
  queue<P> que;
  que.push(P(a, b));
  while (!que.empty()) {
    P p = que.front();
    que.pop();
    int x = p.first, y = p.second;
    rep(dir, 4) {
      for (int step = 1; step <= k; step++) {
        int nx = x + step * dx[dir], ny = y + step * dy[dir];
        if (nx < 0 or ny < 0 or nx >= h or ny >= w or s[nx][ny] == '@' or
            d[nx][ny] < d[x][y] + 1)
          break;
        else {
          d[nx][ny] = d[x][y] + 1;
          que.push(P(nx, ny));
        }
      }
    }
  }
  cout << (d[gx][gy] == INF ? -1 : d[gx][gy]) << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)n; i++)
using ll = long long;
using P = pair<int, int>;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
const int INF = 1e9;

int main() {
  int h, w, k;
  cin >> h >> w >> k;
  int a, b, gx, gy;
  cin >> a >> b >> gx >> gy;
  a--;
  b--;
  gx--;
  gy--;
  string s[h];
  rep(i, h) cin >> s[i];
  vector<vector<int>> d(h, vector<int>(w, INF));
  d[a][b] = 0;
  queue<P> que;
  que.push(P(a, b));
  while (!que.empty()) {
    P p = que.front();
    que.pop();
    int x = p.first, y = p.second;
    rep(dir, 4) {
      for (int step = 1; step <= k; step++) {
        int nx = x + step * dx[dir], ny = y + step * dy[dir];
        if (nx < 0 or ny < 0 or nx >= h or ny >= w or s[nx][ny] == '@' or
            d[nx][ny] < d[x][y] + 1)
          break;
        else {
          if (d[nx][ny] > d[x][y] + 1) {
            d[nx][ny] = d[x][y] + 1;
            que.push(P(nx, ny));
          }
        }
      }
    }
  }
  cout << (d[gx][gy] == INF ? -1 : d[gx][gy]) << endl;
  return 0;
}
Problem p02644 (C++)

Buggy submission (status: Time Limit Exceeded):
#pragma GCC optimize("Ofast", "inline", "-ffast-math")
#pragma GCC target("avx,sse2,sse3,sse4,mmx")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/hash_policy.hpp>
using namespace __gnu_pbds;
using namespace std;

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

signed main() {
  ios::sync_with_stdio(false);
  cin >> n >> m >> k;
  vector<vector<int>> d;
  d.resize(n + 1, vector<int>(m + 1, -1));
  vector<vector<char>> c;
  c.resize(n + 1, vector<char>(m + 1));
  int xa, ya, xb, yb;
  cin >> xa >> ya >> xb >> yb;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> c[i][j];
    }
  }
  queue<pair<int, int>> q;
  d[xa][ya] = 0;
  q.push(make_pair(xa, ya));
  while (!q.empty()) {
    pair<int, int> t = q.front();
    q.pop();
    for (int dir = 0; dir < 4; dir++) {
      int nx = t.first, ny = t.second;
      for (int i = 1; i <= k; i++) {
        nx += dx[dir], ny += dy[dir];
        if (nx <= 0 || ny <= 0 || nx > n || ny > m)
          break;
        if (d[nx][ny] >= 0 && d[nx][ny] <= d[t.first][t.second])
          break;
        if (c[nx][ny] == '@')
          break;
        d[nx][ny] = d[t.first][t.second] + 1;
        q.push(make_pair(nx, ny));
      }
    }
  }
  cout << d[xb][yb] << endl;
  return 0;
}

Fixed submission:
#pragma GCC optimize("Ofast", "inline", "-ffast-math")
#pragma GCC target("avx,sse2,sse3,sse4,mmx")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/hash_policy.hpp>
using namespace __gnu_pbds;
using namespace std;

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

signed main() {
  ios::sync_with_stdio(false);
  cin >> n >> m >> k;
  vector<vector<int>> d;
  d.resize(n + 1, vector<int>(m + 1, -1));
  vector<vector<char>> c;
  c.resize(n + 1, vector<char>(m + 1));
  int xa, ya, xb, yb;
  cin >> xa >> ya >> xb >> yb;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> c[i][j];
    }
  }
  queue<pair<int, int>> q;
  d[xa][ya] = 0;
  q.push(make_pair(xa, ya));
  while (!q.empty()) {
    pair<int, int> t = q.front();
    q.pop();
    for (int dir = 0; dir < 4; dir++) {
      int nx = t.first, ny = t.second;
      for (int i = 1; i <= k; i++) {
        nx += dx[dir], ny += dy[dir];
        if (nx <= 0 || ny <= 0 || nx > n || ny > m)
          break;
        if (d[nx][ny] >= 0 && d[nx][ny] <= d[t.first][t.second])
          break;
        if (c[nx][ny] == '@')
          break;
        if (d[nx][ny] == -1) {
          d[nx][ny] = d[t.first][t.second] + 1;
          q.push(make_pair(nx, ny));
        }
      }
    }
  }
  cout << d[xb][yb] << endl;
  return 0;
}