Problem p02644 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef uint64_t u64;
typedef int64_t s64;
typedef uint32_t u32;
typedef int32_t s32;
typedef vector<s32> vs32;
typedef vector<u32> vu32;
typedef vector<s64> vs64;
typedef vector<u64> vu64;

const double PI = 3.14159265358979323846;

#define MAX(x, y) ((x) < (y) ? (y) : (x))
#define MIN(x, y) ((x) > (y) ? (y) : (x))

#define rep(i, N) for (int i = 0; i < N; ++i)

#define CEIL(x, y) (((x) + (y)-1) / (y))
#define MOD 1000000007ULL

#define IN(l, r, x) ((l) <= (x) && (x) < (r))

using P = pair<s64, int>;

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

  s64 h, w, k;
  cin >> h >> w >> k;

  int si, sj, ti, tj;
  cin >> si >> sj >> ti >> tj;
  --si, --sj, --ti, --tj;
  vector<string> field(h);
  rep(i, h) cin >> field[i];

  const s64 inf = 1e18;
  vector<vector<bool>> reached(h, vector<bool>(w, false));
  vs64 d(h * w, inf);
  auto id = [&](int i, int j) { return i * w + j; };

  queue<int> q;
  q.push(id(si, sj));
  d[id(si, sj)] = 0;
  reached[si][sj] = true;
  int dy[] = {-1, 0, 1, 0};
  int dx[] = {0, -1, 0, 1};
  while (!q.empty()) {
    int from = q.front();
    q.pop();
    int fi = from / w;
    int fj = from % w;
    rep(i, 4) {
      rep(j, k) {
        s64 ni = fi + dy[i] * (j + 1);
        s64 nj = fj + dx[i] * (j + 1);
        if (!(IN(0, h, ni) && IN(0, w, nj)))
          break;
        int to = id(ni, nj);
        if (d[to] < d[from] + 1 || field[ni][nj] == '@')
          break;
        if (d[to] != inf)
          continue;
        reached[ni][nj] = true;
        d[to] = d[from] + 1;
        q.push(to);
      }
    }
  }

  s64 ans = d[id(ti, tj)];
  if (!reached[ti][tj])
    cout << "-1\\n";
  else
    cout << ans << "\\n";

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef uint64_t u64;
typedef int64_t s64;
typedef uint32_t u32;
typedef int32_t s32;
typedef vector<s32> vs32;
typedef vector<u32> vu32;
typedef vector<s64> vs64;
typedef vector<u64> vu64;

const double PI = 3.14159265358979323846;

#define MAX(x, y) ((x) < (y) ? (y) : (x))
#define MIN(x, y) ((x) > (y) ? (y) : (x))

#define rep(i, N) for (int i = 0; i < N; ++i)

#define CEIL(x, y) (((x) + (y)-1) / (y))
#define MOD 1000000007ULL

#define IN(l, r, x) ((l) <= (x) && (x) < (r))

using P = pair<s64, int>;

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

  s64 h, w, k;
  cin >> h >> w >> k;

  int si, sj, ti, tj;
  cin >> si >> sj >> ti >> tj;
  --si, --sj, --ti, --tj;
  vector<string> field(h);
  rep(i, h) cin >> field[i];

  const s64 inf = 1e18;
  vector<vector<bool>> reached(h, vector<bool>(w, false));
  vs64 d(h * w, inf);
  auto id = [&](int i, int j) { return i * w + j; };

  queue<int> q;
  q.push(id(si, sj));
  d[id(si, sj)] = 0;
  reached[si][sj] = true;
  int dy[] = {-1, 0, 1, 0};
  int dx[] = {0, -1, 0, 1};
  while (!q.empty()) {
    int from = q.front();
    q.pop();
    int fi = from / w;
    int fj = from % w;
    rep(i, 4) {
      rep(j, k) {
        s64 ni = fi + dy[i] * (j + 1);
        s64 nj = fj + dx[i] * (j + 1);
        if (!(IN(0, h, ni) && IN(0, w, nj)))
          break;
        int to = id(ni, nj);
        if (d[to] < d[from] + 1 || field[ni][nj] == '@')
          break;
        reached[ni][nj] = true;
        d[to] = d[from] + 1;
        q.push(to);
      }
    }
  }

  s64 ans = d[id(ti, tj)];
  if (!reached[ti][tj])
    cout << "-1\\n";
  else
    cout << ans << "\\n";

  return 0;
}