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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll INF = ll(1e18) + 5;
ll H, W, K;

bool onBoard(int y, int x) {
  if (y >= 0 && y < H && x >= 0 && x < W) {
    return true;
  }
  return false;
}

int main() {
  cin >> H >> W >> K;
  int sx, sy, gx, gy;
  cin >> sy >> sx >> gy >> gx;
  --sy;
  --sx;
  --gx;
  --gy;
  vector<string> S(H);
  for (int i = 0; i < H; i++) {
    cin >> S[i];
  }
  int dy[] = {1, 0, -1, 0};
  int dx[] = {0, 1, 0, -1};

  ll d[H][W];
  for (int i = 0; i < H; i++)
    for (int j = 0; j < W; j++) {
      d[i][j] = INF;
    }
  priority_queue<P, vector<P>, greater<P>> pq;
  pq.push(P(0, sy * W + sx));
  d[sy][sx] = 0;
  while (!pq.empty()) {
    P p = pq.top();
    ll y = p.second / W;
    ll x = p.second % W;
    // cerr <<"y=" << y <<"x=" <<x  <<"d=" <<d[y][x]<<endl;
    pq.pop();
    if (p.first > d[y][x]) {
      continue;
    }
    for (int i = 0; i < 4; i++) {
      for (int j = 1; j <= K; j++) {
        ll ny = y + dy[i] * j;
        ll nx = x + dx[i] * j;
        if (!onBoard(ny, nx) || S[ny][nx] == '@') {
          break;
        }
        if (d[ny][nx] < d[y][x] + 1) {
          // cerr <<"ny=" <<ny <<"nx="<<nx <<"breaked"<<endl;
          break;
        }
        if (d[ny][nx] <= d[y][x] + 1) {
          continue;
        }

        // cerr <<"ny=" <<ny <<"nx=" <<nx << endl;
        d[ny][nx] = d[y][x] + 1;
        pq.push(P(d[ny][nx], ny * W + nx));
      }
    }
  }
  if (d[gy][gx] == INF) {
    d[gy][gx] = -1;
  }
  cout << d[gy][gx] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll INF = ll(1e18) + 5;
ll H, W, K;

bool onBoard(int y, int x) {
  if (y >= 0 && y < H && x >= 0 && x < W) {
    return true;
  }
  return false;
}

int main() {
  cin >> H >> W >> K;
  int sx, sy, gx, gy;
  cin >> sy >> sx >> gy >> gx;
  --sy;
  --sx;
  --gx;
  --gy;
  vector<string> S(H);
  for (int i = 0; i < H; i++) {
    cin >> S[i];
  }
  int dy[] = {1, 0, -1, 0};
  int dx[] = {0, 1, 0, -1};

  ll d[H][W];
  for (int i = 0; i < H; i++)
    for (int j = 0; j < W; j++) {
      d[i][j] = INF;
    }
  priority_queue<P, vector<P>, greater<P>> pq;
  pq.push(P(0, sy * W + sx));
  d[sy][sx] = 0;
  while (!pq.empty()) {
    P p = pq.top();
    ll y = p.second / W;
    ll x = p.second % W;
    // cerr <<"y=" << y <<"x=" <<x  <<"d=" <<d[y][x]<<endl;
    pq.pop();
    if (p.first > d[y][x]) {
      continue;
    }
    for (int i = 0; i < 4; i++) {
      for (int j = 1; j <= K; j++) {
        ll ny = y + dy[i] * j;
        ll nx = x + dx[i] * j;
        if (!onBoard(ny, nx) || S[ny][nx] == '@') {
          break;
        }
        if (d[ny][nx] < d[y][x] + 1) {
          break;
        }

        // cerr <<"ny=" <<ny <<"nx=" <<nx << endl;
        d[ny][nx] = d[y][x] + 1;
        pq.push(P(d[ny][nx], ny * W + nx));
      }
    }
  }
  if (d[gy][gx] == INF) {
    d[gy][gx] = -1;
  }
  cout << d[gy][gx] << endl;
  return 0;
}
Problem p02644 (C++) — buggy submission, verdict: 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;
}

Error: -11

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

Error: TLE

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]) {
            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, verdict: 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;
        if (!seen[nx][ny])
          next.push({nx, ny});
        seen[nx][ny] = true;
      }
    }
  }
  cout << dist[goal.first][goal.second];
}

Error: TLE

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;
        next.push({nx, ny});
        seen[nx][ny] = true;
      }
    }
  }
  cout << dist[goal.first][goal.second];
}
Problem p02644 (C++) — buggy submission, verdict: 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]) {
          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;
}

Error: TLE

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

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

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

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

Error: TLE

Fixed submission:

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

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

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

  cout << dist[goal.first][goal.second] << endl;
}
Problem p02644 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ld = long double;
using uint = unsigned int;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

/* [💭] [💡] [🎈] */

using namespace std;

vector<string> b;
vector<vector<int>> dist;

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

int main() {
  cin.tie(nullptr);
  cout.tie(nullptr);
  ios_base::sync_with_stdio(false);

  int h, w, k;
  cin >> h >> w >> k;
  b.resize(h);
  dist.resize(h);
  for (auto &v : dist)
    v.resize(w);

  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  for (int i = 0; i < h; i++)
    cin >> b[i];
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      b[i] += '.';
  x1--, y1--, x2--, y2--;

  queue<pii> q;
  q.emplace(x1, y1);
  while (q.size()) {
    int x = q.front().first, y = q.front().second;
    if (x == x2 && y == y2)
      break;
    q.pop();
    for (int d = 0; d < 4; d++) {
      int nx = x, ny = y;
      for (int l = 1; l <= k; l++) {
        nx += dx[d], ny += dy[d];
        if (0 > nx || nx >= h)
          break;
        if (0 > ny || ny >= w)
          break;
        if (b[nx][ny] == '@')
          break;
        if (dist[nx][ny]) {
          if (dist[nx][ny] <= dist[x][y])
            break;
          continue;
        }
        if (nx == x1 && ny == y1)
          continue;
        dist[nx][ny] = dist[x][y] + 1;
        q.emplace(nx, ny);
      }
    }
  }

  if (!dist[x2][y2]) {
    cout << -1;
  } else {
    cout << dist[x2][y2];
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ld = long double;
using uint = unsigned int;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

/* [💭] [💡] [🎈] */

using namespace std;

vector<string> b;
vector<vector<int>> dist;

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

int main() {
  cin.tie(nullptr);
  cout.tie(nullptr);
  ios_base::sync_with_stdio(false);

  int h, w, k;
  cin >> h >> w >> k;
  b.resize(h);
  dist.resize(h);
  for (auto &v : dist)
    v.resize(w);

  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  for (int i = 0; i < h; i++)
    cin >> b[i];
  x1--, y1--, x2--, y2--;

  queue<pii> q;
  q.emplace(x1, y1);
  while (q.size()) {
    int x = q.front().first, y = q.front().second;
    if (x == x2 && y == y2)
      break;
    q.pop();
    for (int d = 0; d < 4; d++) {
      for (int l = 1; l <= k; l++) {
        int nx = x + l * dx[d], ny = y + l * dy[d];
        if (0 > nx || nx >= h)
          break;
        if (0 > ny || ny >= w)
          break;
        if (b[nx][ny] == '@')
          break;
        if (dist[nx][ny]) {
          if (dist[nx][ny] < dist[x][y])
            break;
          continue;
        }
        if (nx == x1 && ny == y1)
          continue;
        dist[nx][ny] = dist[x][y] + 1;
        q.emplace(nx, ny);
      }
    }
  }

  if (!dist[x2][y2]) {
    cout << -1;
  } else {
    cout << dist[x2][y2];
  }

  return 0;
}
Problem p02644 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

typedef long long int ll;
typedef pair<ll, ll> pll;

#define FOR(i, n, m) for (ll(i) = (m); (i) < (n); ++(i))
#define REP(i, n) FOR(i, n, 0)
#define OF64 std::setprecision(10)

const ll MOD = 1000000007;
const ll INF = (ll)1e15;

ll x[4] = {0, 1, 0, -1};
ll y[4] = {1, 0, -1, 0};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll H, W, K;
  cin >> H >> W >> K;
  pll s, g;
  cin >> s.first >> s.second >> g.first >> g.second;
  s.first--;
  s.second--;
  g.first--;
  g.second--;

  vector<string> S(H);
  vector<vector<vector<ll>>> C(H);
  REP(i, H) { cin >> S[i]; }

  REP(i, H) {
    vector<vector<ll>> v(W);
    REP(j, W) {
      v[j] = vector<ll>();
      REP(k, 4) { v[j].push_back(INF); }
    }
    C[i] = v;
  }

  queue<tuple<pll, ll>> q;
  q.push(make_tuple(s, 0));
  REP(i, 4) { C[s.first][s.second][i] = 0; }
  while (!q.empty()) {
    auto t = q.front();
    q.pop();
    pll p = get<0>(t);
    ll cost = get<1>(t) + 1;
    REP(i, 4) {
      REP(d, K) {
        ll nx = x[i] * (d + 1) + p.first, ny = y[i] * (d + 1) + p.second;
        if (nx < 0 || nx >= H || ny < 0 || ny >= W)
          break;
        if (S[nx][ny] == '@')
          break;
        if (C[nx][ny][i] <= cost)
          break;
        bool e = false;
        REP(k, 4) {
          if (C[nx][ny][k] < cost)
            e = true;
        }
        if (e)
          break;
        C[nx][ny][i] = cost;
        q.push(make_tuple(pll(nx, ny), cost));
      }
    }
  }

  ll ans = INF;
  REP(i, 4) { ans = std::min(C[g.first][g.second][i], ans); }
  if (ans == INF)
    ans = -1;
  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long int ll;
typedef pair<ll, ll> pll;

#define FOR(i, n, m) for (ll(i) = (m); (i) < (n); ++(i))
#define REP(i, n) FOR(i, n, 0)
#define OF64 std::setprecision(10)

const ll MOD = 1000000007;
const ll INF = (ll)1e15;

ll x[4] = {0, 1, 0, -1};
ll y[4] = {1, 0, -1, 0};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll H, W, K;
  cin >> H >> W >> K;
  pll s, g;
  cin >> s.first >> s.second >> g.first >> g.second;
  s.first--;
  s.second--;
  g.first--;
  g.second--;

  vector<string> S(H);
  vector<vector<ll>> C(H);
  REP(i, H) { cin >> S[i]; }

  REP(i, H) {
    REP(j, W) { C[i].push_back(INF); }
  }

  queue<tuple<pll, ll>> q;
  q.push(make_tuple(s, 0));
  C[s.first][s.second] = 0;
  while (!q.empty()) {
    auto t = q.front();
    q.pop();
    pll p = get<0>(t);
    ll cost = get<1>(t) + 1;
    REP(i, 4) {
      REP(d, K) {
        ll nx = x[i] * (d + 1) + p.first, ny = y[i] * (d + 1) + p.second;
        if (nx < 0 || nx >= H || ny < 0 || ny >= W)
          break;
        if (S[nx][ny] == '@')
          break;
        if (C[nx][ny] < cost)
          break;
        C[nx][ny] = cost;
        q.push(make_tuple(pll(nx, ny), cost));
      }
    }
  }

  if (C[g.first][g.second] == INF)
    cout << -1 << endl;
  else
    cout << C[g.first][g.second] << endl;

  return 0;
}