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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pull = pair<ull, ll>;
#define INF 1000000000
#define LLINF 10000000000000000ll
#define MOD 1000000007LL

bool dbgflag = false;

void bfs(vector<string> &f, vector<vector<ll>> &cnt, ll k, ll xs, ll ys, ll xg,
         ll yg) {
  ll h = f.size();
  ll w = f[0].size();

  queue<pll> que;
  que.push(pll(xs, ys));
  cnt[ys][xs] = 0;
  vector<ll> dx = {-1, 0, 0, 1};
  vector<ll> dy = {0, 1, -1, 0};

  while (!que.empty()) {
    pii p = que.front();
    ll x = p.first, y = p.second;
    // cout << x << " " << y << endl;
    que.pop();
    if ((x == xg) && (y == yg))
      break;
    for (int i = 0; i < 4; i++) {
      for (int j = 1; j <= k; j++) {
        ll nx = x + dx[i] * j, ny = y + dy[i] * j;
        if ((nx >= 0) && (nx < w) && (ny >= 0) && (ny < h) &&
            (f[ny][nx] == '@'))
          break;
        if ((nx >= 0) && (nx < w) && (ny >= 0) && (ny < h) &&
            (f[ny][nx] == '.') && (cnt[ny][nx] == -1)) {
          cnt[ny][nx] = cnt[y][x] + 1;
          que.push(pll(nx, ny));
        } else if ((nx >= 0) && (nx < w) && (ny >= 0) && (ny < h) &&
                   (f[ny][nx] == '.') && (cnt[ny][nx] != -1)) {
          if (cnt[ny][nx] > cnt[y][x] + 1)
            cnt[ny][nx] = cnt[y][x] + 1;
          else if (cnt[ny][nx] < cnt[y][x] + 1)
            break;
        }
      }
    }
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll h, w, k;
  cin >> h >> w >> k;
  ll xs, ys, xg, yg;
  cin >> xs >> ys >> xg >> yg;
  vector<string> f(h);
  for (int i = 0; i < h; i++)
    cin >> f[i];

  vector<vector<ll>> cnt(h, vector<ll>(w, -1));

  bfs(f, cnt, k, ys - 1, xs - 1, yg - 1, xg - 1);
  // cout << "Y" << endl;

  /*for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) cout << cnt[i][j] << " ";
    cout << endl;
  }*/
  cout << cnt[xg - 1][yg - 1] << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pull = pair<ull, ll>;
#define INF 1000000000
#define LLINF 10000000000000000ll
#define MOD 1000000007LL

bool dbgflag = false;

void bfs(vector<string> &f, vector<vector<ll>> &cnt, ll k, ll xs, ll ys, ll xg,
         ll yg) {
  ll h = f.size();
  ll w = f[0].size();

  queue<pll> que;
  que.push(pll(xs, ys));
  cnt[ys][xs] = 0;
  vector<ll> dx = {-1, 0, 0, 1};
  vector<ll> dy = {0, 1, -1, 0};

  while (!que.empty()) {
    pii p = que.front();
    ll x = p.first, y = p.second;
    que.pop();
    if ((x == xg) && (y == yg))
      break;
    for (int i = 0; i < 4; i++) {
      for (int j = 1; j <= k; j++) {
        ll nx = x + dx[i] * j, ny = y + dy[i] * j;
        if ((nx >= 0) && (nx < w) && (ny >= 0) && (ny < h) &&
            (f[ny][nx] == '@'))
          break;
        if ((nx >= 0) && (nx < w) && (ny >= 0) && (ny < h) &&
            (f[ny][nx] == '.') && (cnt[ny][nx] == -1)) {
          cnt[ny][nx] = cnt[y][x] + 1;
          que.push(pll(nx, ny));
        } else if ((nx >= 0) && (nx < w) && (ny >= 0) && (ny < h) &&
                   (f[ny][nx] == '.') && (cnt[ny][nx] != -1)) {
          if (cnt[ny][nx] > cnt[y][x] + 1)
            cnt[ny][nx] = cnt[y][x] + 1;
        }
      }
    }
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll h, w, k;
  cin >> h >> w >> k;
  ll xs, ys, xg, yg;
  cin >> xs >> ys >> xg >> yg;
  vector<string> f(h);
  for (int i = 0; i < h; i++)
    cin >> f[i];

  vector<vector<ll>> cnt(h, vector<ll>(w, -1));

  bfs(f, cnt, k, ys - 1, xs - 1, yg - 1, xg - 1);
  /*cout << "Y" << endl;

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) cout << cnt[i][j] << " ";
    cout << endl;
  }*/
  cout << cnt[xg - 1][yg - 1] << endl;
}