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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll inf = 100000000;
ll px[] = {1, 0, -1, 0};
ll py[] = {0, 1, 0, -1};
int main() {
  ios::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  ll n, m, i, j, x, y, k, sx, sy;
  cin >> n >> m >> k;
  cin >> x >> y >> sx >> sy;

  ll dist[n + 5][m + 5];
  char arr[n + 5][m + 5];
  bool vis[n + 5][m + 5];
  memset(vis, false, sizeof(vis));

  for (i = 0; i <= n; i++)
    for (j = 0; j <= m; j++)
      dist[i][j] = inf;

  dist[x][y] = 0;
  vis[x][y] = true;

  for (i = 1; i <= n; i++)
    for (j = 1; j <= m; j++)
      cin >> arr[i][j];

  queue<pair<ll, ll>> pq;

  pq.push(make_pair(x, y));

  while (!pq.empty()) {
    pair<ll, ll> p = pq.front();
    pq.pop();
    x = p.first, y = p.second;

    for (i = 0; i < 4; i++) {
      for (j = 1; j <= k; j++) {
        ll dx = x + px[i] * j, dy = y + py[i] * j;
        if (dx < 1 || dy < 1 || dx > n || dy > m)
          break;
        if (arr[dx][dy] == '@')
          break;
        if (vis[dx][dy] && dist[dx][dy] <= dist[x][y])
          break;
        if (vis[dx][dy])
          continue;
        vis[dx][dy] = true;
        dist[dx][dy] = 1 + dist[x][y];
        pq.push(make_pair(dx, dy));
      }
    }
  }

  if (!vis[sx][sy])
    cout << "-1";
  else
    cout << dist[sx][sy];
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll inf = 100000000;
ll px[] = {1, 0, -1, 0};
ll py[] = {0, 1, 0, -1};
int main() {
  ios::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  ll n, m, i, j, x, y, k, sx, sy;
  cin >> n >> m >> k;
  cin >> x >> y >> sx >> sy;

  ll dist[n + 5][m + 5];
  char arr[n + 5][m + 5];

  for (i = 0; i <= n; i++)
    for (j = 0; j <= m; j++)
      dist[i][j] = inf;

  dist[x][y] = 0;

  for (i = 1; i <= n; i++)
    for (j = 1; j <= m; j++)
      cin >> arr[i][j];

  priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> pq;

  pq.push(make_pair(x, y));

  while (!pq.empty()) {
    pair<ll, ll> p = pq.top();
    pq.pop();
    x = p.first, y = p.second;

    for (i = 0; i < 4; i++) {
      for (j = 1; j <= k; j++) {
        ll dx = x + px[i] * j, dy = y + py[i] * j;
        if (dx < 1 || dy < 1 || dx > n || dy > m)
          break;
        if (arr[dx][dy] == '@')
          break;
        if (dist[dx][dy] <= dist[x][y])
          break;

        dist[dx][dy] = 1 + dist[x][y];
        pq.push(make_pair(dx, dy));
      }
    }
  }

  if (dist[sx][sy] == inf)
    cout << "-1";
  else
    cout << dist[sx][sy];
}
Problem p02644 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
int h, w, k, x1, yy1, x2, y2;
int cx[] = {0, 0, 1, -1};
int cy[] = {1, -1, 0, 0};
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> h >> w >> k >> x1 >> yy1 >> x2 >> y2;
  char arr[h + 1][w + 1];
  int dist[h + 1][w + 1];
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      cin >> arr[i][j];
    }
  }
  memset(dist, -1, sizeof(dist));
  dist[x1][yy1] = 0;
  queue<pair<int, int>> q;
  q.push(mp(x1, yy1));
  while (!q.empty()) {
    pair<int, int> cur = q.front();
    q.pop();
    for (int i = 0; i < 4; i++) {
      int dx = cur.first;
      int dy = cur.second;
      for (int j = 0; j < k; j++) {
        dx += cx[i];
        dy += cy[i];
        if (dx < 1 || dy < 1 || dx > h || dy > w)
          break;
        if (arr[dx][dy] == '@')
          break;
        if (dist[dx][dy] != -1 &&
            dist[dx][dy] < dist[cur.first][cur.second] + 1)
          break;
        if (dist[dx][dy] != -1 &&
            dist[dx][dy] <= dist[cur.first][cur.second] + 1)
          continue;
        dist[dx][dy] = dist[cur.first][cur.second] + 1;
        q.push(mp(dx, dy));
      }
    }
  }
  cout << dist[x2][y2];
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

#define pb push_back
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define ll long long
#define ii pair<int, int>
#define vi vector<int>
#define vll vector<ll>
#define vii vector<ii>

using namespace std;

const int INF = 1e9 + 10;

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

int h, w, k, X1, Y1, X2, Y2;
char g[1123][1123];
int dist[1123][1123];

bool ok(int x, int y) {
  return x >= 1 && x <= h && y >= 1 && y <= w && g[x][y] == '.';
}

void dfs(int x, int y, int m, int d) {
  if (dist[x][y] < d)
    return;
  dist[x][y] = d;
  for (int i = 0; i < 4; ++i) {
    int n_x = x + dx[i], n_y = y + dy[i];
    if (ok(n_x, n_y)) {
      if (m == 1) {
        dfs(n_x, n_y, k, d + 1);
      } else {
        dfs(n_x, n_y, m - 1, d);
      }
    }
  }
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> h >> w >> k >> X1 >> Y1 >> X2 >> Y2;
  for (int i = 1; i <= h; ++i) {
    for (int j = 1; j <= w; ++j) {
      dist[i][j] = INF;
      cin >> g[i][j];
    }
  }
  dfs(X1, Y1, k, 1);
  cout << (dist[X2][Y2] == INF ? -1 : dist[X2][Y2]) << endl;
  return 0;
}
Problem p02644 (C++) — buggy submission, verdict: 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) || 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;
}

Error: TLE

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))
          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;
}
Problem p02644 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int H, W, K;
int sx, sy, gx, gy;
vector<vector<int>> hasu;
vector<vector<int>> dist;
constexpr int dx[] = {0, 1, 0, -1};
constexpr int dy[] = {1, 0, -1, 0};

int main() {
  cin >> H >> W >> K;
  cin >> sx >> sy >> gx >> gy;
  sx--, sy--, gx--, gy--;
  hasu = vector<vector<int>>(H, vector<int>(W, 0));
  dist = vector<vector<int>>(H, vector<int>(W, 0xffffff));
  dist[sx][sy] = 0;
  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      char c;
      cin >> c;
      hasu[i][j] = c == '@';
    }
  }
  queue<pair<int, int>> que;
  que.push(make_pair(sx, sy));
  while (que.size()) {
    int x, y;
    tie(x, y) = que.front();
    que.pop();
    for (int d = 0; d < 4; ++d) {
      for (int i = 1; i <= K; ++i) {
        int nx = x + dx[d] * i;
        int ny = y + dy[d] * i;
        if (nx < 0 || H <= nx)
          break;
        if (ny < 0 || W <= ny)
          break;
        if (hasu[nx][ny])
          break;
        if (dist[nx][ny] <= dist[x][y])
          break;
        if (dist[nx][ny] <= dist[x][y] + 1)
          continue;
        dist[nx][ny] = dist[x][y] + 1;
        que.push(make_pair(nx, ny));
      }
    }
  }
  cout << (0xffffff == dist[gx][gy] ? -1 : dist[gx][gy]) << endl;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
using ll = long long;

int H, W, K;
int x1, y1, x2, y2;
vector<vector<int>> hasu;
vector<vector<int>> dist;

void dfs(int x, int y) {
  constexpr int dx[] = {0, 1, 0, -1};
  constexpr int dy[] = {1, 0, -1, 0};
  for (int d = 0; d < 4; ++d) {
    for (int i = 1; i <= K; ++i) {
      int nx = x + dx[d] * i;
      int ny = y + dy[d] * i;
      if (nx < 0 || H <= nx)
        break;
      if (ny < 0 || W <= ny)
        break;
      if (hasu[nx][ny])
        break;
      if (dist[nx][ny] <= dist[x][y] + 1)
        continue;
      dist[nx][ny] = dist[x][y] + 1;
      dfs(nx, ny);
    }
  }
}

int main() {
  cin >> H >> W >> K;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--, y1--, x2--, y2--;
  hasu = vector<vector<int>>(H, vector<int>(W, 0));
  dist = vector<vector<int>>(H, vector<int>(W, 0xffffff));
  dist[x1][y1] = 0;
  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      char c;
      cin >> c;
      hasu[i][j] = c == '@';
    }
  }
  dfs(x1, y1);
  cout << (0xffffff == dist[x2][y2] ? -1 : dist[x2][y2]) << endl;
}
Problem p02644 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
int h, w, k, x1, yy1, x2, y2, d[4] = {1, -1}, mp[2000005], qu[1000005], b, e,
                              inf = 1e9;
int main() {
  scanf("%d%d%d", &h, &w, &k);
  w++;
  scanf("%d%d%d%d", &x1, &yy1, &x2, &y2);
  x1--;
  x2--;
  memset(mp, -1, sizeof(mp));
  for (int i = 0; i < h * w; i++)
    if (getchar() == '.')
      mp[i] = inf;
  mp[qu[e++] = x1 * w + yy1] = 0;
  d[2] = w;
  d[3] = -w;
  while (b < e)
    for (int i = 0, x = qu[b++]; i < 4; i++)
      for (int nx = x + d[i], u = 0; u < k && 0 <= nx && mp[nx] > mp[x];
           u++, nx += d[i])
        if (mp[nx] == inf)
          mp[qu[e++] = nx] = mp[x] + 1;
  printf("%d\\n", mp[x2 * w + y2] == inf ? -1 : mp[x2 * w + y2]);
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
int h, w, k, x1, yy1, x2, y2, d[4] = {1, -1}, mp[1000005], qu[1000005], b, e,
                              inf = 1e9;
int main() {
  scanf("%d%d%d", &h, &w, &k);
  w++;
  scanf("%d%d%d%d", &x1, &yy1, &x2, &y2);
  x1--;
  x2--;
  memset(mp, -1, sizeof(mp));
  for (int i = 0; i < h * w; i++)
    if (getchar() == '.')
      mp[i] = inf;
  mp[qu[e++] = x1 * w + yy1] = 0;
  d[2] = w;
  d[3] = -w;
  while (b < e)
    for (int i = 0, x = qu[b++]; i < 4; i++)
      for (int nx = x + d[i], u = 0;
           u < k && 0 <= nx && nx < h * w && mp[nx] > mp[x]; u++, nx += d[i])
        if (mp[nx] == inf)
          mp[qu[e++] = nx] = mp[x] + 1;
  printf("%d\\n", mp[x2 * w + y2] == inf ? -1 : mp[x2 * w + y2]);
}
Problem p02644 (C++) — buggy submission, verdict: 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)]) {
            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;
}


Error: TLE

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)]) {
            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, verdict: 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 {
          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;
}


Error: TLE

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 {
          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, verdict: 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();
    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;
}

Error: TLE

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();
    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, verdict: 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] == '@' 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]);
}

Error: TLE

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] == '@'; }

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]);
}