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

// 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++;

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

  /*
  F0R(r, H) {
    F0R(c, W) {
      if (dp[r][c] > inf) {
        cout << "* ";
      }
      else cout << dp[r][c] << ' ';
    }
    cout << newl;
  }
  //*/

  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++;

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

  /*
  F0R(r, H) {
    F0R(c, W) {
      if (dp[r][c] > inf) {
        cout << "* ";
      }
      else cout << dp[r][c] << ' ';
    }
    cout << newl;
  }
  //*/

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