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

#include <bits/stdc++.h>
#define f first
#define s second
#define fore(i, a, b) for (int i = (a), ThxMK = (b); i < ThxMK; ++i)
#define pb push_back
#define all(s) begin(s), end(s)
#define _                                                                      \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define sz(s) int(s.size())
#define ENDL '\\n'
using namespace std;
typedef long double ld;
typedef long long lli;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<lli> vl;
typedef vector<ii> vii;
typedef vector<vi> vvi;
typedef vector<bool> vb;
#define deb(x) cout << #x ": " << (x) << endl;

const int N = 1e3 + 5;
const lli INF = 1e18;
const lli MOD = 1e9 + 7;

bool mm[N];
vi primes(1, 2);
ld nk[N][N];
void sieve() {
  for (int i = 4; i < N; i += 2)
    mm[i] = 1;
  for (int i = 3; i * i <= N; i += 2)
    if (!mm[i])
      for (int j = i * i; j < N; j += i)
        mm[j] = 1;
  for (int i = 3; i < N; i += 2)
    if (!mm[i])
      primes.pb(i);
}

void pascal() {
  fore(i, 0, N) nk[i][0] = nk[i][i] = 1.0;
  fore(i, 1, N) fore(j, 1, i) nk[i][j] = nk[i - 1][j - 1] + nk[i - 1][j];
}

lli gcd(lli a, lli b) { return (b ? gcd(b, a % b) : a); }

lli lcm(lli a, lli b) {
  if (a < b)
    swap(a, b);
  lli c = gcd(a, b);
  a /= c;
  return a * b;
}

int popcount(lli x) { return __builtin_popcountll(x); }

lli poww(lli a, lli b) {
  lli res = 1;
  while (b) {
    if (b & 1)
      res = res * a;
    a = a * a;
    b /= 2;
  }
  return res;
}

lli powm(lli a, lli b) {
  lli res = 1;
  while (b) {
    if (b & 1)
      res = (res * a) % MOD;
    a = (a * a) % MOD;
    b /= 2;
  }
  return res;
}

bool isPrime(lli x) {
  if (x == 1)
    return 0;
  for (auto i : primes) {
    if (i * i > x)
      return 1;
    if (x % i == 0)
      return 0;
  }
  return 1;
}

// ---- コーディングはここから！ ('-')7

struct SS {
  int x, y, z;
  SS(int x, int y, int z) : x(x), y(y), z(z) {}
};
int main() {
  int n, m, k;
  cin >> n >> m >> k;
  int sx, sy, tx, ty;
  cin >> sx >> sy >> tx >> ty;
  sx--;
  sy--;
  tx--;
  ty--;
  vector<string> s(n);
  fore(i, 0, n) cin >> s[i];
  queue<SS> q;
  vi pos, st;
  pos = st = vi(n * m * 4, 0);
  auto W = [&](int a, int b, int c) { return (a * m + b) * 4 + c; };
  auto valid = [&](int x, int y) {
    return x >= 0 and y >= 0 and x < n and y < m and s[x][y] != '@';
  };
  fore(i, 0, 4) {
    pos[W(sx, sy, i)] = 1;
    q.emplace(sx, sy, i);
  }
  int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
  int tt = 0;
  while (!q.empty()) {
    queue<SS> co;
    while (!q.empty()) {
      auto [x, y, z] = q.front();
      q.pop();
      if (st[W(x, y, z)])
        continue;
      st[W(x, y, z)] = 1;
      fore(i, 0, k) {
        x += dx[z];
        y += dy[z];
        if (!valid(x, y))
          break;
        if (pos[W(x, y, z)])
          break;
        fore(j, 0, 4) if (j != z or i == k - 1) co.emplace(x, y, j);
      }
    }
    while (!co.empty()) {
      auto [x, y, z] = co.front();
      co.pop();
      if (pos[W(x, y, z)])
        continue;
      q.emplace(x, y, z);
      pos[W(x, y, z)] = 1;
    }
    tt++;
    fore(i, 0, 4) if (pos[W(tx, ty, i)]) {
      cout << tt << ENDL;
      return 0;
    }
  }
  cout << -1 << ENDL;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define f first
#define s second
#define fore(i, a, b) for (int i = (a), ThxMK = (b); i < ThxMK; ++i)
#define pb push_back
#define all(s) begin(s), end(s)
#define _                                                                      \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define sz(s) int(s.size())
#define ENDL '\\n'
using namespace std;
typedef long double ld;
typedef long long lli;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<lli> vl;
typedef vector<ii> vii;
typedef vector<vi> vvi;
typedef vector<bool> vb;
#define deb(x) cout << #x ": " << (x) << endl;

const int N = 1e3 + 5;
const lli INF = 1e18;
const lli MOD = 1e9 + 7;

bool mm[N];
vi primes(1, 2);
ld nk[N][N];
void sieve() {
  for (int i = 4; i < N; i += 2)
    mm[i] = 1;
  for (int i = 3; i * i <= N; i += 2)
    if (!mm[i])
      for (int j = i * i; j < N; j += i)
        mm[j] = 1;
  for (int i = 3; i < N; i += 2)
    if (!mm[i])
      primes.pb(i);
}

void pascal() {
  fore(i, 0, N) nk[i][0] = nk[i][i] = 1.0;
  fore(i, 1, N) fore(j, 1, i) nk[i][j] = nk[i - 1][j - 1] + nk[i - 1][j];
}

lli gcd(lli a, lli b) { return (b ? gcd(b, a % b) : a); }

lli lcm(lli a, lli b) {
  if (a < b)
    swap(a, b);
  lli c = gcd(a, b);
  a /= c;
  return a * b;
}

int popcount(lli x) { return __builtin_popcountll(x); }

lli poww(lli a, lli b) {
  lli res = 1;
  while (b) {
    if (b & 1)
      res = res * a;
    a = a * a;
    b /= 2;
  }
  return res;
}

lli powm(lli a, lli b) {
  lli res = 1;
  while (b) {
    if (b & 1)
      res = (res * a) % MOD;
    a = (a * a) % MOD;
    b /= 2;
  }
  return res;
}

bool isPrime(lli x) {
  if (x == 1)
    return 0;
  for (auto i : primes) {
    if (i * i > x)
      return 1;
    if (x % i == 0)
      return 0;
  }
  return 1;
}

// ---- コーディングはここから！ ('-')7

int n, m;
vector<string> x;
vvi dis;
int dx[4] = {1, 0, 0, -1}, dy[4] = {0, 1, -1, 0};
bool valid(int a, int b) {
  return a >= 0 and a < n and b >= 0 and b < m and x[a][b] != '@';
}
int main() {
  _ cin >> n >> m;
  int a, b, c, d, k;
  cin >> k >> a >> b >> c >> d;
  a--;
  b--;
  c--;
  d--;
  dis = vvi(n, vi(m, -1));
  x = vector<string>(n);
  fore(i, 0, n) cin >> x[i];
  if (!valid(a, b) or !valid(c, d)) {
    cout << -1 << ENDL;
    return 0;
  }
  dis[a][b] = 0;
  queue<ii> q;
  q.push({a, b});
  while (!q.empty()) {
    int ax = q.front().f, ay = q.front().s;
    int di = dis[ax][ay];
    q.pop();
    fore(i, 0, 4) {
      int cx = ax, cy = ay;
      fore(j, 1, k + 1) {
        int bx = cx + dx[i], by = cy + dy[i];
        if (!valid(bx, by))
          break;
        if (dis[bx][by] == -1) {
          q.push({bx, by});
          dis[bx][by] = di + 1;
          if (bx == c and by == d) {
            cout << dis[bx][by] << ENDL;
            return 0;
          }
        }
        cx = bx;
        cy = by;
      }
    }
  }
  cout << -1 << ENDL;
}