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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
#define per1(i, n) for (int i = n; i > 0; i--)
#define all(v) v.begin(), v.end()
typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<ll> vec;
typedef vector<vec> mat;
typedef pair<ll, P> PP;
typedef pair<ll, PP> PPP;
int h, w, k, sx, sy, gx, gy;
vector<vector<char>> C;
ll INF = 1e18;
int dx[4] = {0, 0, 1, -1}, dy[4] = {1, -1, 0, 0};
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> h >> w >> k >> sx >> sy >> gx >> gy;
  sx--, sy--, gx--, gy--;
  vec B(w, INF);
  ll d[4][h][w];
  rep(i, h) {
    vector<char> A;
    rep(j, w) {
      char a;
      cin >> a;
      A.push_back(a);
      rep(k, 4) d[k][i][j] = INF;
    }
    C.push_back(A);
  }
  priority_queue<PPP, vector<PPP>, greater<PPP>> que;
  d[0][sx][sy] = 0;
  que.push(PPP(0, PP(0, P(sx, sy))));
  while (!que.empty()) {
    PPP p = que.top();
    que.pop();
    ll dir = p.second.first, vx = p.second.second.first,
       vy = p.second.second.second;
    if (d[dir][vx][vy] < p.first)
      continue;
    ll nx = vx + dx[dir], ny = vy + dy[dir];
    if (!(nx < 0 || nx >= h || ny < 0 || ny >= w || C[nx][ny] == '@'))
      if (d[dir][nx][ny] > d[dir][vx][vy] + 1) {
        d[dir][nx][ny] = d[dir][vx][vy] + 1;
        ll x = d[dir][nx][ny];
        que.push(PPP(x, PP(dir, P(nx, ny))));
      }
    if (d[(dir + 1) % 4][vx][vy] > (d[dir][vx][vy] + k - 1) / k * k) {
      d[(dir + 1) % 4][vx][vy] = (d[dir][vx][vy] + k - 1) / k * k;
      ll x = d[(dir + 1) % 4][vx][vy];
      que.push(PPP(x, PP((dir + 1) % 4, P(vx, vy))));
    }
  }
  if (d[0][gx][gy] == INF)
    cout << -1;
  else
    cout << (d[0][gx][gy] + k - 1) / k;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
#define per1(i, n) for (int i = n; i > 0; i--)
#define all(v) v.begin(), v.end()
typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<ll> vec;
typedef vector<vec> mat;
typedef pair<ll, P> PP;
typedef pair<ll, PP> PPP;
int h, w, k, sx, sy, gx, gy;
map<PP, ll> d;
vector<vector<char>> C;
ll INF = 1e18;
int dx[4] = {0, 0, 1, -1}, dy[4] = {1, -1, 0, 0};
ll dijkstra() {
  priority_queue<PPP, vector<PPP>, greater<PPP>> que;
  d[PP(0, P(sx, sy))] = 0;
  que.push(PPP(0, PP(0, P(sx, sy))));
  while (!que.empty()) {
    PPP p = que.top();
    que.pop();
    ll dir = p.second.first, vx = p.second.second.first,
       vy = p.second.second.second;
    if (d[PP(dir, P(vx, vy))] < p.first)
      continue;
    ll nx = vx + dx[dir], ny = vy + dy[dir];
    if (!(nx < 0 || nx >= h || ny < 0 || ny >= w || C[nx][ny] == '@'))
      if (d[PP(dir, P(nx, ny))] > d[PP(dir, P(vx, vy))] + 1) {
        d[PP(dir, P(nx, ny))] = d[PP(dir, P(vx, vy))] + 1;
        ll x = d[PP(dir, P(nx, ny))];
        que.push(PPP(x, PP(dir, P(nx, ny))));
      }
    if (d[PP((dir + 1) % 4, P(vx, vy))] >
        (d[PP(dir, P(vx, vy))] + k - 1) / k * k) {
      d[PP((dir + 1) % 4, P(vx, vy))] = (d[PP(dir, P(vx, vy))] + k - 1) / k * k;
      ll x = d[PP((dir + 1) % 4, P(vx, vy))];
      que.push(PPP(x, PP((dir + 1) % 4, P(vx, vy))));
    }
  }
  if (d[PP(0, P(gx, gy))] == INF)
    return -1;
  else
    return (d[PP(0, P(gx, gy))] + k - 1) / k;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> h >> w >> k >> sx >> sy >> gx >> gy;
  sx--, sy--, gx--, gy--;
  vec B(w, INF);
  rep(i, h) {
    vector<char> A;
    rep(j, w) {
      char a;
      cin >> a;
      A.push_back(a);
      rep(k, 4) d[PP(k, P(i, j))] = INF;
    }
    C.push_back(A);
  }
  cout << dijkstra();
}