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

#include <bits/stdc++.h>
using namespace std;
#define FastRead                                                               \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define int long long int
#define ll int
#define bits_count __builtin_popcountll
#define endl '\\n'
#define double long double
#define ld double
#define FOR(i, a, n) for (ll i = (a); i <= (n); ++i)
#define RFOR(i, a, n) for (ll i = (n); i >= (a); --i)
#define ZERO(a) memset((a), 0, sizeof((a)))
#define MINUS(a) memset((a), -1, sizeof((a)))
#define f first
#define s second
#define pb push_back
#define mk make_pair
#define all(g) g.begin(), g.end()
#define sz(x) (ll) x.size()
#define pr pair<int, int>
int fastMax(int x, int y) { return (((y - x) >> (32 - 1)) & (x ^ y)) ^ y; }
int fastMin(int x, int y) { return (((y - x) >> (32 - 1)) & (x ^ y)) ^ x; }

// #include <ext/pb_ds/assoc_container.hpp> // Common file
// #include <ext/pb_ds/tree_policy.hpp>     // Including
// tree_order_statistics_node_updat using namespace __gnu_pbds; typedef tree<ll,
// null_type, less_equal<ll>, rb_tree_tag, tree_order_statistics_node_update>
// ordered_set; const int RANDOM =
// chrono::high_resolution_clock::now().time_since_epoch().count(); struct chash
// { int operator()(int x) const { return x ^ RANDOM; }}; cc_hash_table<int,
// int, hash<int>> cnt;

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

vector<vector<int>> dist[5];
vector<vector<char>> a;

#define prx pair<pair<int, pair<int, int>>, pair<int, int>>
void solve() {
  int H, W, K;
  cin >> H >> W >> K;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;

  a.resize(H + 1);
  FOR(i, 0, H) a[i].resize(W + 1, '@');

  FOR(j, 0, 4) {
    dist[j].resize(H + 1);
    FOR(i, 0, H) dist[j][i].resize(W + 1, 1e10);
  }
  FOR(k, 0, 4) FOR(i, 0, H) FOR(j, 0, W) dist[k][i][j] = 1e15;
  FOR(i, 1, H) FOR(j, 1, W) cin >> a[i][j];

  priority_queue<prx, vector<prx>, greater<prx>> pq;

  pq.push(mk(mk(0, mk(0, 4)), mk(x1, y1)));
  dist[4][x1][y1] = 0;

  while (pq.size()) {
    auto top = pq.top();
    pq.pop();
    int dis = top.f.f;
    int k_left = top.f.s.f, dir = top.f.s.s;
    int x = top.s.f, y = top.s.s;

    FOR(d, 0, 3) {
      int nx = x + dx[d], ny = y + dy[d];

      if (nx < 1 || ny < 1 || nx > H || ny > W)
        continue;
      if (a[nx][ny] == '@')
        continue;

      int n_dis, n_k_left;

      if (dir == d) {
        if (k_left > 0) {
          n_k_left = k_left - 1;
          n_dis = dis;
        } else
          n_k_left = K - 1, n_dis = dis + 1;
      } else {
        n_k_left = K - 1;
        n_dis = dis + 1;
      }

      if (n_dis >= dist[d][nx][ny])
        continue;
      else {
        pq.push(mk(mk(n_dis, mk(n_k_left, d)), mk(nx, ny)));
        dist[d][nx][ny] = n_dis;
      }
    }
  }

  int ans = 1e10;
  FOR(i, 0, 4) ans = min(ans, dist[i][x2][y2]);

  if (ans >= 1e10)
    cout << -1 << endl;
  else
    cout << ans << endl;
}

signed main() {

  FastRead;

  int t = 1;
  // cin>>t;
  FOR(i, 1, t) {
    // cout<<"Case #"<<i<<": ";
    solve();
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define FastRead                                                               \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define int long long int
#define ll int
#define bits_count __builtin_popcountll
#define endl '\\n'
#define double long double
#define ld double
#define FOR(i, a, n) for (ll i = (a); i <= (n); ++i)
#define RFOR(i, a, n) for (ll i = (n); i >= (a); --i)
#define ZERO(a) memset((a), 0, sizeof((a)))
#define MINUS(a) memset((a), -1, sizeof((a)))
#define f first
#define s second
#define pb push_back
#define mk make_pair
#define all(g) g.begin(), g.end()
#define sz(x) (ll) x.size()
#define pr pair<int, int>
int fastMax(int x, int y) { return (((y - x) >> (32 - 1)) & (x ^ y)) ^ y; }
int fastMin(int x, int y) { return (((y - x) >> (32 - 1)) & (x ^ y)) ^ x; }

// #include <ext/pb_ds/assoc_container.hpp> // Common file
// #include <ext/pb_ds/tree_policy.hpp>     // Including
// tree_order_statistics_node_updat using namespace __gnu_pbds; typedef tree<ll,
// null_type, less_equal<ll>, rb_tree_tag, tree_order_statistics_node_update>
// ordered_set; const int RANDOM =
// chrono::high_resolution_clock::now().time_since_epoch().count(); struct chash
// { int operator()(int x) const { return x ^ RANDOM; }}; cc_hash_table<int,
// int, hash<int>> cnt;

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

vector<vector<int>> dist[5];
vector<vector<char>> a;

void solve() {
  int H, W, K;
  cin >> H >> W >> K;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;

  a.resize(H + 1);
  FOR(i, 0, H) a[i].resize(W + 1, '@');

  FOR(j, 0, 4) {
    dist[j].resize(H + 1);
    FOR(i, 0, H) dist[j][i].resize(W + 1, 1e10);
  }
  FOR(k, 0, 4) FOR(i, 0, H) FOR(j, 0, W) dist[k][i][j] = 1e15;
  FOR(i, 1, H) FOR(j, 1, W) cin >> a[i][j];

  queue<pair<pair<int, int>, pair<int, int>>> q;

  q.push(make_pair(make_pair(4, 0), make_pair(x1, y1)));
  dist[4][x1][y1] = 0;

  while (q.size()) {
    auto top = q.front();
    q.pop();

    int x = top.s.f, y = top.s.s;
    int dir = top.f.f;
    int k_used = top.f.s;

    FOR(d, 0, 3) {
      FOR(dis, 1, K) {
        int nx = x + dis * dx[d], ny = y + dis * dy[d];
        if (nx < 1 || ny < 1 || nx > H || ny > W)
          break;
        if (a[nx][ny] == '@' || dist[d][nx][ny] < dist[dir][x][y] + 1)
          break;
        if (dist[d][nx][ny] == dist[dir][x][y] && dir == d && k_used <= dis)
          break;
        dist[d][nx][ny] = dist[dir][x][y] + 1;
        q.push(make_pair(make_pair(d, dis), make_pair(nx, ny)));
      }
    }
  }

  int ans = 1e15;
  FOR(i, 0, 4) { ans = min(ans, dist[i][x2][y2]); }

  if (ans >= 1e10)
    cout << -1 << endl;
  else
    cout << ans << endl;
}

signed main() {

  FastRead;

  int t = 1;
  // cin>>t;
  FOR(i, 1, t) {
    // cout<<"Case #"<<i<<": ";
    solve();
  }
}