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

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, l, r) for (long long i = (l); i < (r); ++i)
#define REP(i, n) FOR(i, 0, n)
#define REPS(i, n) FOR(i, 1, n + 1)
#define RFOR(i, l, r) for (long long i = (l); i >= (r); --i)
#define RREP(i, n) RFOR(i, n - 1, 0)
#define RREPS(i, n) RFOR(i, n, 1)
#define int long long
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define SZ(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
const int INF = 1e18;

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

struct Data {
  int x, y, turn;
  Data(int x, int y, int turn) : x(x), y(y), turn(turn) {}
};

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int h, w, k;
  cin >> h >> w >> k;
  vector<vector<int>> maze(h, vector<int>(w, INF));
  int sx, sy, gx, gy;
  cin >> sx >> sy >> gx >> gy;
  sx--;
  sy--;
  gx--;
  gy--;
  REP(i, h) REP(j, w) {
    char c;
    cin >> c;
    if (c == '@')
      maze[i][j] = -1;
  }
  queue<Data> que;
  que.push({sx, sy, 0});
  while (que.size()) {
    int nx = que.front().x, ny = que.front().y, turn = que.front().turn;
    que.pop();
    REP(i, 4) {
      REPS(j, k) {
        int x = nx + dx[i] * j, y = ny + dy[i] * j;
        if (!(x > -1 and x < h and y > -1 and y < w))
          break;
        if (maze[x][y] <= turn)
          break;
        if (maze[x][y] > turn + 1) {
          que.push({x, y, turn + 1});
          maze[x][y] = turn + 1;
        }
      }
    }
  }
  cout << (maze[gx][gy] != INF ? maze[gx][gy] : -1) << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, l, r) for (long long i = (l); i < (r); ++i)
#define REP(i, n) FOR(i, 0, n)
#define REPS(i, n) FOR(i, 1, n + 1)
#define RFOR(i, l, r) for (long long i = (l); i >= (r); --i)
#define RREP(i, n) RFOR(i, n - 1, 0)
#define RREPS(i, n) RFOR(i, n, 1)
#define int long long
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define SZ(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
const int INF = 1e18;

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

struct Data {
  int x, y, turn;
  Data(int x, int y, int turn) : x(x), y(y), turn(turn) {}
};

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int h, w, k;
  cin >> h >> w >> k;
  vector<vector<double>> maze(h, vector<double>(w, INF));
  int sx, sy, gx, gy;
  cin >> sx >> sy >> gx >> gy;
  sx--;
  sy--;
  gx--;
  gy--;
  REP(i, h) REP(j, w) {
    char c;
    cin >> c;
    if (c == '@')
      maze[i][j] = -1;
  }
  queue<Data> que;
  que.push({sx, sy, 0});
  while (que.size()) {
    int nx = que.front().x, ny = que.front().y, turn = que.front().turn;
    que.pop();
    REP(i, 4) {
      REPS(j, k) {
        int x = nx + dx[i] * j, y = ny + dy[i] * j;
        if (x > -1 and x < h and y > -1 and y < w and maze[x][y] > turn + 1) {
          que.push({x, y, turn + 1});
          maze[x][y] = turn + 1.1;
        } else
          break;
      }
    }
  }
  cout << (maze[gx][gy] != INF ? (int)maze[gx][gy] : -1) << endl;
}