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

#include <bits/stdc++.h>
using namespace std;

const vector<int> dx = {1, 0, -1, 0};
const vector<int> dy = {0, 1, 0, -1};
const int64_t INF = INT64_MAX;

class State {
public:
  int x, y, d;

  State(int x0, int y0, int d0) {
    x = x0;
    y = y0;
    d = d0;
  }

  bool operator<(const State &other) const { return x < other.x; }
};

int main() {
  int H, W, K;
  cin >> H >> W >> K;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  vector<string> c(H);
  for (int i = 0; i < H; i++) {
    cin >> c.at(i);
  }

  x1--;
  y1--;
  x2--;
  y2--;

  vector<vector<vector<int64_t>>> g(
      H, vector<vector<int64_t>>(W, vector<int64_t>(4, INF)));
  priority_queue<pair<int64_t, State>, vector<pair<int64_t, State>>,
                 greater<pair<int64_t, State>>>
      q;
  auto push = [&](int x, int y, int d, int64_t c) {
    if (g.at(x).at(y).at(d) <= c)
      return;
    g.at(x).at(y).at(d) = c;
    q.push(make_pair(c, State(x, y, d)));
  };
  for (int d = 0; d < 4; d++) {
    push(x1, y1, d, 0);
  }

  while (!q.empty()) {
    int64_t cost = q.top().first;
    int x = q.top().second.x, y = q.top().second.y, d = q.top().second.d;
    q.pop();

    for (int nd = 0; nd < 4; nd++)
      push(x, y, nd, (cost + K - 1) / K * K);

    int nx = x + dx.at(d), ny = y + dy.at(d);
    if (nx < 0 || H <= nx || ny < 0 || W <= ny)
      continue;
    if (c.at(nx).at(ny) == '@')
      continue;
    push(x + dx.at(d), y + dy.at(d), d, cost + 1);
  }

  int64_t res = INF;
  for (int d = 0; d < 4; d++) {
    if (g.at(x2).at(y2).at(d) == INF)
      continue;
    int64_t t = g.at(x2).at(y2).at(d);
    res = min(res, (t + K - 1) / K);
  }
  cout << (res == INF ? -1 : res) << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

const vector<int> dx = {1, 0, -1, 0};
const vector<int> dy = {0, 1, 0, -1};
const int64_t INF = INT64_MAX;

class State {
public:
  int x, y, d;

  State(int x0, int y0, int d0) {
    x = x0;
    y = y0;
    d = d0;
  }
};

int main() {
  int H, W, K;
  cin >> H >> W >> K;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  vector<string> c(H);
  for (int i = 0; i < H; i++) {
    cin >> c.at(i);
  }

  x1--;
  y1--;
  x2--;
  y2--;

  vector<vector<vector<int64_t>>> cost(
      H, vector<vector<int64_t>>(W, vector<int64_t>(4, INF)));
  queue<State> q;
  for (int d = 0; d < 4; d++) {
    cost.at(x1).at(y1).at(d) = 0;
    q.push(State(x1, y1, d));
  }

  while (!q.empty()) {
    int x = q.front().x, y = q.front().y, d = q.front().d;
    q.pop();

    for (int k = 1; k <= K; k++) {
      int nx = x + k * dx.at(d);
      int ny = y + k * dy.at(d);
      if (nx < 0 || H <= nx || ny < 0 || W <= ny)
        break;
      if (c.at(nx).at(ny) == '@')
        break;
      for (int nd = 0; nd < 4; nd++) {
        if (cost.at(nx).at(ny).at(nd) != INF)
          continue;
        cost.at(nx).at(ny).at(nd) = cost.at(x).at(y).at(d) + 1;
        if (d != nd) {
          q.push(State(nx, ny, nd));
        }
      }
      if (k == K) {
        q.push(State(nx, ny, d));
      }
    }
  }

  int64_t res = INF;
  for (int d = 0; d < 4; d++) {
    res = min(res, cost.at(x2).at(y2).at(d));
  }
  cout << (res == INF ? -1 : res) << endl;

  return 0;
}