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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops,no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

using namespace __gnu_pbds;
using namespace std;

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define fbo find_by_order
#define ook order_of_key

template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<long long, long long> ii;
typedef complex<long double> com;

const ll mod = 1e9 + 7;

vector<vector<ll>> dp;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n, m, k;
  cin >> n >> m >> k;
  dp.resize(n, vector<ll>(m, LLONG_MAX));
  ll x1, x2, y1, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  y1--;
  x2--;
  y2--;
  vector<string> arr(n);
  for (int z = 0; z < n; z++) {
    cin >> arr[z];
  }
  queue<ii> que;
  que.push({x1, y1});
  vector<vector<bool>> visited(n, vector<bool>(m, false));

  dp[x1][y1] = 0;
  visited[x1][y1] = true;
  while (!que.empty()) {
    ll ox = que.front().fi;
    ll oy = que.front().se;
    que.pop();
    for (int x = ox + 1, i = 0; x < n && i < k; x++, i++) {
      if (arr[x][oy] == '@')
        break;
      if (dp[ox][oy] >= dp[x][oy])
        break;
      dp[x][oy] = dp[ox][oy] + 1;
      if (!visited[x][oy])
        que.push({x, oy});
      visited[x][oy] = true;
    }
    for (int x = ox - 1, i = 0; x >= 0 && i < k; x--, i++) {
      if (arr[x][oy] == '@')
        break;
      if (dp[ox][oy] >= dp[x][oy])
        break;
      dp[x][oy] = dp[ox][oy] + 1;
      if (!visited[x][oy])
        que.push({x, oy});
      visited[x][oy] = true;
    }
    for (int y = oy + 1, i = 0; y < m && i < k; y++, i++) {
      if (arr[ox][y] == '@')
        break;
      if (dp[ox][oy] >= dp[ox][y])
        break;
      dp[ox][y] = dp[ox][oy] + 1;
      if (!visited[ox][y])
        que.push({ox, y});
      visited[ox][y] = true;
    }
    for (int y = oy - 1, i = 0; y >= 0 && i < k; y--, i++) {
      if (arr[ox][y] == '@')
        break;
      if (dp[ox][oy] >= dp[ox][y])
        break;
      dp[ox][y] = dp[ox][oy] + 1;
      if (!visited[ox][y])
        que.push({ox, y});
      visited[ox][y] = true;
    }
  }
  if (dp[x2][y2] == LLONG_MAX)
    cout << -1 << endl;
  else
    cout << dp[x2][y2] << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops,no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

using namespace __gnu_pbds;
using namespace std;

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define fbo find_by_order
#define ook order_of_key

template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<long long, long long> ii;
typedef complex<long double> com;

const ll mod = 1e9 + 7;

vector<vector<ll>> dp;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n, m, k;
  cin >> n >> m >> k;
  dp.resize(n, vector<ll>(m, LLONG_MAX));
  ll x1, x2, y1, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  y1--;
  x2--;
  y2--;
  vector<string> arr(n);
  for (int z = 0; z < n; z++) {
    cin >> arr[z];
  }
  queue<ii> que;
  que.push({x1, y1});
  dp[x1][y1] = 0;
  while (!que.empty()) {
    ll ox = que.front().fi;
    ll oy = que.front().se;
    que.pop();
    for (int x = ox + 1, i = 0; x < n && i < k; x++, i++) {
      if (arr[x][oy] == '@')
        break;
      if (dp[ox][oy] >= dp[x][oy])
        break;
      dp[x][oy] = dp[ox][oy] + 1;
      que.push({x, oy});
    }
    for (int x = ox - 1, i = 0; x >= 0 && i < k; x--, i++) {
      if (arr[x][oy] == '@')
        break;
      if (dp[ox][oy] >= dp[x][oy])
        break;
      dp[x][oy] = dp[ox][oy] + 1;
      que.push({x, oy});
    }
    for (int y = oy + 1, i = 0; y < m && i < k; y++, i++) {
      if (arr[ox][y] == '@')
        break;
      if (dp[ox][oy] >= dp[ox][y])
        break;
      dp[ox][y] = dp[ox][oy] + 1;
      que.push({ox, y});
    }
    for (int y = oy - 1, i = 0; y >= 0 && i < k; y--, i++) {
      if (arr[ox][y] == '@')
        break;
      if (dp[ox][oy] >= dp[ox][y])
        break;
      dp[ox][y] = dp[ox][oy] + 1;
      que.push({ox, y});
    }
  }
  if (dp[x2][y2] == LLONG_MAX)
    cout << -1 << endl;
  else
    cout << dp[x2][y2] << endl;
}