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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;

#define F first
#define S second
#define pb push_back
#define eb emplace_back
#define PI 3.14159265359
#define sz(c) (int)c.size()
#define len(c) (int)c.length()
#define hcf(a, b) __gcd(a, b)
#define all(c) c.begin(), c.end()
#define vint vector<int>
#define vll vector<ll>
#define vpint vector<pair<int, int>>
#define vpll vector<pair<ll, ll>>
#define fr(it, a, b) for (int it = (a); it < (b); it++)
#define tr(con, it) for (auto it = con.begin(); it != con.end(); it++)
#define present(con, ele) (con.find(ele) != con.end())
#define cpresent(con, ele) (find(all(con), ele) != con.end())
#define maximum(a, b, c) (a > b ? (a > c ? a : c) : (b > c ? b : c))
#define minimum(a, b, c) (a < b ? (a < c ? a : c) : (b < c ? b : c))
typedef long long ll;
typedef long double lf;
typedef pair<int, int> pi;
typedef unsigned long long lu;
const ll mod = 1e9 + 7;
int32_t main() {
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int h, w, k, x1, x2, y1, y2;
  cin >> h >> w >> k >> x1 >> y1 >> x2 >> y2;
  vector<pair<int, int>> p = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
  vector<string> a(h);
  fr(i, 0, h) { cin >> a[i]; }
  vector<vll> vis(h, vll(w, 0)), dis(h, vll(w, 1e18));
  queue<pair<int, int>> q;
  q.push({x1 - 1, y1 - 1});
  dis[x1 - 1][y1 - 1] = 0;
  while (!q.empty()) {
    pi nd = q.front();
    q.pop();
    if (vis[nd.F][nd.S]) {
      continue;
    }
    vis[nd.F][nd.S] = 1;
    int i = nd.F, j = nd.S;
    for (auto u : p) {
      for (int l = 1; l <= k; l++) {
        if (i + u.F * l < h && i + u.F * l >= 0 && j + u.S * l >= 0 &&
            j + u.S * l < w) {
          if (a[i + u.F * l][j + u.S * l] == '@') {
            break;
          }
          if (dis[i + u.F * l][j + u.S * l] >= dis[i][j] + 1) {
            dis[i + u.F * l][j + u.S * l] = dis[i][j] + 1;
            q.push({i + u.F * l, j + u.S * l});
          } else {
            break;
          }
        } else {
          break;
        }
      }
    }
  }
  if (!vis[x2 - 1][y2 - 1]) {
    cout << -1 << "\\n";
    return 0;
  }
  cout << dis[x2 - 1][y2 - 1] << "\\n";
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;

#define F first
#define S second
#define pb push_back
#define eb emplace_back
#define PI 3.14159265359
#define sz(c) (int)c.size()
#define len(c) (int)c.length()
#define hcf(a, b) __gcd(a, b)
#define all(c) c.begin(), c.end()
#define vint vector<int>
#define vll vector<ll>
#define vpint vector<pair<int, int>>
#define vpll vector<pair<ll, ll>>
#define fr(it, a, b) for (int it = (a); it < (b); it++)
#define tr(con, it) for (auto it = con.begin(); it != con.end(); it++)
#define present(con, ele) (con.find(ele) != con.end())
#define cpresent(con, ele) (find(all(con), ele) != con.end())
#define maximum(a, b, c) (a > b ? (a > c ? a : c) : (b > c ? b : c))
#define minimum(a, b, c) (a < b ? (a < c ? a : c) : (b < c ? b : c))
typedef long long ll;
typedef long double lf;
typedef pair<int, int> pi;
typedef unsigned long long lu;
const ll mod = 1e9 + 7;
int32_t main() {
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int h, w, k, x1, x2, y1, y2;
  cin >> h >> w >> k >> x1 >> y1 >> x2 >> y2;
  vector<pair<int, int>> p = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
  vector<string> a(h);
  fr(i, 0, h) { cin >> a[i]; }
  vector<vll> vis(h, vll(w, 0)), dis(h, vll(w, 1e18));
  queue<pair<int, int>> q;
  q.push({x1 - 1, y1 - 1});
  dis[x1 - 1][y1 - 1] = 0;
  while (!q.empty()) {
    pi nd = q.front();
    q.pop();
    if (vis[nd.F][nd.S]) {
      continue;
    }
    vis[nd.F][nd.S] = 1;
    int i = nd.F, j = nd.S;
    for (auto u : p) {
      for (int l = 1; l <= k; l++) {
        if (i + u.F * l < h && i + u.F * l >= 0 && j + u.S * l >= 0 &&
            j + u.S * l < w) {
          if (a[i + u.F * l][j + u.S * l] == '@') {
            break;
          }
          if (dis[i + u.F * l][j + u.S * l] > dis[i][j] + 1) {
            dis[i + u.F * l][j + u.S * l] = dis[i][j] + 1;
            q.push({i + u.F * l, j + u.S * l});
          }
        } else {
          break;
        }
      }
    }
  }
  if (!vis[x2 - 1][y2 - 1]) {
    cout << -1 << "\\n";
    return 0;
  }
  cout << dis[x2 - 1][y2 - 1] << "\\n";
  return 0;
}