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 < (n); i++)
#define sz(x) int(x.size())
#define pb push_back
#define eb emplace_back
using ll = long long;
using P = pair<int, int>;
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
const ll MOD = 1000000007, MOD2 = 998244353;

int main() {
  int H, W, K;
  cin >> H >> W >> K;
  int xs, ys, xg, yg;
  cin >> xs >> ys >> xg >> yg;
  --xs;
  --ys;
  --xg;
  --yg;
  vector<vector<int>> rake(H, vector<int>(W));
  rep(i, H) rep(j, W) {
    char c;
    cin >> c;
    if (c == '@') {
      rake.at(i).at(j) = 1;
    }
  }
  vector<vector<int>> ans(H, vector<int>(W, -1));
  ans.at(xs).at(ys) = 0;
  queue<P> q;
  q.push({xs, ys});
  while (!q.empty()) {
    P p = q.front();
    q.pop();
    int x = p.first, y = p.second;
    int now = ans.at(x).at(y);
    if (x == xg && y == yg) {
      cout << now << endl;
      return 0;
    }
    for (int i = 1; i <= K; i++) {
      if (x + i >= H)
        break;
      if (rake.at(x + i).at(y))
        break;
      if (ans.at(x + i).at(y) >= 0 && ans.at(x + i).at(y) <= now)
        break;
      if (ans.at(x + i).at(y) >= 0 && ans.at(x + i).at(y) == now + 1)
        continue;
      ans.at(x + i).at(y) = now + 1;
      q.push({x + i, y});
    }
    for (int i = 1; i <= K; i++) {
      if (x - i < 0)
        break;
      if (rake.at(x - i).at(y))
        break;
      if (ans.at(x - i).at(y) >= 0 && ans.at(x - i).at(y) <= now)
        break;
      if (ans.at(x - i).at(y) >= 0 && ans.at(x - i).at(y) == now + 1)
        continue;
      ans.at(x - i).at(y) = now + 1;
      q.push({x - i, y});
    }
    for (int i = 1; i <= K; i++) {
      if (y + i >= W)
        break;
      if (rake.at(x).at(y + i))
        break;
      if (ans.at(x).at(y + i) >= 0 && ans.at(x).at(y + i) <= now)
        break;
      if (ans.at(x).at(y + i) >= 0 && ans.at(x).at(y + i) == now + 1)
        continue;
      ans.at(x).at(y + i) = now + 1;
      q.push({x, y + i});
    }
    for (int i = 1; i <= K; i++) {
      if (y - i < 0)
        break;
      if (rake.at(x).at(y - i))
        break;
      if (ans.at(x).at(y - i) >= 0 && ans.at(x).at(y - i) <= now)
        break;
      if (ans.at(x).at(y - i) >= 0 && ans.at(x).at(y - i) == now + 1)
        continue;
      ans.at(x).at(y - i) = now + 1;
      q.push({x, y - i});
    }
  }

  cout << -1 << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define sz(x) int(x.size())
#define pb push_back
#define eb emplace_back
using ll = long long;
using P = pair<int, int>;
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
const ll MOD = 1000000007, MOD2 = 998244353;

int main() {
  int H, W, K;
  cin >> H >> W >> K;
  int xs, ys, xg, yg;
  cin >> xs >> ys >> xg >> yg;
  --xs;
  --ys;
  --xg;
  --yg;
  vector<vector<int>> rake(H, vector<int>(W));
  rep(i, H) rep(j, W) {
    char c;
    cin >> c;
    if (c == '@') {
      rake.at(i).at(j) = 1;
    }
  }
  vector<vector<int>> ans(H, vector<int>(W, -1));
  ans.at(xs).at(ys) = 0;
  queue<P> q;
  q.push({xs, ys});
  while (!q.empty()) {
    P p = q.front();
    q.pop();
    int x = p.first, y = p.second;
    int now = ans.at(x).at(y);
    if (x == xg && y == yg) {
      cout << now << endl;
      return 0;
    }
    for (int i = 1; i <= K; i++) {
      if (x + i >= H)
        break;
      if (rake.at(x + i).at(y))
        break;
      if (ans.at(x + i).at(y) >= 0 && ans.at(x + i).at(y) <= now)
        break;
      ans.at(x + i).at(y) = now + 1;
      q.push({x + i, y});
    }
    for (int i = 1; i <= K; i++) {
      if (x - i < 0)
        break;
      if (rake.at(x - i).at(y))
        break;
      if (ans.at(x - i).at(y) >= 0 && ans.at(x - i).at(y) <= now)
        break;
      ans.at(x - i).at(y) = now + 1;
      q.push({x - i, y});
    }
    for (int i = 1; i <= K; i++) {
      if (y + i >= W)
        break;
      if (rake.at(x).at(y + i))
        break;
      if (ans.at(x).at(y + i) >= 0 && ans.at(x).at(y + i) <= now)
        break;
      ans.at(x).at(y + i) = now + 1;
      q.push({x, y + i});
    }
    for (int i = 1; i <= K; i++) {
      if (y - i < 0)
        break;
      if (rake.at(x).at(y - i))
        break;
      if (ans.at(x).at(y - i) >= 0 && ans.at(x).at(y - i) <= now)
        break;
      ans.at(x).at(y - i) = now + 1;
      q.push({x, y - i});
    }
  }

  cout << -1 << endl;
}