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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <vector>

#define SPBR(w, n) std::cout << (w + 1 == n ? '\\n' : ' ');
#define YES cout << "YES" << endl
#define Yes cout << "Yes" << endl
#define NO cout << "NO" << endl
#define No cout << "No" << endl
#define ALL(i) (i).begin(), (i).end()
#define FOR(i, a, n) for (int i = (a); i < (n); ++i)
#define RFOR(i, a, n) for (int i = (n)-1; i >= (a); --i)
#define REP(i, n) for (int i = 0; i < int(n); ++i)
#define RREP(i, n) for (int i = int(n) - 1; i >= 0; --i)
#define IN(a, x, b) (a <= x && x < b)
#define OUT(a, x, b) (x < a || b <= x)
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;
}

#define int ll
using ll = long long;
using ull = unsigned long long;
using ld = long double;
const int MOD = 1000000007;
/* const int MOD = 998244353; */
const int INF = 1e18;
const double PI = acos(-1);

using namespace std;
struct INIT {
  INIT() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
  }
} INIT;

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

int H, W, K;
int sx, sy, gx, gy;

vector<string> s;
vector<vector<int>> dist;

signed main() {
  cin >> H >> W >> K;

  cin >> sx >> sy >> gx >> gy;
  sx--;
  sy--;
  gx--;
  gy--;

  s.resize(H);
  REP(i, H) cin >> s[i];

  dist = vector<vector<int>>(H, vector<int>(W, INF));
  dist[sx][sy] = 0;

  queue<pair<int, int>> q;
  q.emplace(sx, sy);

  while (!q.empty()) {
    auto [x, y] = q.front();
    q.pop();
    REP(i, 4) {
      FOR(j, 1, K + 1) {
        int nx = x + dx[i] * j;
        int ny = y + dy[i] * j;
        if (OUT(0, nx, H) || OUT(0, ny, W))
          break;
        if (s[nx][ny] == '@')
          break;
        if (dist[nx][ny] <= dist[x][y])
          break;
        if (dist[nx][ny] > dist[x][y] + 1) {
          dist[nx][ny] = dist[x][y] + 1;
          q.emplace(nx, ny);
        }
      }
    }
  }

  if (dist[gx][gy] == INF)
    dist[gx][gy] = -1;
  cout << dist[gx][gy] << "\\n";

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <vector>

#define SPBR(w, n) std::cout << (w + 1 == n ? '\\n' : ' ');
#define YES cout << "YES" << endl
#define Yes cout << "Yes" << endl
#define NO cout << "NO" << endl
#define No cout << "No" << endl
#define ALL(i) (i).begin(), (i).end()
#define FOR(i, a, n) for (int i = (a); i < (n); ++i)
#define RFOR(i, a, n) for (int i = (n)-1; i >= (a); --i)
#define REP(i, n) for (int i = 0; i < int(n); ++i)
#define RREP(i, n) for (int i = int(n) - 1; i >= 0; --i)
#define IN(a, x, b) (a <= x && x < b)
#define OUT(a, x, b) (x < a || b <= x)
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;
}

#define int ll
using ll = long long;
using ull = unsigned long long;
using ld = long double;
const int MOD = 1000000007;
/* const int MOD = 998244353; */
const int INF = 1e18;
const double PI = acos(-1);

using namespace std;
struct INIT {
  INIT() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
  }
} INIT;

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

int H, W, K;
int sx, sy, gx, gy;

vector<string> s;
vector<vector<int>> dist;

signed main() {
  cin >> H >> W >> K;

  cin >> sx >> sy >> gx >> gy;
  sx--;
  sy--;
  gx--;
  gy--;

  s.resize(H);
  REP(i, H) cin >> s[i];

  dist = vector<vector<int>>(H, vector<int>(W, INF));
  dist[sx][sy] = 0;

  queue<pair<int, int>> q;
  q.emplace(sx, sy);

  while (!q.empty()) {
    auto [x, y] = q.front();
    q.pop();
    REP(i, 4) {
      FOR(j, 1, K + 1) {
        int nx = x + dx[i] * j;
        int ny = y + dy[i] * j;
        if (OUT(0, nx, H) || OUT(0, ny, W))
          break;
        ;
        if (s[nx][ny] == '@')
          break;
        ;
        if (dist[nx][ny] > dist[x][y] + 1) {
          dist[nx][ny] = dist[x][y] + 1;
          q.emplace(nx, ny);
        }
      }
    }
  }

  if (dist[gx][gy] == INF)
    dist[gx][gy] = -1;
  cout << dist[gx][gy] << "\\n";

  return 0;
}