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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
long double pi = 2 * acos(0.0);
const ll mod = 998244353;

ll h, w, k, xi, yi, x2, y2;
vector<vector<ll>> cost;
vector<string> mat;
vector<set<ll>> urc, ucc;

void dijkstra() {
  set<pair<ll, pair<ll, ll>>> s;
  s.emplace(make_pair(0, make_pair(xi - 1, yi - 1)));
  cost[xi - 1][yi - 1] = 0;
  while (!s.empty()) {
    pair<ll, pair<ll, ll>> node = *s.begin();
    s.erase(s.begin());
    ll r = node.second.first, c = node.second.second, d = node.first;
    for (ll i = 1; i <= k; i++) {
      if (c + i < w && mat[r][c + i] != '@') {
        if (cost[r][c + i] == INT_MAX) {
          s.emplace(make_pair(d + 1, make_pair(r, c + i)));
          cost[r][c + i] = d + 1;
        } else if (d + 1 > cost[r][c + i])
          break;
      } else
        break;
    }
    for (ll i = 1; i <= k; i++) {
      if (c - i >= 0 && mat[r][c - i] != '@') {
        if (cost[r][c - i] == INT_MAX) {
          s.emplace(make_pair(d + 1, make_pair(r, c - i)));
          cost[r][c - i] = d + 1;
        } else if (d + 1 > cost[r][c - i])
          break;
      } else
        break;
    }
    for (ll i = 1; i <= k; i++) {
      if (r + i < h && mat[r + i][c] != '@') {
        if (cost[r + i][c] == INT_MAX) {
          s.emplace(make_pair(d + 1, make_pair(r + i, c)));
          cost[r + i][c] = d + 1;
        } else if (d + 1 > cost[r + i][c])
          break;
      } else
        break;
    }
    for (ll i = 1; i <= k; i++) {
      if (r - i >= 0 && mat[r - i][c] != '@') {
        if (cost[r - i][c] == INT_MAX) {
          s.emplace(make_pair(d + 1, make_pair(r - i, c)));
          cost[r - i][c] = d + 1;
        } else if (d + 1 > cost[r - i][c])
          break;
      } else
        break;
    }
  }
}

void solve() {
  cin >> h >> w >> k;
  cin >> xi >> yi >> x2 >> y2;
  mat.resize(h);
  cost.resize(h);
  urc.resize(h);
  ucc.resize(w);

  for (ll i = 0; i < h; i++)
    cost[i].resize(w);
  for (ll i = 0; i < h; i++) {
    cin >> mat[i];
    for (ll j = 0; j < w; j++) {
      cost[i][j] = INT_MAX;
    }
  }
  dijkstra();
  if (cost[x2 - 1][y2 - 1] != INT_MAX)
    cout << cost[x2 - 1][y2 - 1];
  else
    cout << "-1";
}

int main() {
  ios_base ::sync_with_stdio(false);
  cin.tie(nullptr);
  ll t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
long double pi = 2 * acos(0.0);
const ll mod = 998244353;

ll h, w, k, xi, yi, x2, y2;
vector<vector<ll>> cost;
vector<string> mat;

void dijkstra() {
  set<pair<ll, pair<ll, ll>>> s;
  s.emplace(make_pair(0, make_pair(xi - 1, yi - 1)));
  cost[xi - 1][yi - 1] = 0;
  while (!s.empty()) {
    if (cost[x2 - 1][y2 - 1] != INT_MAX)
      break;
    pair<ll, pair<ll, ll>> node = *s.begin();
    s.erase(s.begin());
    ll r = node.second.first, c = node.second.second, d = node.first;
    // cout << r << " " << c << "\\n";
    for (ll i = 1; i <= k; i++) {
      if (c + i < w && mat[r][c + i] != '@') {
        if (cost[r][c + i] == INT_MAX) {
          s.emplace(make_pair(d + 1, make_pair(r, c + i)));
          cost[r][c + i] = d + 1;
        }
      } else
        break;
    }
    for (ll i = 1; i <= k; i++) {
      if (c - i >= 0 && mat[r][c - i] != '@') {
        if (cost[r][c - i] == INT_MAX) {
          s.emplace(make_pair(d + 1, make_pair(r, c - i)));
          cost[r][c - i] = d + 1;
        }
      } else
        break;
    }
    for (ll i = 1; i <= k; i++) {
      if (r + i < h && mat[r + i][c] != '@') {
        if (cost[r + i][c] == INT_MAX) {
          s.emplace(make_pair(d + 1, make_pair(r + i, c)));
          cost[r + i][c] = d + 1;
        }
      } else
        break;
    }
    for (ll i = 1; i <= k; i++) {
      if (r - i >= 0 && mat[r - i][c] != '@') {
        if (cost[r - i][c] == INT_MAX) {
          s.emplace(make_pair(d + 1, make_pair(r - i, c)));
          cost[r - i][c] = d + 1;
        }
      } else
        break;
    }
  }
}

void solve() {
  cin >> h >> w >> k;
  cin >> xi >> yi >> x2 >> y2;
  mat.resize(h);
  cost.resize(h);
  for (ll i = 0; i < h; i++)
    cost[i].resize(w);
  for (ll i = 0; i < h; i++) {
    cin >> mat[i];
    for (ll j = 0; j < w; j++) {
      cost[i][j] = INT_MAX;
    }
  }
  dijkstra();
  if (cost[x2 - 1][y2 - 1] != INT_MAX)
    cout << cost[x2 - 1][y2 - 1];
  else
    cout << "-1";
}

int main() {
  ios_base ::sync_with_stdio(false);
  cin.tie(nullptr);
  ll t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}