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

// Nihal Mittal - nihal_47
// Strike First , Strike Hard , No Mercy !!

/**⠀⠀⠀⠀⠀⠀⣀⣤⣤⣄⣀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⣠⣾⣿⣿⣿⣿⣿⣿⣿⣿⣿⣦⣀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⣰⣿⣿⣿⣿⠿⠿⣿⣿⣿⣿⣿⣿⣿⣧⢀⠀⠀⠀⠀
⠀⠀⠀⣿⣿⣿⠋⠀⠀⠀⠀⠀⠙⠀⠙⣿⣿⣿⣷⢳⢀⠀⠀⠀
⠀⠀⣠⣿⣿⣿⢸⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣿⣿⣿⣿⣿⢀
⠀⠀⣸⣿⣿⣿⠸⠀⠀⠀⠒⠒⠒⠐⠀⠀⢿⣿⣿⣿⣿⣿⠀⠀
⠀⣴⣿⣿⣿⡿⠀⠒⣋⣙⡒⢰⠀⠤⣖⠒⢾⣿⣿⣿⣿⣧⠀⠀
⢺⣿⣿⣿⣿⢀⠀⠀⠉⠉⠉⠸⠀⡇⠉⠉⠀⢿⣿⣿⣿⣄⠀⠀
⠀⠙⣿⣿⣧⢻⠀⠀⠀⠀⠀⠠⠀⠰⠀⠀⠀⣸⠸⣿⣿⠿⠰⠀
⠀⠀⠀⠹⣿⣿⣿⣷⠀⡠⠙⣲⣔⣅⢡⣰⣷⣿⣿⣿⣧⠀⠀⠀
⠀⠀⠀⣼⣿⣿⣿⣿⠀⡿⠭⠭⠭⠭⢿⠀⣿⢻⣿⣿⠃⠀⠀⠀
⠀⠀⠀⠙⠛⣿⢻⠹⣿⠐⠙⠛⠟⠉⢀⣴⡟⢿⣿⡏⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⡟⠀⠀⠻⣦⣤⣶⠾⠋⠀⠀⠁⡦⢄⢀⠀⠀⠀
⠀⠀⠀⠀⡠⠁⡇⠑⢄⠀⠀⠀⠀⠀⠀⠔⠀⠀⠁⠀⠀⠀⠉⠁
⠀⠔⠊⠁⠀⠀⣇⠀⠀⠀⠑⡤⠤⢎⠁⠀⠀⡘⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⢢⠠⠀⡠⢆⠀⠀⡠⠙⢄⠀⡸⠀
**/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define ld long double
#define mk make_pair
#define pb push_back
#define in insert
#define se second
#define fi first
#define mod 1000000007
#define watch(x) cout << (#x) << " is " << (x) << "\\n"
#define all(v) (v).begin(), (v).end()
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(0);
#define pii pair<ll, ll>
#define inf 1000000000000000000
#define vi(x) vector<x>
#define maxn 100005
#define ordered_set                                                            \\
  tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>
using namespace std;
using namespace __gnu_pbds;
ll dx[] = {1, 0, -1, 0};
ll dy[] = {0, 1, 0, -1};
ll n, m;
bool isafe(ll x, ll y) {
  if (x >= 1 and x <= n and y >= 1 and y <= m)
    return true;
  return false;
}
signed main() {
  fast;

  ll k, i, j;
  cin >> n >> m >> k;
  char mat[n + 2][m + 2];
  ll x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++)
      cin >> mat[i][j];
  }
  vector<vector<ll>> dj(n + 2, vector<ll>(m + 2, inf));
  queue<pii> q;
  q.push({x1, y1});
  dj[x1][y1] = 0;
  while (!q.empty()) {
    pii st = q.front();
    q.pop();
    for (i = 0; i < 4; i++) {
      for (j = 1; j <= k; j++) {
        ll nx = dx[i] * j + st.fi;
        ll ny = dy[i] * j + st.se;
        if (isafe(nx, ny)) {
          if (mat[nx][ny] == '@')
            break;
          if (dj[nx][ny] <= dj[st.fi][st.se] and dj[nx][ny] != inf)
            break;
          if (dj[nx][ny] == inf) {
            dj[nx][ny] = dj[st.fi][st.se] + 1;
            q.push({nx, ny});
          }
        }
      }
    }
  }
  // cout<<dj[x2][y2];
  cout << ((dj[x2][y2] == inf) ? -1 : dj[x2][y2]);
}

Error: TLE

Fixed submission:

// Nihal Mittal - nihal_47
// Strike First , Strike Hard , No Mercy !!

/**⠀⠀⠀⠀⠀⠀⣀⣤⣤⣄⣀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⣠⣾⣿⣿⣿⣿⣿⣿⣿⣿⣿⣦⣀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⣰⣿⣿⣿⣿⠿⠿⣿⣿⣿⣿⣿⣿⣿⣧⢀⠀⠀⠀⠀
⠀⠀⠀⣿⣿⣿⠋⠀⠀⠀⠀⠀⠙⠀⠙⣿⣿⣿⣷⢳⢀⠀⠀⠀
⠀⠀⣠⣿⣿⣿⢸⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣿⣿⣿⣿⣿⢀
⠀⠀⣸⣿⣿⣿⠸⠀⠀⠀⠒⠒⠒⠐⠀⠀⢿⣿⣿⣿⣿⣿⠀⠀
⠀⣴⣿⣿⣿⡿⠀⠒⣋⣙⡒⢰⠀⠤⣖⠒⢾⣿⣿⣿⣿⣧⠀⠀
⢺⣿⣿⣿⣿⢀⠀⠀⠉⠉⠉⠸⠀⡇⠉⠉⠀⢿⣿⣿⣿⣄⠀⠀
⠀⠙⣿⣿⣧⢻⠀⠀⠀⠀⠀⠠⠀⠰⠀⠀⠀⣸⠸⣿⣿⠿⠰⠀
⠀⠀⠀⠹⣿⣿⣿⣷⠀⡠⠙⣲⣔⣅⢡⣰⣷⣿⣿⣿⣧⠀⠀⠀
⠀⠀⠀⣼⣿⣿⣿⣿⠀⡿⠭⠭⠭⠭⢿⠀⣿⢻⣿⣿⠃⠀⠀⠀
⠀⠀⠀⠙⠛⣿⢻⠹⣿⠐⠙⠛⠟⠉⢀⣴⡟⢿⣿⡏⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⡟⠀⠀⠻⣦⣤⣶⠾⠋⠀⠀⠁⡦⢄⢀⠀⠀⠀
⠀⠀⠀⠀⡠⠁⡇⠑⢄⠀⠀⠀⠀⠀⠀⠔⠀⠀⠁⠀⠀⠀⠉⠁
⠀⠔⠊⠁⠀⠀⣇⠀⠀⠀⠑⡤⠤⢎⠁⠀⠀⡘⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⢢⠠⠀⡠⢆⠀⠀⡠⠙⢄⠀⡸⠀
**/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define ld long double
#define mk make_pair
#define pb push_back
#define in insert
#define se second
#define fi first
#define mod 1000000007
#define watch(x) cout << (#x) << " is " << (x) << "\\n"
#define all(v) (v).begin(), (v).end()
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(0);
#define pii pair<ll, ll>
#define inf 1000000000000000000
#define vi(x) vector<x>
#define maxn 100005
#define ordered_set                                                            \\
  tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>
using namespace std;
using namespace __gnu_pbds;
ll dx[] = {1, 0, -1, 0};
ll dy[] = {0, 1, 0, -1};
ll n, m;
bool isafe(ll x, ll y) {
  if (x >= 1 and x <= n and y >= 1 and y <= m)
    return true;
  return false;
}
signed main() {
  fast;

  ll k, i, j;
  cin >> n >> m >> k;
  char mat[n + 2][m + 2];
  ll x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++)
      cin >> mat[i][j];
  }
  vector<vector<ll>> dj(n + 2, vector<ll>(m + 2, inf));
  queue<pii> q;
  q.push({x1, y1});
  dj[x1][y1] = 0;
  while (!q.empty()) {
    pii st = q.front();
    q.pop();
    for (i = 0; i < 4; i++) {
      for (j = 1; j <= k; j++) {
        ll nx = dx[i] * j + st.fi;
        ll ny = dy[i] * j + st.se;
        if (isafe(nx, ny)) {
          if (mat[nx][ny] == '@')
            break;
          if (dj[nx][ny] <= dj[st.fi][st.se] and dj[nx][ny] != inf)
            continue;
          if (dj[nx][ny] == inf) {
            dj[nx][ny] = dj[st.fi][st.se] + 1;
            q.push({nx, ny});
          }
        }
      }
    }
  }
  // cout<<dj[x2][y2];
  cout << ((dj[x2][y2] == inf) ? -1 : dj[x2][y2]);
}