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

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <vector>
// #include<unordered_set>

using namespace std;

/*
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;

tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
st;*/

#pragma GCC optimize("Ofast")
#pragma GCC optimize("fast-math")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,tune=native")

#define pb push_back
#define ld long double

mt19937 rnd(51);

int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, m, k, x1, y1, x2, y2;
  cin >> n >> m >> k >> x1 >> y1 >> x2 >> y2;
  x1--, y1--, x2--, y2--;
  vector<vector<char>> v(n, vector<char>(m));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> v[i][j];
    }
  }
  vector<vector<int>> d(n, vector<int>(m, 1e9));
  queue<pair<int, int>> q;
  q.push({x1, y1});
  d[x1][y1] = 0;
  while (!q.empty()) {
    int i = q.front().first, j = q.front().second;
    q.pop();
    for (int l = 1; l <= k && i + l < n; l++) {
      if (v[i + l][j] == '@' || d[i + l][j] < d[i][j] + 1) {
        break;
      }
      if (d[i + l][j] > d[i][j] + 1) {
        d[i + l][j] = d[i][j] + 1;
        q.push({i + l, j});
      }
    }
    for (int l = 1; l <= k && i - l >= 0; l++) {
      if (v[i - l][j] == '@' || d[i - l][j] < d[i][j] + 1) {
        break;
      }
      if (d[i - l][j] > d[i][j] + 1) {
        d[i - l][j] = d[i][j] + 1;
        q.push({i - l, j});
      }
    }
    for (int l = 1; l <= k && j + l < m; l++) {
      if (v[i][j + l] == '@' || d[i][j + l] < d[i][j] + 1) {
        break;
      }
      if (d[i][j + l] > d[i][j] + 1) {
        d[i][j + l] = d[i][j] + 1;
        q.push({i, j + l});
      }
    }
    for (int l = 1; l <= k && j - l >= 0; l++) {
      if (v[i][j - l] == '@' || d[i][j - l] < d[i][j] + 1) {
        break;
      }
      if (d[i][j - l] > d[i][j] + 1) {
        d[i][j - l] = d[i][j] + 1;
        q.push({i, j - l});
      }
    }
  }
  if (d[x2][y2] == 1e9) {
    cout << -1;
  } else {
    cout << d[x2][y2] << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <vector>
// #include<unordered_set>

using namespace std;

/*
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;

tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
st;*/

#pragma GCC optimize("Ofast")
#pragma GCC optimize("fast-math")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,tune=native")

#define pb push_back
#define ld long double

mt19937 rnd(51);

int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, m, k, x1, y1, x2, y2;
  cin >> n >> m >> k >> x1 >> y1 >> x2 >> y2;
  x1--, y1--, x2--, y2--;
  vector<vector<char>> v(n, vector<char>(m));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> v[i][j];
    }
  }
  vector<vector<int>> d(n, vector<int>(m, 1e9));
  queue<pair<int, int>> q;
  q.push({x1, y1});
  d[x1][y1] = 0;
  while (!q.empty()) {
    int i = q.front().first, j = q.front().second;
    q.pop();
    for (int l = 1; l <= k && i + l < n; l++) {
      if (v[i + l][j] == '@' || d[i + l][j] < d[i][j]) {
        break;
      }
      if (d[i + l][j] > d[i][j] + 1) {
        d[i + l][j] = d[i][j] + 1;
        q.push({i + l, j});
      }
    }
    for (int l = 1; l <= k && i - l >= 0; l++) {
      if (v[i - l][j] == '@' || d[i - l][j] < d[i][j]) {
        break;
      }
      if (d[i - l][j] > d[i][j] + 1) {
        d[i - l][j] = d[i][j] + 1;
        q.push({i - l, j});
      }
    }
    for (int l = 1; l <= k && j + l < m; l++) {
      if (v[i][j + l] == '@' || d[i][j + l] < d[i][j]) {
        break;
      }
      if (d[i][j + l] > d[i][j] + 1) {
        d[i][j + l] = d[i][j] + 1;
        q.push({i, j + l});
      }
    }
    for (int l = 1; l <= k && j - l >= 0; l++) {
      if (v[i][j - l] == '@' || d[i][j - l] < d[i][j]) {
        break;
      }
      if (d[i][j - l] > d[i][j] + 1) {
        d[i][j - l] = d[i][j] + 1;
        q.push({i, j - l});
      }
    }
  }
  if (d[x2][y2] == 1e9) {
    cout << -1;
  } else {
    cout << d[x2][y2] << endl;
  }
  return 0;
}