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

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

int main() {
  int h, w, k;
  cin >> h >> w >> k;
  int x, y, x2, y2;
  cin >> x >> y >> x2 >> y2;
  vector<string> v(h);
  vector<vector<int>> f(h, vector<int>(w, 0));
  for (int i = 0; i < h; ++i) {
    cin >> v[i];
  }
  int sx = x - 1, sy = y - 1;
  int gx = x2 - 1, gy = y2 - 1;
  queue<pair<pair<int, int>, int>> q;
  q.push({{sx, sy}, 0});
  f[sx][sy] = true;
  int ans = -1;
  while (!q.empty()) {
    auto now = q.front().first;
    int num = q.front().second;
    // cout<<now.first<<now.second<<endl;
    if (now.first == gx && now.second == gy) {
      ans = num;
      break;
    }
    q.pop();
    for (int i = 1; i <= k; ++i) {
      if (now.first + i < h && now.first + i >= 0) {
        auto next = make_pair(now.first + i, now.second);
        if (v[next.first][next.second] != '@') {
          if (!f[next.first][next.second]) {
            q.push({next, num + 1});
            f[next.first][next.second] = num + 1;
          } else if (f[next.first][next.second] <= num)
            break;
        } else
          break;
      } else
        break;
    }
    for (int i = 1; i <= k; ++i) {
      if (now.first - i < h && now.first - i >= 0) {
        auto next = make_pair(now.first - i, now.second);
        if (v[next.first][next.second] != '@') {
          if (!f[next.first][next.second]) {
            q.push({next, num + 1});
            f[next.first][next.second] = num + 1;
          } else if (f[next.first][next.second] <= num)
            break;
        } else
          break;
      } else
        break;
    }
    for (int i = 1; i <= k; ++i) {
      if (now.second + i < w && now.second + i >= 0) {
        auto next = make_pair(now.first, now.second + i);
        if (v[next.first][next.second] != '@') {
          if (!f[next.first][next.second]) {
            q.push({next, num + 1});
            f[next.first][next.second] = num + 1;
          } else if (f[next.first][next.second] <= num)
            break;
        } else
          break;
      } else
        break;
    }
    for (int i = 1; i <= k; ++i) {
      if (now.second - i < w && now.second - i >= 0) {
        auto next = make_pair(now.first, now.second - i);
        if (v[next.first][next.second] != '@') {
          if (!f[next.first][next.second]) {
            q.push({next, num + 1});
            f[next.first][next.second] = num + 1;
          } else if (f[next.first][next.second] <= num)
            break;
        } else
          break;
      } else
        break;
    }
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

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

int main() {
  int h, w, k;
  cin >> h >> w >> k;
  int x, y, x2, y2;
  cin >> x >> y >> x2 >> y2;
  vector<string> v(h);
  vector<vector<bool>> f(h, vector<bool>(w, false));
  for (int i = 0; i < h; ++i) {
    cin >> v[i];
  }
  int sx = x - 1, sy = y - 1;
  int gx = x2 - 1, gy = y2 - 1;
  queue<pair<pair<int, int>, int>> q;
  q.push({{sx, sy}, 0});
  f[sx][sy] = true;
  int ans = -1;
  while (!q.empty()) {
    auto now = q.front().first;
    int num = q.front().second;
    // cout<<now.first<<now.second<<endl;
    if (now.first == gx && now.second == gy) {
      ans = num;
      break;
    }
    q.pop();
    for (int i = 1; i <= k; ++i) {
      if (now.first + i < h && now.first + i >= 0) {
        auto next = make_pair(now.first + i, now.second);
        if (v[next.first][next.second] != '@') {
          if (!f[next.first][next.second]) {
            q.push({next, num + 1});
            f[next.first][next.second] = true;
          }
        } else
          break;
      } else
        break;
    }
    for (int i = 1; i <= k; ++i) {
      if (now.first - i < h && now.first - i >= 0) {
        auto next = make_pair(now.first - i, now.second);
        if (v[next.first][next.second] != '@') {
          if (!f[next.first][next.second]) {
            q.push({next, num + 1});
            f[next.first][next.second] = true;
          }
        } else
          break;
      } else
        break;
    }
    for (int i = 1; i <= k; ++i) {
      if (now.second + i < w && now.second + i >= 0) {
        auto next = make_pair(now.first, now.second + i);
        if (v[next.first][next.second] != '@') {
          if (!f[next.first][next.second]) {
            q.push({next, num + 1});
            f[next.first][next.second] = true;
          }
        } else
          break;
      } else
        break;
    }
    for (int i = 1; i <= k; ++i) {
      if (now.second - i < w && now.second - i >= 0) {
        auto next = make_pair(now.first, now.second - i);
        if (v[next.first][next.second] != '@') {
          if (!f[next.first][next.second]) {
            q.push({next, num + 1});
            f[next.first][next.second] = true;
          }
        } else
          break;
      } else
        break;
    }
  }
  cout << ans << endl;
}