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

// Created by: WeirdBugsButOkay

#include <bits/stdc++.h>

#define ll long long
#define ld long double
#define ff first
#define ss second
#define PI 3.14159265358979323846

using namespace std;

int h, w, k;

void solve() {
  int x1, x2, y1, y2;
  cin >> h >> w >> k;
  string s[h];
  cin >> x1 >> y1;
  cin >> x2 >> y2;
  for (int i = 0; i < h; i++)
    cin >> s[i];
  vector<vector<int>> dis(h, vector<int>(w, 1e9));
  vector<vector<bool>> vis(h, vector<bool>(w, 0));
  x1--, x2--, y1--, y2--;
  dis[x1][y1] = 0;
  int x[4] = {1, 0, -1, 0}, y[4] = {0, 1, 0, -1};
  queue<pair<int, int>> bfs;
  bfs.push({x1, y1});
  // cout << "Hi" << endl;
  auto ok = [&](int i, int j) {
    if (i >= h || i < 0)
      return false;
    if (j >= w || j < 0)
      return false;
    if (s[i][j] == '@')
      return false;
    return true;
  };
  vis[x1][y1] = 1;
  while (!bfs.empty()) {
    auto p = bfs.front();
    bfs.pop();
    // cout << "at " << p.ff << "," << p.ss << endl;
    for (int j = 0; j < 4; j++) {
      for (int i = 1; i <= k; i++) {
        int xx = p.ff + i * x[j], yy = p.ss + i * y[j];
        if (!ok(xx, yy))
          break;
        if (dis[xx][yy] <= dis[p.ff][p.ss])
          break;
        dis[xx][yy] = dis[p.ff][p.ss] + 1;
        if (vis[xx][yy] == 0) {
          bfs.push({xx, yy});
          vis[xx][yy] = 1;
        }
      }
    }
    // cout << endl;
  }
  /*for(int i = 0; i < h; i++)
  {
          for(int j = 0; j < w; j++)
                  cout << setw(12) << dis[i][j];
          cout << endl;
  }*/
  if (dis[x2][y2] == 1e9)
    cout << -1 << endl;
  else
    cout << dis[x2][y2] << endl;
}

int main() {
  int q = 1;
  // scanf("%d", &q);
  for (int i = 1; i <= q; i++) {
    solve();
  }
  return 0;
}

Error: TLE

Fixed submission:

// Created by: WeirdBugsButOkay

#include <bits/stdc++.h>

#define ll long long
#define ld long double
#define ff first
#define ss second
#define PI 3.14159265358979323846

using namespace std;

int h, w, k;

void solve() {
  int x1, x2, y1, y2;
  cin >> h >> w >> k;
  string s[h];
  cin >> x1 >> y1;
  cin >> x2 >> y2;
  for (int i = 0; i < h; i++)
    cin >> s[i];
  vector<vector<int>> dis(h, vector<int>(w, 1e9));
  x1--, x2--, y1--, y2--;
  dis[x1][y1] = 0;
  int x[4] = {1, 0, -1, 0}, y[4] = {0, 1, 0, -1};
  queue<pair<int, int>> bfs;
  bfs.push({x1, y1});
  // cout << "Hi" << endl;
  auto ok = [&](int i, int j) {
    if (i >= h || i < 0)
      return false;
    if (j >= w || j < 0)
      return false;
    if (s[i][j] == '@')
      return false;
    return true;
  };
  while (!bfs.empty()) {
    auto p = bfs.front();
    bfs.pop();
    // cout << "at " << p.ff << "," << p.ss << endl;
    for (int j = 0; j < 4; j++) {
      for (int i = 1; i <= k; i++) {
        if (!ok(p.ff + i * x[j], p.ss + i * y[j]))
          break;
        if (dis[p.ff + i * x[j]][p.ss + i * y[j]] <= dis[p.ff][p.ss])
          break;
        dis[p.ff + i * x[j]][p.ss + i * y[j]] = dis[p.ff][p.ss] + 1;
        bfs.push({p.ff + i * x[j], p.ss + i * y[j]});
      }
    }
    // cout << endl;
  }
  /*for(int i = 0; i < h; i++)
  {
          for(int j = 0; j < w; j++)
                  cout << setw(12) << dis[i][j];
          cout << endl;
  }*/
  if (dis[x2][y2] == 1e9)
    cout << -1 << endl;
  else
    cout << dis[x2][y2] << endl;
}

int main() {
  int q = 1;
  // scanf("%d", &q);
  for (int i = 1; i <= q; i++) {
    solve();
  }
  return 0;
}