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

#ifdef MY_LOCAL
#define MY_NAMESPACE(ns) namespace ns {
#define MY_NAMESPACE_ }
#define MY_DEBUG(s) s
#else
#define MY_NAMESPACE(ns)
#define MY_NAMESPACE_
#define MY_DEBUG(s)
#endif

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <tuple>
#include <unordered_map>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define EPS (1e-7)
#define IINF ((int)1e9)

MY_NAMESPACE(testbed)
int main() {
  cin.tie(nullptr);
  ios::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<vector<char>> pond(h, vector<char>(w));
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> pond[i][j];
    }
  }

  vector<vector<vector<int>>> min_step(
      h, vector<vector<int>>(w, vector<int>(4, IINF)));
  int dx[] = {1, 0, -1, 0};
  int dy[] = {0, 1, 0, -1};
  typedef tuple<int, int, int, int> T4int; // {step, x, y, direction(0-3)}
  priority_queue<T4int, vector<T4int>, greater<T4int>> q;
  for (int i = 0; i <= 3; i++) {
    min_step[x1][y1][i] = 0;
    q.emplace(0, x1, y1, i);
  }
  while (!q.empty()) {
    int this_step, this_x, this_y, this_dir;
    tie(this_step, this_x, this_y, this_dir) = q.top();
    q.pop();

    // change the direction
    for (int i = 0; i <= 3; i++) {
      int next_step = ((this_step + k - 1) / k) * k;
      if (min_step[this_x][this_y][i] > next_step) {
        min_step[this_x][this_y][i] = next_step;
        q.emplace(next_step, this_x, this_y, i);
      }
    }

    // one step along with the direction
    int next_x = this_x + dx[this_dir];
    int next_y = this_y + dy[this_dir];
    int next_dir = this_dir;
    if (next_x < 0 || h <= next_x || next_y < 0 || w <= next_y)
      continue;
    if (min_step[next_x][next_y][next_dir] <= this_step)
      continue;
    if (pond[next_x][next_y] == '@')
      continue;
    min_step[next_x][next_y][next_dir] = this_step + 1;
    q.emplace(this_step + 1, next_x, next_y, next_dir);
  }

  int res = IINF;
  for (int i = 0; i <= 3; i++) {
    if (min_step[x2][y2][i] == IINF)
      continue;
    int actual_step = (min_step[x2][y2][i] + k - 1) / k;
    res = min(res, actual_step);
  }
  if (res == IINF)
    res = -1;
  cout << res << "\\n";
  return 0;
}
MY_NAMESPACE_

Error: TLE

Fixed submission:

#ifdef MY_LOCAL
#define MY_NAMESPACE(ns) namespace ns {
#define MY_NAMESPACE_ }
#define MY_DEBUG(s) s
#else
#define MY_NAMESPACE(ns)
#define MY_NAMESPACE_
#define MY_DEBUG(s)
#endif

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <tuple>
#include <unordered_map>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define EPS (1e-7)
#define IINF ((int)1e9)

MY_NAMESPACE(testbed)
int main() {
  cin.tie(nullptr);
  ios::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<vector<char>> pond(h, vector<char>(w));
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> pond[i][j];
    }
  }

  vector<vector<vector<int>>> min_step(
      h, vector<vector<int>>(w, vector<int>(4, IINF)));
  int dx[] = {1, 0, -1, 0};
  int dy[] = {0, 1, 0, -1};
  typedef tuple<int, int, int, int> T4int; // {step, x, y, direction(0-3)}
  priority_queue<T4int, vector<T4int>, less<T4int>> q;
  for (int i = 0; i <= 3; i++) {
    min_step[x1][y1][i] = 0;
    q.emplace(0, x1, y1, i);
  }
  while (!q.empty()) {
    int this_step, this_x, this_y, this_dir;
    tie(this_step, this_x, this_y, this_dir) = q.top();
    q.pop();

    // change the direction
    for (int i = 0; i <= 3; i++) {
      int next_step = ((this_step + k - 1) / k) * k;
      if (min_step[this_x][this_y][i] > next_step) {
        min_step[this_x][this_y][i] = next_step;
        q.emplace(next_step, this_x, this_y, i);
      }
    }

    // one step along with the direction
    int next_x = this_x + dx[this_dir];
    int next_y = this_y + dy[this_dir];
    int next_dir = this_dir;
    if (next_x < 0 || h <= next_x || next_y < 0 || w <= next_y)
      continue;
    if (min_step[next_x][next_y][next_dir] <= this_step)
      continue;
    if (pond[next_x][next_y] == '@')
      continue;
    min_step[next_x][next_y][next_dir] = this_step + 1;
    q.emplace(this_step + 1, next_x, next_y, next_dir);
  }

  int res = IINF;
  for (int i = 0; i <= 3; i++) {
    if (min_step[x2][y2][i] == IINF)
      continue;
    int actual_step = (min_step[x2][y2][i] + k - 1) / k;
    res = min(res, actual_step);
  }
  if (res == IINF)
    res = -1;
  cout << res << "\\n";
  return 0;
}
MY_NAMESPACE_