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

#pragma GCC optimize("Ofast")
#define _USE_MATH_DEFINES
#include "bits/stdc++.h"

using namespace std;

using u8 = uint8_t;
using u16 = uint16_t;
using u32 = uint32_t;
using u64 = uint64_t;
using i8 = int8_t;
using i16 = int16_t;
using i32 = int32_t;
using i64 = int64_t;

constexpr char newl = '\\n';
constexpr double eps = 1e-10;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define F0R(i, b) FOR(i, 0, b)
#define RFO(i, a, b) for (int i = ((b)-1); i >= (a); i--)
#define RF0(i, b) RFO(i, 0, b)
#define fi first
#define se second
#define debug(x) cout << #x << ": " << x << '\\n';
#define rng(a) a.begin(), a.end()
#define rrng(a) a.rbegin(), a.rend()

template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}
template <class T> void Print(vector<T> v) {
  F0R(i, v.size()) { cout << v[i] << ' '; }
  cout << newl;
}

#if 1

enum class Dir { None, Up, Left, Down, Right };

// INSERT ABOVE HERE
signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  int H, W, K;
  cin >> H >> W >> K;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  y1--;
  x2--;
  y2--;
  vector<string> cs(H);
  F0R(i, H) { cin >> cs[i]; }

  int inf = 10000000;
  vector<vector<int>> dp(H, vector<int>(W, inf + 1));

  queue<tuple<int, int, int, Dir>> qu;
  qu.emplace(x1, y1, 0, Dir::None);
  dp[x1][y1] = 0;

  while (qu.size()) {
    auto [r, c, d, dir] = qu.front();
    qu.pop();

    if (r == x2 && c == y2)
      break;
    d++;

    int up = 0, left = 0, down = 0, right = 0;

    if (dir != Dir::Down) {
      FOR(i, 1, min(K + 1, r + 1)) {
        if (cs[r - i][c] == '@')
          break;
        up++;
      }
    }
    if (dir != Dir::Right) {
      FOR(i, 1, min(K + 1, c + 1)) {
        if (cs[r][c - i] == '@')
          break;
        left++;
      }
    }
    if (dir != Dir::Up) {
      FOR(i, 1, min(K + 1, H - r)) {
        if (cs[r + i][c] == '@')
          break;
        down++;
      }
    }
    if (dir != Dir::Left) {
      FOR(i, 1, min(K + 1, W - c)) {
        if (cs[r][c + i] == '@')
          break;
        right++;
      }
    }

    RFO(i, 1, up + 1) {
      if (dp[r - i][c] > d) {
        dp[r - i][c] = d;
        qu.emplace(r - i, c, d, Dir::Up);
      }
    }
    RFO(i, 1, left + 1) {
      if (dp[r][c - i] > d) {
        dp[r][c - i] = d;
        qu.emplace(r, c - i, d, Dir::Left);
      }
    }
    RFO(i, 1, down + 1) {
      if (dp[r + i][c] > d) {
        dp[r + i][c] = d;
        qu.emplace(r + i, c, d, Dir::Down);
      }
    }
    RFO(i, 1, right + 1) {

      if (dp[r][c + i] > d) {
        dp[r][c + i] = d;
        qu.emplace(r, c + i, d, Dir::Right);
      }
    }
  }

  cout << (dp[x2][y2] > inf ? -1 : dp[x2][y2]);
}
#endif


Error: TLE

Fixed submission:

#pragma GCC optimize("Ofast")
#define _USE_MATH_DEFINES
#include "bits/stdc++.h"

using namespace std;

using u8 = uint8_t;
using u16 = uint16_t;
using u32 = uint32_t;
using u64 = uint64_t;
using i8 = int8_t;
using i16 = int16_t;
using i32 = int32_t;
using i64 = int64_t;

constexpr char newl = '\\n';
constexpr double eps = 1e-10;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define F0R(i, b) FOR(i, 0, b)
#define RFO(i, a, b) for (int i = ((b)-1); i >= (a); i--)
#define RF0(i, b) RFO(i, 0, b)
#define fi first
#define se second
#define debug(x) cout << #x << ": " << x << '\\n';
#define rng(a) a.begin(), a.end()
#define rrng(a) a.rbegin(), a.rend()

template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}
template <class T> void Print(vector<T> v) {
  F0R(i, v.size()) { cout << v[i] << ' '; }
  cout << newl;
}

#if 1

// INSERT ABOVE HERE
signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  int H, W, K;
  cin >> H >> W >> K;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  y1--;
  x2--;
  y2--;
  vector<string> cs(H);
  F0R(i, H) { cin >> cs[i]; }

  int inf = 10000000;
  vector<vector<int>> dp(H, vector<int>(W, inf + 1));

  queue<tuple<int, int, int>> qu;
  qu.emplace(x1, y1, 0);
  dp[x1][y1] = 0;

  while (qu.size()) {
    auto [r, c, d] = qu.front();
    qu.pop();

    if (r == x2 && c == y2)
      break;
    d++;

    int up = 0, left = 0, down = 0, right = 0;

    FOR(i, 1, min(K + 1, r + 1)) {
      if (cs[r - i][c] == '@')
        break;
      up++;
    }
    FOR(i, 1, min(K + 1, c + 1)) {
      if (cs[r][c - i] == '@')
        break;
      left++;
    }

    FOR(i, 1, min(K + 1, H - r)) {
      if (cs[r + i][c] == '@')
        break;
      right++;
    }
    FOR(i, 1, min(K + 1, W - c)) {
      if (cs[r][c + i] == '@')
        break;
      down++;
    }
    RFO(i, 1, K + 1) {
      if (i <= up) {
        if (dp[r - i][c] > d) {
          dp[r - i][c] = d;
          qu.emplace(r - i, c, d);
        }
      }
      if (i <= left) {
        if (dp[r][c - i] > d) {
          dp[r][c - i] = d;
          qu.emplace(r, c - i, d);
        }
      }
      if (i <= right) {
        if (dp[r + i][c] > d) {
          dp[r + i][c] = d;
          qu.emplace(r + i, c, d);
        }
      }
      if (i <= down) {
        if (dp[r][c + i] > d) {
          dp[r][c + i] = d;
          qu.emplace(r, c + i, d);
        }
      }
    }
  }

  cout << (dp[x2][y2] > inf ? -1 : dp[x2][y2]);
}
#endif