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

#include <algorithm>
#include <iostream>
#include <queue>
#include <string>
#include <tuple>
#include <vector>
using namespace std;

using P = tuple<int, int, int>;
using T = tuple<long, P>;
const long inf = 1L << 60;

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> a(H);
  for (auto &s : a)
    cin >> s;

  vector d(H, vector(W, vector(4, inf)));
  priority_queue<T, vector<T>, greater<T>> q;
  for (int i = 0; i < 4; ++i) {
    q.emplace(0L, P({sx, sy, i}));
    d[sx][sy][i] = 0;
  }

  int dx[4] = {-1, 0, 1, 0};
  int dy[4] = {0, -1, 0, 1};
  while (!q.empty()) {
    auto [cost, pos] = q.top();
    q.pop();
    auto [x, y, z] = pos;

    uint nx = x + dx[z];
    uint ny = y + dy[z];
    auto nc = cost + 1;
    P np = {nx, ny, z};

    if (!(nx >= H or ny >= W or a[nx][ny] == '@')) {
      if (d[nx][ny][z] > nc) {
        q.emplace(nc, np);
        d[nx][ny][z] = nc;
      }
    }
    for (int i = 0; i < 4; ++i)
      if (i != z) {
        np = {x, y, i};
        nc = (cost + K - 1) / K * K;
        if (d[x][y][i] <= nc)
          continue;
        q.emplace(nc, np);
        d[x][y][i] = nc;
      }
  }

  long res = inf;
  for (int i = 0; i < 4; ++i)
    res = min(res, d[gx][gy][i]);
  cout << (res < inf ? (res + K - 1) / K : -1) << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <tuple>
#include <vector>
using namespace std;

using P = tuple<int, int, int>;
using T = tuple<long, P>;
const long inf = 1L << 60;

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> a(H);
  for (auto &s : a)
    cin >> s;

  priority_queue<T, vector<T>, greater<T>> q;
  for (int i = 0; i < 4; ++i)
    q.emplace(0L, P({sx, sy, i}));
  map<P, long> d;
  d[{sx, sy, 0}] = 0;

  int dx[4] = {-1, 0, 1, 0};
  int dy[4] = {0, -1, 0, 1};
  while (!q.empty()) {
    auto [cost, pos] = q.top();
    q.pop();
    auto [x, y, z] = pos;

    uint nx = x + dx[z];
    uint ny = y + dy[z];
    auto nc = cost + 1;
    P np = {nx, ny, z};

    if (!(nx >= H or ny >= W or a[nx][ny] == '@')) {
      if (!(d.count(np) and d[np] <= nc)) {
        q.emplace(nc, np);
        d[np] = nc;
      }
    }
    for (int i = 0; i < 4; ++i)
      if (i != z) {
        np = {x, y, i};
        nc = (cost + K - 1) / K * K;
        if (d.count(np) and d[np] <= nc)
          continue;
        q.emplace(nc, np);
        d[np] = nc;
      }
  }

  long res = inf;
  for (int i = 0; i < 4; ++i)
    if (d.count({gx, gy, i}))
      res = min(res, d[{gx, gy, i}]);
  cout << (res < inf ? (res + K - 1) / K : -1) << endl;
}
Problem p02644 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cassert>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
int main() {
  int h, w, k;
  cin >> h >> w >> k;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  --x1;
  --y1;
  --x2;
  --y2;
  vector<string> c(h);
  rep(i, h) cin >> c[i];

  vector<vector<int>> d(h, vector<int>(w, -1));
  queue<P> q;
  auto push = [&](int x, int y, int nd) {
    if (x < 0 || h <= x || y < 0 || w <= y)
      return false;
    if (c[x][y] == '@')
      return false;
    if (d[x][y] != -1 && nd > d[x][y])
      return false;
    if (d[x][y] != -1 && nd == d[x][y])
      return true;
    d[x][y] = nd;
    q.emplace(x, y);
    return true;
  };
  push(x1, y1, 0);
  while (!q.empty()) {
    P p = q.front();
    q.pop();
    int x = p.first, y = p.second;
    int nd = d[x][y] + 1;
    rep(i, 4) rep(j, k) {
      if (!push(x + dx[i] * (j + 1), y + dy[i] * (j + 1), nd))
        break;
    }
  }
  cout << d[x2][y2] << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
int main() {
  int h, w, k;
  cin >> h >> w >> k;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  --x1;
  --y1;
  --x2;
  --y2;
  vector<string> c(h);
  rep(i, h) cin >> c[i];

  vector<vector<int>> d(h, vector<int>(w, -1));
  queue<P> q;
  auto push = [&](int x, int y, int nd) {
    if (x < 0 || h <= x || y < 0 || w <= y)
      return false;
    if (c[x][y] == '@')
      return false;
    if (d[x][y] != -1 && nd >= d[x][y])
      return true;
    d[x][y] = nd;
    q.emplace(x, y);
    return true;
  };
  push(x1, y1, 0);
  while (!q.empty()) {
    P p = q.front();
    q.pop();
    int x = p.first, y = p.second;
    int di = d[x][y];
    rep(i, 4) {
      int v = 1;
      while (true) {
        if (!push(x + dx[i] * v, y + dy[i] * v, di + (v + k - 1) / k))
          break;
        ++v;
      }
    }
  }
  cout << d[x2][y2] << endl;
}
Problem p02644 (C++) — buggy submission, verdict: 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;
}

Error: TLE

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, verdict: 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";
}

Error: TLE

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, verdict: Time Limit Exceeded

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

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

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);
  int h, w, k;
  cin >> h >> w >> k;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  --x1, --y1, --x2, --y2;
  char c;
  vector<vector<int>> g(h, vector<int>(w));
  rep(i, h) rep(j, w) {
    cin >> c;
    if (c == '@')
      g[i][j] = -1;
  }
  g[x1][y1] = 1;
  queue<P> q;
  q.emplace(x1, y1);
  while (!q.empty()) {
    int x = q.front().first;
    int y = q.front().second;
    q.pop();
    rep(i, 4) {
      for (int j = 1; j <= k; ++j) {
        int nx = x + dx[i] * j;
        int ny = y + dy[i] * j;
        if (nx < 0 || ny < 0 || nx >= h || ny >= w || g[nx][ny] == -1)
          break;
        if (g[nx][ny] != 0 && g[nx][ny] <= g[x][y])
          break;
        if (g[nx][ny] == 0) {
          g[nx][ny] = g[x][y] + 1;
          q.emplace(nx, ny);
        }
      }
    }
  }
  cout << g[x2][y2] - 1 << endl;
}

Error: TLE

Fixed submission:

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

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

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);
  int h, w, k;
  cin >> h >> w >> k;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  --x1, --y1, --x2, --y2;
  char c;
  vector<vector<ll>> g(h, vector<ll>(w));
  rep(i, h) rep(j, w) {
    cin >> c;
    if (c == '@')
      g[i][j] = -1;
  }
  g[x1][y1] = 1;
  queue<P> q;
  q.emplace(x1, y1);
  while (!q.empty()) {
    int x = q.front().first;
    int y = q.front().second;
    q.pop();
    rep(i, 4) {
      for (int j = 1; j <= k; ++j) {
        int nx = x + dx[i] * j;
        int ny = y + dy[i] * j;
        if (nx < 0 || ny < 0 || nx >= h || ny >= w || g[nx][ny] == -1)
          break;
        if (g[nx][ny] != 0 && g[nx][ny] <= g[x][y])
          break;
        g[nx][ny] = g[x][y] + 1;
        q.emplace(nx, ny);
      }
    }
  }
  cout << g[x2][y2] - 1 << endl;
}
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 rrep(i, n) for (int i = (n - 1); i >= 0; i--)
#define ALL(v) v.begin(), v.end()
using namespace std;
using P = pair<int, int>;
typedef long long ll;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int INF = (1 << 30);

int main() {
  int h, w, k;
  cin >> h >> w >> k;
  vector<vector<int>> dist(h, vector<int>(w, INF));
  vector<string> grid(h);
  int sx, sy, gx, gy;
  cin >> sx >> sy >> gx >> gy;
  sx--, sy--, gx--, gy--;
  rep(i, h) cin >> grid[i];
  dist[sx][sy] = 0;
  queue<P> que;
  que.push(P(sx, sy));
  while (!que.empty()) {
    P p = que.front();
    que.pop();
    rep(i, 4) {
      for (int j = 1; j <= k; j++) {
        int nx = p.first + j * dx[i], ny = p.second + j * dy[i];
        if (nx < 0 || ny < 0 || nx >= h || ny >= w)
          break;
        if (grid[nx][ny] == '@')
          break;
        if (dist[nx][ny] <= dist[p.first][p.second])
          break;
        if (dist[nx][ny] != INF && dist[p.first][p.second] >= dist[nx][ny])
          break;
        if (dist[nx][ny] != INF)
          continue;
        dist[nx][ny] = dist[p.first][p.second] + 1;
        que.push(P(nx, ny));
      }
    }
  }
  if (dist[gx][gy] == INF) {
    cout << -1 << endl;
  } else {
    cout << dist[gx][gy] << endl;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = (n - 1); i >= 0; i--)
#define ALL(v) v.begin(), v.end()
using namespace std;
using P = pair<int, int>;
using PP = pair<int, P>;
typedef long long ll;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int INF = (1 << 30);

int main() {
  int h, w, k;
  cin >> h >> w >> k;
  vector<vector<int>> dist(h, vector<int>(w, INF));
  vector<string> grid(h);
  int sx, sy, gx, gy;
  cin >> sx >> sy >> gx >> gy;
  sx--, sy--, gx--, gy--;
  rep(i, h) cin >> grid[i];
  dist[sx][sy] = 0;
  priority_queue<PP, vector<PP>, greater<PP>> que;
  que.push(PP(0, P(sx, sy)));
  while (!que.empty()) {
    PP p = que.top();
    que.pop();
    rep(i, 4) {
      for (int j = 1; j <= k; j++) {
        int nx = p.second.first + j * dx[i], ny = p.second.second + j * dy[i];
        if (nx < 0 || ny < 0 || nx >= h || ny >= w)
          break;
        if (grid[nx][ny] == '@')
          break;
        if (dist[nx][ny] <= dist[p.second.first][p.second.second] + 1)
          continue;
        dist[nx][ny] = dist[p.second.first][p.second.second] + 1;
        que.push(PP(dist[nx][ny], P(nx, ny)));
      }
    }
  }
  if (dist[gx][gy] == INF) {
    cout << -1 << endl;
  } else {
    cout << dist[gx][gy] << endl;
  }
}
Problem p02644 (C++) — buggy submission, verdict: 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>;

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

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

  queue<P> q;
  q.push(P(sx, sy));
  dist[sx][sy] = 0;
  while (!q.empty()) {
    P p = q.front();
    q.pop();
    int cx = p.first;
    int cy = p.second;
    if (cx == gx && cy == gy)
      break;
    rep(i, 4) {
      for (int j = 1; j <= k; j++) {
        int nx = cx + dx[i] * j;
        int ny = cy + dy[i] * j;
        if (0 > nx || nx >= h || 0 > ny || ny >= w)
          break;
        if (maze[nx][ny] == '@')
          break;
        if (dist[nx][ny] != -1 && dist[nx][ny] <= dist[cx][cy])
          break;
        if (dist[nx][ny] != -1)
          continue;
        dist[nx][ny] = dist[cx][cy] + 1;
        q.push(P(nx, ny));
      }
    }
  }
  cout << dist[gx][gy] << endl;
  return 0;
}


Error: TLE

Fixed submission:

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

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

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

  queue<P> q;
  q.push(P(sx, sy));
  dist[sx][sy] = 0;
  while (!q.empty()) {
    P p = q.front();
    q.pop();
    if (p.first == gx && p.second == gy)
      break;
    rep(i, 4) {
      for (int j = 1; j <= k; j++) {
        int nx = p.first + dx[i] * j;
        int ny = p.second + dy[i] * j;
        if (0 > nx || nx >= h || 0 > ny || ny >= w)
          break;
        if (maze[nx][ny] == '@')
          break;
        if (dist[nx][ny] != -1)
          continue;
        dist[nx][ny] = dist[p.first][p.second] + 1;
        q.push(P(nx, ny));
      }
    }
  }
  cout << dist[gx][gy] << endl;
  return 0;
}
Problem p02644 (C++) — buggy submission, verdict: 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);
  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;
}


Error: WA

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);
  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, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using lint = long long;
using P = pair<int, int>;
using vec = vector<int>;
using mat = vector<vector<int>>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
#define endl "\\n"

constexpr int MOD = 1000000007;
const int INF = 1 << 30;

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

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

  mat dist(h, vec(w, INF));
  dist[sy][sx] = 0;

  queue<P> que;
  que.push({sy, sx});

  while (!que.empty()) {
    auto fr = que.front();
    que.pop();

    rep(i, 4) {
      for (int j = 1; j <= k; j++) {
        int ty = fr.first + dy[i] * j;
        int tx = fr.second + dx[i] * j;
        if (ty >= 0 && ty < h && tx >= 0 && tx < w) {
          if (g[ty][tx] == '@')
            break;
          if (dist[ty][tx] <= dist[fr.first][fr.second])
            break;
          if (dist[ty][tx] == INF) {
            dist[ty][tx] = dist[fr.first][fr.second] + 1;
            que.push({ty, tx});
          }
        }
      }
    }
  }

  if (dist[gy][gx] != INF)
    cout << dist[gy][gx] << endl;
  else
    cout << "-1" << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using lint = long long;
using P = pair<int, int>;
using IP = pair<int, pair<int, int>>;
using vec = vector<int>;
using mat = vector<vector<int>>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
#define endl "\\n"

constexpr int MOD = 1000000007;
const int INF = 1 << 30;

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

int main() {
  int h, w, k;
  cin >> h >> w >> k;
  int sx, sy, gx, gy;
  cin >> sy >> sx >> gy >> gx;
  sx--;
  sy--;
  gx--;
  gy--;
  // グリッド
  vector<string> g(h);
  rep(i, h) cin >> g[i];

  // (sy, sx)からの距離
  mat dist(h, vec(w, INF));
  dist[sy][sx] = 0;

  priority_queue<IP, vector<IP>, greater<IP>> que;
  que.push(IP(0, P(sy, sx)));

  while (!que.empty()) {
    auto fr = que.top();
    que.pop();

    int tmp_dist = fr.first;
    P from = fr.second;

    if (dist[from.first][from.second] < tmp_dist)
      continue;

    rep(i, 4) {
      for (int j = 1; j <= k; j++) {
        int ty = from.first + dy[i] * j;
        int tx = from.second + dx[i] * j;
        if (ty >= 0 && ty < h && tx >= 0 && tx < w) {
          if (g[ty][tx] == '@')
            break;
          if (dist[ty][tx] > dist[from.first][from.second]) {
            dist[ty][tx] = dist[from.first][from.second] + 1;
            que.push(IP(dist[ty][tx], P(ty, tx)));
          } else {
            break;
          }
        }
      }
    }
  }

  if (dist[gy][gx] != INF)
    cout << dist[gy][gx] << endl;
  else
    cout << "-1" << endl;
  return 0;
}