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

#include <bits/stdc++.h>

typedef long long ll;
typedef long double ld;
using namespace std;
mt19937_64 mrand(chrono::steady_clock::now().time_since_epoch().count());
// mt19937_64 mrand(42);
#define ii for (int i = 1; i <= n; ++i)
#define ji for (int j = 1; j <= n; ++j)
#define jj for (int j = 1; j <= m; ++j)
#define ij for (int i = 1; i <= m; ++i)
#define sz(x) ((ll)x.size())
#define all(x) x.begin(), x.end()
#define al(x) x + 1, x + 1 + n
#define asd cout << "ok" << endl;
#define asdd cout << "okok" << endl;
#define pii pair<int, int>
#define vi vector<int>
#define vvi vector<vector<int>>
#define vl vector<ll>
#define vii vector<pair<int, int>>
#define pr(v)                                                                  \\
  for (auto i : v)                                                             \\
    cout << i << " ";                                                          \\
  cout << endl
#define prt(a, l, r)                                                           \\
  for (auto i = l; i <= r; ++i)                                                \\
    cout << a[i] << " ";                                                       \\
  cout << endl;
#define pc(x) __builtin_popcount(x)
#define pb push_back

const int maxn = 1000005;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, m, k;
  cin >> n >> m >> k;
  vector<vector<char>> a(n + 1, vector<char>(m + 1));
  vector<vector<int>> dis(n + 1, vector<int>(m + 1));
  ii jj dis[i][j] = 1e9;
  int sx, sy, tx, ty;
  cin >> sx >> sy >> tx >> ty;
  ii jj cin >> a[i][j];
  auto get = [&](int x, int y) { return (x - 1) * m + y; };
  queue<pair<int, int>> q;
  q.push({sx, sy});
  dis[sx][sy] = 0;
  int dir1[4] = {-1, 1, 0, 0};
  int dir2[4] = {0, 0, -1, 1};
  while (!q.empty()) {
    auto p = q.front();
    q.pop();
    int d = dis[p.first][p.second];
    //        cout<<p.first<<" "<<p.second<<" "<<d<<endl;
    if (p.first == tx && p.second == ty)
      return cout << d << endl, 0;
    for (int i = 0; i < 4; ++i) {
      int x = p.first, y = p.second;
      for (int j = 1; j <= k; ++j) {
        x += dir1[i], y += dir2[i];
        if (x <= 0 || y <= 0 || x > n || y > m || dis[x][y] <= d ||
            a[x][y] == '@')
          break;
        if (dis[x][y] > d + 1) {
          dis[x][y] = d + 1;
          q.push({x, y});
        }
      }
    }
  }
  cout << -1 << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

typedef long long ll;
typedef long double ld;
using namespace std;
mt19937_64 mrand(chrono::steady_clock::now().time_since_epoch().count());
// mt19937_64 mrand(42);
#define ii for (int i = 1; i <= n; ++i)
#define ji for (int j = 1; j <= n; ++j)
#define jj for (int j = 1; j <= m; ++j)
#define ij for (int i = 1; i <= m; ++i)
#define sz(x) ((ll)x.size())
#define all(x) x.begin(), x.end()
#define al(x) x + 1, x + 1 + n
#define asd cout << "ok" << endl;
#define asdd cout << "okok" << endl;
#define pii pair<int, int>
#define vi vector<int>
#define vvi vector<vector<int>>
#define vl vector<ll>
#define vii vector<pair<int, int>>
#define pr(v)                                                                  \\
  for (auto i : v)                                                             \\
    cout << i << " ";                                                          \\
  cout << endl
#define prt(a, l, r)                                                           \\
  for (auto i = l; i <= r; ++i)                                                \\
    cout << a[i] << " ";                                                       \\
  cout << endl;
#define pc(x) __builtin_popcount(x)
#define pb push_back

const int maxn = 1000005;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, m, k;
  cin >> n >> m >> k;
  vector<vector<char>> a(n + 1, vector<char>(m + 1));
  vector<vector<int>> dis(n + 1, vector<int>(m + 1));
  ii jj dis[i][j] = 1e9;
  int sx, sy, tx, ty;
  cin >> sx >> sy >> tx >> ty;
  ii jj cin >> a[i][j];
  auto get = [&](int x, int y) { return (x - 1) * m + y; };
  queue<pair<int, int>> q;
  q.push({sx, sy});
  dis[sx][sy] = 0;
  int dir1[4] = {-1, 1, 0, 0};
  int dir2[4] = {0, 0, -1, 1};
  while (!q.empty()) {
    auto p = q.front();
    q.pop();
    int d = dis[p.first][p.second];
    if (p.first == tx && p.second == ty)
      return cout << d << endl, 0;
    for (int i = 0; i < 4; ++i) {
      int x = p.first, y = p.second;
      for (int j = 1; j <= k; ++j) {
        x += dir1[i], y += dir2[i];
        if (x <= 0 || y <= 0 || x > n || y > m || dis[x][y] < d ||
            a[x][y] == '@')
          break;
        if (dis[x][y] > d) {
          dis[x][y] = d + 1;
          q.push({x, y});
        }
      }
    }
  }
  cout << -1 << endl;
}