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

#include <bits/stdc++.h>
// #pragma GCC optimize("Ofast")
// #pragma GCC target("avx,avx2,fma")

using namespace std;

typedef long long ll;

// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// typedef
// tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>pb_ds;

#define IN freopen("in.txt", "r", stdin)
#define OUT freopen("out.txt", "w", stdout)
#define pi pair<int, int>
#define F first
#define S second
#define pb push_back
#define pp pop_back
#define mod 1000000007
#define pai acos(-1)
#define N 1005

int r, c;
int fx[] = {-1, 1, 0, 0};
int fy[] = {0, 0, 1, -1};

bool valid(int x, int y) { return x >= 1 && x <= r && y >= 1 && y <= c; }

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m, k, sx, sy, dx, dy;
  cin >> n >> m >> k >> sx >> sy >> dx >> dy;
  r = n, c = m;
  char s[n + 5][m + 5];
  int dis[n + 5][m + 5];
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> s[i][j];
      dis[i][j] = INT_MAX;
    }
  }
  queue<pi> q;
  if (s[sx][sy] == '.')
    q.push({sx, sy}), dis[sx][sy] = 0;
  while (!q.empty()) {
    pi u = q.front();
    q.pop();
    for (int i = 0; i < 4; i++) {
      for (int j = 1; j <= k; j++) {
        int nx = u.F + fx[i] * j;
        int ny = u.S + fy[i] * j;
        if (!valid(nx, ny) || s[nx][ny] == '@' || dis[nx][ny] <= dis[u.F][u.S])
          break;
        if (dis[nx][ny] > dis[u.F][u.S] + 1) {
          dis[nx][ny] = dis[u.F][u.S] + 1;
          q.push({nx, ny});
        }
      }
    }
  }
  int ans = (dis[dx][dy] == INT_MAX) ? -1 : dis[dx][dy];
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
#define orderd_set                                                             \\
  tree<pair<int, int>, null_type, greater<pair<int, int>>, rb_tree_tag,        \\
       tree_order_statistics_node_update>
// find_by_order(k)
// order_of_key(k)
#define FIO                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);
const int N = 2e3 + 69;
#define ALL(x) begin(x), end(x)
#define sz(x) (ll) x.size()
const int OO = 0x3f3f3f3f;
const int mod = 1000000007;

int n, m, k, xs, ys, xt, yt;
string grid[N];
vector<vector<ll>> dis;
int mvx[] = {1, -1, 0, 0};
int mvy[] = {0, 0, 1, -1};

bool valid(int x, int y) {
  return x < n && x >= 0 && y < m && y >= 0 && grid[x][y] == '.';
}

void bfs(int sx, int sy) {
  queue<pair<int, int>> q;
  q.push({sx, sy});
  dis[sx][sy] = 0;
  while (sz(q)) {
    int ux = q.front().first;
    int uy = q.front().second;
    q.pop();
    for (int i = 0; i < 4; i++) {
      for (int j = 1; j <= k; j++) {
        int vx = ux + mvx[i] * j;
        int vy = uy + mvy[i] * j;
        if (valid(vx, vy)) {
          if (dis[vx][vy] > dis[ux][uy] + 1) {
            dis[vx][vy] = dis[ux][uy] + 1;
            q.push({vx, vy});
          }
        } else
          break;
      }
    }
  }
}

int main() {
  FIO cin >> n >> m >> k >> xs >> ys >> xt >> yt;
  dis = vector<vector<ll>>(n + 69, vector<ll>(m + 69, OO));
  for (int i = 0; i < n; i++) {
    cin >> grid[i];
  }
  xs--;
  xt--;
  yt--;
  ys--;
  bfs(xs, ys);
  //    for(int i = 0; i< n; i++)
  //    {
  //        for(int j = 0; j< m; j++)
  //        {
  //            cout << dis[i][j] << "          ";
  //        }
  //        cout << "\\n";
  //    }
  if (dis[xt][yt] == OO)
    cout << -1 << "\\n";
  else
    cout << dis[xt][yt] << "\\n";
  return 0;
}