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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < int(n); ++i)
#define all(a) (a).begin(), (a).end()
typedef long long ll;
#define rep1(i, n) for (int i = 1; i <= int(n); ++i)
#define lb(s, x) (lower_bound(all(s), x) - s.begin())
#define ub(s, x) (upper_bound(all(s), x) - s.begin())
#define uniq(a) a.erase(unique(all(a)), a.end())
#define bit(k) (1LL << (k))
const int INF = 1e9;

#define debug(x) cerr << (#x) << ": " << (x) << endl
template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &p) {
  o << "(" << p.first << ", " << p.second << ")";
  return o;
}
#define print(v)                                                               \\
  {                                                                            \\
    cerr << #v << ": [ ";                                                      \\
    for (auto _ : v)                                                           \\
      cerr << _ << ", ";                                                       \\
    cerr << "]" << endl;                                                       \\
  }

struct Point {
  int y, x;
};

int dy[4] = {-1, 0, +1, 0};
int dx[4] = {0, 1, 0, -1};

int main(void) {
  int Y, X, K;
  cin >> Y >> X >> K;
  int sx, sy, gx, gy;
  cin >> sy >> sx >> gy >> gx;
  sx--, sy--, gx--, gy--;
  vector<string> s(Y);
  rep(y, Y) cin >> s[y];

  vector<vector<int>> dp(Y, vector<int>(X, INF));
  dp[sy][sx] = 0;
  queue<Point> qu;
  qu.push({sy, sx});
  while (qu.size()) {
    Point p = qu.front();
    qu.pop();
    rep(dir, 4) {
      rep1(k, K) {
        int ny = p.y + dy[dir] * k;
        int nx = p.x + dx[dir] * k;
        if (ny < 0 || nx < 0 || ny >= Y || nx >= X)
          break;
        if (s[ny][nx] == '@')
          break;
        if (dp[ny][nx] <= dp[p.y][p.x])
          break;
        if (dp[ny][nx] > dp[p.y][p.x] + 1) {
          dp[ny][nx] = dp[p.y][p.x] + 1;
          qu.push({ny, nx});
        }
      }
    }
  }
  cout << ((dp[gy][gx] == INF) ? -1 : dp[gy][gx]) << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < int(n); ++i)
#define all(a) (a).begin(), (a).end()
typedef long long ll;
#define rep1(i, n) for (int i = 1; i <= int(n); ++i)
#define lb(s, x) (lower_bound(all(s), x) - s.begin())
#define ub(s, x) (upper_bound(all(s), x) - s.begin())
#define uniq(a) a.erase(unique(all(a)), a.end())
#define bit(k) (1LL << (k))
const int INF = 1e9;

#define debug(x) cerr << (#x) << ": " << (x) << endl
template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &p) {
  o << "(" << p.first << ", " << p.second << ")";
  return o;
}
#define print(v)                                                               \\
  {                                                                            \\
    cerr << #v << ": [ ";                                                      \\
    for (auto _ : v)                                                           \\
      cerr << _ << ", ";                                                       \\
    cerr << "]" << endl;                                                       \\
  }

struct Point {
  int y, x;
};

int dy[4] = {-1, 0, +1, 0};
int dx[4] = {0, 1, 0, -1};

int main(void) {
  int Y, X, K;
  cin >> Y >> X >> K;
  int sx, sy, gx, gy;
  cin >> sy >> sx >> gy >> gx;
  sx--, sy--, gx--, gy--;
  vector<string> s(Y);
  rep(y, Y) cin >> s[y];

  vector<vector<int>> dp(Y, vector<int>(X, INF));
  dp[sy][sx] = 0;
  queue<Point> qu;
  qu.push({sy, sx});
  while (qu.size()) {
    Point p = qu.front();
    qu.pop();
    rep(dir, 4) {
      rep1(k, K) {
        int ny = p.y + dy[dir] * k;
        int nx = p.x + dx[dir] * k;
        if (ny < 0 || nx < 0 || ny >= Y || nx >= X)
          break;
        if (s[ny][nx] == '@')
          break;
        if (dp[ny][nx] <= dp[p.y][p.x])
          break;
        dp[ny][nx] = dp[p.y][p.x] + 1;
        qu.push({ny, nx});
      }
    }
  }
  cout << ((dp[gy][gx] == INF) ? -1 : dp[gy][gx]) << endl;
  return 0;
}