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, INF = 1e18;

int H, W, K;
int xs, xg, ys, yg;
vector<vector<int>> rake;
vector<vector<int>> checked;

void dfs(int x, int y, int bx, int by) {
  if (x >= H || x < 0 || y >= W || y < 0)
    return;
  if (rake.at(x).at(y))
    return;
  if (checked.at(x).at(y))
    return;
  checked.at(x).at(y) = checked.at(bx).at(by) + 1;
  dfs(x + 1, y, x, y);
  dfs(x - 1, y, x, y);
  dfs(x, y + 1, x, y);
  dfs(x, y - 1, x, y);
  return;
}

int main() {

  cin >> H >> W >> K;

  cin >> xs >> ys >> xg >> yg;
  --xs;
  --xg;
  --ys;
  --yg;
  rake.resize(H, vector<int>(W));
  checked.resize(H, vector<int>(W, 0));
  rep(i, H) rep(j, W) {
    char c;
    cin >> c;
    if (c == '@') {
      rake.at(i).at(j) = 1;
    }
  }
  vector<vector<int>> reached(H, vector<int>(W));
  queue<pair<P, pair<ll, P>>> q;
  q.push({{xs, ys}, {0, {xs, ys}}});

  dfs(xs, ys, xs, ys);
  if (checked.at(xg).at(yg) == 0) {
    cout << -1 << endl;
    return 0;
  }

  while (!q.empty()) {
    pair<P, pair<ll, P>> now;
    now = q.front();
    q.pop();
    if (xg == now.first.first && yg == now.first.second) {
      cout << now.second.first << endl;
      return 0;
    }
    if (reached.at(now.first.first).at(now.first.second) == 1)
      continue;
    reached.at(now.first.first).at(now.first.second) = 1;
    int x = now.first.first, y = now.first.second;
    int bx = now.second.second.first, by = now.second.second.second;
    if (abs(x - bx) == K || x == bx || x == 0 || x == H - 1) {
      for (int i = 1; i <= K; i++) {
        if (x + i >= H)
          break;
        if (rake.at(x + i).at(y))
          break;
        if (reached.at(x + i).at(y))
          continue;
        q.push({{x + i, y}, {now.second.first + 1, {x, y}}});
      }

      for (int i = 1; i <= K; i++) {
        if (x - i < 0)
          break;
        if (rake.at(x - i).at(y))
          break;
        if (reached.at(x - i).at(y))
          continue;
        q.push({{x - i, y}, {now.second.first + 1, {x, y}}});
      }
    }
    if (abs(y - by) == K || y == by || y == 0 || y == W - 1) {
      for (int i = 1; i <= K; i++) {
        if (y + i >= W)
          break;
        if (rake.at(x).at(y + i))
          break;
        if (reached.at(x).at(y + i))
          continue;
        q.push({{x, y + i}, {now.second.first + 1, {x, y}}});
      }
      for (int i = 1; i <= K; i++) {
        if (y - i < 0)
          break;
        if (rake.at(x).at(y - i))
          break;
        if (reached.at(x).at(y - i))
          continue;
        q.push({{x, y - i}, {now.second.first + 1, {x, y}}});
      }
    }
  }
}

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, INF = 1e18;

int H, W, K;
int xs, xg, ys, yg;
vector<vector<int>> rake;
vector<vector<int>> checked;

void dfs(int x, int y) {
  if (x >= H || x < 0 || y >= W || y < 0)
    return;
  if (rake.at(x).at(y))
    return;
  if (checked.at(x).at(y))
    return;
  checked.at(x).at(y) = 1;
  dfs(x + 1, y);
  dfs(x - 1, y);
  dfs(x, y + 1);
  dfs(x, y - 1);
  return;
}

int main() {

  cin >> H >> W >> K;

  cin >> xs >> ys >> xg >> yg;
  --xs;
  --xg;
  --ys;
  --yg;
  rake.resize(H, vector<int>(W));
  checked.resize(H, vector<int>(W, 0));
  rep(i, H) rep(j, W) {
    char c;
    cin >> c;
    if (c == '@') {
      rake.at(i).at(j) = 1;
    }
  }
  vector<vector<int>> reached(H, vector<int>(W));
  queue<pair<P, ll>> q;
  q.push({{xs, ys}, 0});

  dfs(xs, ys);
  if (checked.at(xg).at(yg) == 0) {
    cout << -1 << endl;
    return 0;
  }

  while (!q.empty()) {
    pair<P, ll> now;
    now = q.front();
    q.pop();
    if (xg == now.first.first && yg == now.first.second) {
      cout << now.second << endl;
      return 0;
    }
    if (reached.at(now.first.first).at(now.first.second) == 1)
      continue;
    reached.at(now.first.first).at(now.first.second) = 1;
    int x = now.first.first, y = now.first.second;
    for (int i = 1; i <= K; i++) {
      if (x + i >= H)
        break;
      if (rake.at(x + i).at(y))
        break;
      if (reached.at(x + i).at(y))
        continue;
      q.push({{x + i, y}, now.second + 1});
    }
    for (int i = 1; i <= K; i++) {
      if (x - i < 0)
        break;
      if (rake.at(x - i).at(y))
        break;
      if (reached.at(x - i).at(y))
        continue;
      q.push({{x - i, y}, now.second + 1});
    }
    for (int i = 1; i <= K; i++) {
      if (y + i >= W)
        break;
      if (rake.at(x).at(y + i))
        break;
      if (reached.at(x).at(y + i))
        continue;
      q.push({{x, y + i}, now.second + 1});
    }
    for (int i = 1; i <= K; i++) {
      if (y - i < 0)
        break;
      if (rake.at(x).at(y - i))
        break;
      if (reached.at(x).at(y - i))
        continue;
      q.push({{x, y - i}, now.second + 1});
    }
  }
}