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

#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef pair<ll, ll> P;
typedef vector<P> vpl;
typedef tuple<ll, ll, ll> tapu;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, a, b) for (int i = (a); i < (b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = 1 << 30;
const ll linf = 1LL << 62;
const int MAX = 510000;
ll dy[8] = {1, -1, 0, 0, 1, -1, 1, -1};
ll dx[8] = {0, 0, 1, -1, 1, -1, -1, 1};
const double pi = acos(-1);
const double eps = 1e-7;
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
  if (a > b) {
    a = b;
    return true;
  } else
    return false;
}
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
  if (a < b) {
    a = b;
    return true;
  } else
    return false;
}
template <typename T> inline void print(T &a) {
  rep(i, a.size()) cout << a[i] << " ";
  cout << "\\n";
}
template <typename T1, typename T2> inline void print2(T1 a, T2 b) {
  cout << a << " " << b << "\\n";
}
template <typename T1, typename T2, typename T3>
inline void print3(T1 a, T2 b, T3 c) {
  cout << a << " " << b << " " << c << "\\n";
}
const int mod = 1e9 + 7;
// const int mod = 998244353;
typedef pair<int, int> pii;

struct dat {
  int t, y, x, r, dir;
  bool operator<(const dat &d) const { return t > d.t; }
};

bool in(ll y, ll x, ll h, ll w) { return 0 <= y && y < h && 0 <= x && x < w; }

int main() {
  int h, w, k;
  cin >> h >> w >> k;
  int sy, sx, gy, gx;
  cin >> sy >> sx >> gy >> gx;
  sy--;
  sx--;
  gy--;
  gx--;
  vs s(h);
  rep(i, h) cin >> s[i];
  vector<vector<vector<int>>> v(h, vector<vector<int>>(w, vector<int>(4, inf)));
  rep(i, 4) v[sy][sx][i] = 0;
  priority_queue<dat> q;
  rep(i, 4) q.push({0, sy, sx, 0, i});
  while (!q.empty()) {
    dat d = q.top();
    q.pop();
    rep(i, 4) {
      int ny = d.y + dy[i];
      int nx = d.x + dx[i];
      if (!in(ny, nx, h, w))
        continue;
      if (s[ny][nx] == '@')
        continue;
      if (d.dir == i) {
        int t = d.t;
        int r = d.r;
        if (r == 0)
          r = k - 1, t++;
        else
          r--;
        if (v[ny][nx][i] <= t)
          continue;
        v[ny][nx][i] = t;
        q.push({t, ny, nx, r, i});
      } else {
        if (v[ny][nx][i] <= d.t + 1)
          continue;
        v[ny][nx][i] = d.t + 1;
        q.push({d.t + 1, ny, nx, k - 1, i});
      }
    }
  }
  int ans = inf;
  /*rep(k,4){
      rep(i,h){
          rep(j,w) cout << v[i][j][k] << " ";
          cout << "\\n";
      }
      cout << "\\n";
  }*/
  rep(i, 4) { chmin(ans, v[gy][gx][i]); }
  if (ans == inf)
    puts("-1");
  else
    cout << ans << "\\n";
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef pair<ll, ll> P;
typedef vector<P> vpl;
typedef tuple<ll, ll, ll> tapu;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, a, b) for (int i = (a); i < (b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = 1 << 30;
const ll linf = 1LL << 62;
const int MAX = 510000;
ll dy[8] = {1, -1, 0, 0, 1, -1, 1, -1};
ll dx[8] = {0, 0, 1, -1, 1, -1, -1, 1};
const double pi = acos(-1);
const double eps = 1e-7;
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
  if (a > b) {
    a = b;
    return true;
  } else
    return false;
}
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
  if (a < b) {
    a = b;
    return true;
  } else
    return false;
}
template <typename T> inline void print(T &a) {
  rep(i, a.size()) cout << a[i] << " ";
  cout << "\\n";
}
template <typename T1, typename T2> inline void print2(T1 a, T2 b) {
  cout << a << " " << b << "\\n";
}
template <typename T1, typename T2, typename T3>
inline void print3(T1 a, T2 b, T3 c) {
  cout << a << " " << b << " " << c << "\\n";
}
const int mod = 1e9 + 7;
// const int mod = 998244353;
typedef pair<int, int> pii;

struct dat {
  int y, x, r, t, dir;
};

bool in(ll y, ll x, ll h, ll w) { return 0 <= y && y < h && 0 <= x && x < w; }

int main() {
  int h, w, k;
  cin >> h >> w >> k;
  int sy, sx, gy, gx;
  cin >> sy >> sx >> gy >> gx;
  sy--;
  sx--;
  gy--;
  gx--;
  vs s(h);
  rep(i, h) cin >> s[i];
  vector<vector<vector<int>>> v(h, vector<vector<int>>(w, vector<int>(4, inf)));
  rep(i, 4) v[sy][sx][i] = 0;
  queue<dat> q;
  rep(i, 4) q.push({sy, sx, 0, 0, i});
  while (!q.empty()) {
    dat d = q.front();
    q.pop();
    rep(i, 4) {
      int ny = d.y + dy[i];
      int nx = d.x + dx[i];
      if (!in(ny, nx, h, w))
        continue;
      if (s[ny][nx] == '@')
        continue;
      if (d.dir == i) {
        int t = d.t;
        int r = d.r;
        if (r == 0)
          r = k - 1, t++;
        else
          r--;
        if (v[ny][nx][i] <= t)
          continue;
        v[ny][nx][i] = t;
        q.push({ny, nx, r, t, i});
      } else {
        if (v[ny][nx][i] <= d.t + 1)
          continue;
        v[ny][nx][i] = d.t + 1;
        q.push({ny, nx, k - 1, d.t + 1, i});
      }
    }
  }
  int ans = inf;
  /*rep(k,4){
      rep(i,h){
          rep(j,w) cout << v[i][j][k] << " ";
          cout << "\\n";
      }
      cout << "\\n";
  }*/
  rep(i, 4) { chmin(ans, v[gy][gx][i]); }
  if (ans == inf)
    puts("-1");
  else
    cout << ans << "\\n";
}