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

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

#define rep(i, x, y) for (ll i = (x); i < (y); i++)
#define rrep(i, x, y) for (ll i = (ll)(y)-1; i >= (x); i--)
#define all(x) (x).begin(), (x).end()
#ifdef LOCAL
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl
#define itrdebug(x)                                                            \\
  cerr << #x << " ";                                                           \\
  for (auto &el : (x)) {                                                       \\
    cerr << (el) << " ";                                                       \\
  }                                                                            \\
  cerr << endl
#define dassert(...) assert(__VA_ARGS__)
#else
#define debug(x)
#define itrdebug(x)
#define dassert(...)
#endif
// #define int long long

typedef long long ll;
const ll MOD = 1e9 + 7;
const long double EPS = 1e-8;

template <class NUMBER> struct Point {
  NUMBER x, y;
  Point(NUMBER x, NUMBER y) : x(x), y(y) {}
  Point() = default;

  Point operator+(const Point &point) const {
    return Point(x + point.x, y + point.y);
  }
  Point operator-(const Point &point) const {
    return Point(x - point.x, y, -point.y);
  }
  Point operator*(const NUMBER d) const { return Point(x * d, y * d); }
  Point operator/(const NUMBER d) const { return Point(x / d, y / d); }
  Point &operator+=(const Point &point) {
    x += point.x;
    y += point.y;
    return *this;
  }
  Point &operator-=(const Point &point) {
    x -= point.x;
    y -= point.y;
    return *this;
  }
  Point &operator*=(const NUMBER d) {
    x *= d;
    y *= d;
    return *this;
  }
  Point &operator/=(const NUMBER d) {
    x /= d;
    y /= d;
    return *this;
  }
  bool operator<(const Point &point) const { return x < point.x; }
};

signed main() {
  int H, W, K;
  cin >> H >> W >> K;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  vector<vector<char>> grids(H + 1, vector<char>(W + 1));
  rep(i, 1, H + 1) rep(j, 1, W + 1) cin >> grids[i][j];
  vector<vector<int>> distances(H + 1, vector<int>(W + 1, INT_MAX));
  priority_queue<pair<int, Point<int>>, vector<pair<int, Point<int>>>,
                 greater<>>
      que;
  if (grids[x1][y1] != '@') {
    distances[x1][y1] = 0;
    que.push({0, {x1, y1}});
  }
  const auto isValid = [H, W](const Point<int> &point) -> bool {
    return point.x >= 1 && point.y >= 1 && point.x <= H && point.y <= W;
  };
  while (!que.empty()) {
    auto distance = que.top().first;
    auto base = que.top().second;
    que.pop();
    vector<Point<int>> directions = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
    for (const auto &direction : directions) {
      auto current = base;
      rep(i, 0, K) {
        current += direction;
        if (!isValid(current) || grids[current.x][current.y] == '@')
          break;
        if (distances[current.x][current.y] <= distance)
          break;
        if (distances[current.x][current.y] > distance + 1) {
          distances[current.x][current.y] = distance + 1;
          que.push({distance + 1, current});
        }
      }
    }
  }
  int answer = distances[x2][y2];
  cout << (answer == INT_MAX ? -1 : answer) << endl;
  return 0;
}


Error: TLE

Fixed submission:

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

#define rep(i, x, y) for (ll i = (x); i < (y); i++)
#define rrep(i, x, y) for (ll i = (ll)(y)-1; i >= (x); i--)
#define all(x) (x).begin(), (x).end()
#ifdef LOCAL
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl
#define itrdebug(x)                                                            \\
  cerr << #x << " ";                                                           \\
  for (auto &el : (x)) {                                                       \\
    cerr << (el) << " ";                                                       \\
  }                                                                            \\
  cerr << endl
#define dassert(...) assert(__VA_ARGS__)
#else
#define debug(x)
#define itrdebug(x)
#define dassert(...)
#endif
// #define int long long

typedef long long ll;
const ll MOD = 1e9 + 7;
const long double EPS = 1e-8;

template <class NUMBER> struct Point {
  NUMBER x, y;
  Point(NUMBER x, NUMBER y) : x(x), y(y) {}
  Point() = default;

  Point operator+(const Point &point) const {
    return Point(x + point.x, y + point.y);
  }
  Point operator-(const Point &point) const {
    return Point(x - point.x, y, -point.y);
  }
  Point operator*(const NUMBER d) const { return Point(x * d, y * d); }
  Point operator/(const NUMBER d) const { return Point(x / d, y / d); }
  Point &operator+=(const Point &point) {
    x += point.x;
    y += point.y;
    return *this;
  }
  Point &operator-=(const Point &point) {
    x -= point.x;
    y -= point.y;
    return *this;
  }
  Point &operator*=(const NUMBER d) {
    x *= d;
    y *= d;
    return *this;
  }
  Point &operator/=(const NUMBER d) {
    x /= d;
    y /= d;
    return *this;
  }
  bool operator<(const Point &point) const { return x < point.x; }
};

signed main() {
  int H, W, K;
  cin >> H >> W >> K;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  vector<vector<char>> grids(H + 1, vector<char>(W + 1));
  rep(i, 1, H + 1) rep(j, 1, W + 1) cin >> grids[i][j];
  vector<vector<int>> distances(H + 1, vector<int>(W + 1, INT_MAX));
  priority_queue<pair<int, Point<int>>, vector<pair<int, Point<int>>>,
                 greater<>>
      que;
  if (grids[x1][y1] != '@') {
    distances[x1][y1] = 0;
    que.push({0, {x1, y1}});
  }
  const auto isValid = [H, W](const Point<int> &point) -> bool {
    return point.x >= 1 && point.y >= 1 && point.x <= H && point.y <= W;
  };
  while (!que.empty()) {
    auto distance = que.top().first;
    auto base = que.top().second;
    que.pop();
    vector<Point<int>> directions = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
    for (const auto &direction : directions) {
      auto current = base;
      rep(i, 0, K) {
        current += direction;
        if (!isValid(current) || grids[current.x][current.y] == '@')
          break;
        if (distances[current.x][current.y] > distance + 1) {
          distances[current.x][current.y] = distance + 1;
          que.push({distance + 1, current});
        }
      }
    }
  }
  int answer = distances[x2][y2];
  cout << (answer == INT_MAX ? -1 : answer) << endl;
  return 0;
}