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

// Catchharsh
// Legendary Sage Jiraiya Sensei
// https://codeforces.com/contest/877/problem/D
#include <bits/stdc++.h>
// #pragma GCC optimize "03"
#define ll int
#define ld long double
#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define FRE                                                                    \\
  freopen("input.txt", "r", stdin);                                            \\
  freopen("output.txt", "w", stdout);
#define F first
#define S second
#define sz(x) (ll) x.size()
#define all(x) x.begin(), x.end()
#define pb push_back
#define endl '\\n'
#define debug(x) cout << #x << " = " << x << endl
#define catchharsh cout << "\\n---------catchharsh---------\\n"
const ll MOD = 1e9 + 7;
const ll INF = 1011111111;
// const ll LLINF = 1000111000111000111LL;
const ld EPS = 1e-10;
const ld PI = 3.14159265358979323;
using namespace std;
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// #define ordered_set tree<ll, null_type,less<ll>,
// rb_tree_tag,tree_order_statistics_node_update>
// inline ll add(ll a,ll b){a+=b;if(a>=MOD)a-=MOD;return a;}
// inline ll sub(ll a,ll b){a-=b;if(a<0)a+=MOD;return a;}
// inline ll mul(ll a,ll b){return (a*1ll*b)%MOD;}
ll power(ll base, ll exponent) {
  ll ans = 1;
  // base%=MOD;
  // exponent%=(MOD-1);
  while (exponent != 0) {
    if (exponent & 1)
      ans = (1LL * ans * base);
    base = (1LL * base * base);
    exponent >>= 1;
  }
  return ans;
}
ll dx[] = {1, -1, 0, 0};
ll dy[] = {0, 0, 1, -1};
int32_t main() {
  IOS;
  ll n, m, i, j, k;
  cin >> n >> m >> k;
  vector<vector<ll>> grid(n, vector<ll>(m));
  vector<vector<bool>> visit(n, vector<bool>(m));
  vector<vector<ll>> dis(n, vector<ll>(m));
  ll xs, ys, xf, yf;
  cin >> xs >> ys >> xf >> yf;
  xs--;
  ys--;
  xf--;
  yf--;
  for (i = 0; i < n; i++) {
    for (j = 0; j < m; j++) {
      char c;
      cin >> c;
      if (c == '.')
        grid[i][j] = 0;
      else
        grid[i][j] = 1;
      dis[i][j] = -1;
      visit[i][j] = false;
    }
  }
  if (xs == xf && ys == yf)
    return cout << 0, 0;
  queue<pair<ll, ll>> q;
  q.push({xs, ys});
  dis[xs][ys] = 0;
  visit[xs][ys] = true;
  while (!q.empty()) {
    if (visit[xf][yf])
      break;
    ll now_x = q.front().F;
    ll now_y = q.front().S;
    q.pop();
    for (ll dir = 0; dir < 4; dir++) {
      for (ll len = 1; len <= k; len++) {
        ll n_x = now_x + dx[dir] * len;
        ll n_y = now_y + dy[dir] * len;
        if (n_x < 0 || n_x >= n || n_y < 0 || n_y >= m)
          break;
        if (visit[n_x][n_y] && dis[now_x][now_y] + 1 > dis[n_x][n_y])
          break;
        if (grid[n_x][n_y])
          break;
        if (visit[n_x][n_y])
          continue;
        dis[n_x][n_y] = dis[now_x][now_y] + 1;
        visit[n_x][n_y] = true;
        q.push({n_x, n_y});
      }
    }
  }
  cout << dis[xf][yf];

  cerr << endl
       << "Time elapsed : " << clock() * 1000.0 / CLOCKS_PER_SEC << " ms"
       << '\\n';
}

Error: TLE

Fixed submission:

// Catchharsh
#include <bits/stdc++.h>
#define ll long long int
#define ld long double
#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define F first
#define S second
#define sz(x) x.size()
#define all(x) x.begin(), x.end()
#define pb push_back
#define endl '\\n'
#define see(x) cout << #x << " = " << x << endl
const ll MOD = 1e9 + 7;
const ll INF = 1011111111;
const ll LLINF = 1000111000111000111LL;
const ld EPS = 1e-10;
const ld PI = 3.14159265358979323;
using namespace std;
ll power(ll base, ll exponent) {
  ll ans = 1;
  // base%=MOD;
  // exponent%=(MOD-1);
  while (exponent != 0) {
    if (exponent & 1)
      ans = (1LL * ans * base);
    base = (1LL * base * base);
    exponent >>= 1;
  }
  return ans;
}
int32_t main() {
  IOS;
  int n, m, i, j, k;
  cin >> n >> m >> k;
  int xs, ys, xf, yf;
  cin >> xs >> ys >> xf >> yf;
  xs--;
  ys--;
  xf--;
  yf--;
  vector<vector<ll>> grid(n, vector<ll>(m, 0));
  vector<vector<bool>> visit(n, vector<bool>(m, false));
  vector<vector<ll>> dis(n, vector<ll>(m, 0));
  for (i = 0; i < n; i++) {
    for (j = 0; j < m; j++) {
      char c;
      cin >> c;
      if (c == '.')
        grid[i][j] = 1;
      else
        grid[i][j] = 0;
      dis[i][j] = -1;
    }
  }
  if (xs == xf && ys == yf)
    return cout << 0, 0;
  queue<pair<int, int>> q;
  q.push({xs, ys});
  visit[xs][ys] = 1;
  dis[xs][ys] = 0;
  dis[xf][yf] = -1;
  while (!q.empty()) {
    if (visit[xf][yf])
      break;
    int now_x = q.front().F;
    int now_y = q.front().S;
    for (i = now_x + 1; i <= now_x + k && i < n; i++) {
      if (grid[i][now_y] == 0)
        break;
      if (!visit[i][now_y]) {
        dis[i][now_y] = dis[now_x][now_y] + 1;
        visit[i][now_y] = true;
        q.push({i, now_y});
      }
    }
    for (i = now_x - 1; i >= now_x - k && i >= 0; i--) {
      if (grid[i][now_y] == 0)
        break;
      if (!visit[i][now_y]) {
        dis[i][now_y] = dis[now_x][now_y] + 1;
        visit[i][now_y] = true;
        q.push({i, now_y});
      }
    }
    for (j = now_y - 1; j >= now_y - k && j >= 0; j--) {
      if (grid[now_x][j] == 0)
        break;
      if (!visit[now_x][j]) {
        dis[now_x][j] = dis[now_x][now_y] + 1;
        visit[now_x][j] = true;
        q.push({now_x, j});
      }
    }
    for (j = now_y + 1; j <= now_y + k && j < m; j++) {
      if (grid[now_x][j] == 0)
        break;
      if (!visit[now_x][j]) {
        dis[now_x][j] = dis[now_x][now_y] + 1;
        visit[now_x][j] = true;
        q.push({now_x, j});
      }
    }
    q.pop();
  }
  // for(i=0;i<n;i++)
  // {
  //   for(j=0;j<m;j++) cout<<dis[i][j]<<" ";
  //    cout<<endl;
  // }
  cout << dis[xf][yf];

  cerr << endl
       << "Time elapsed : " << clock() * 1000.0 / CLOCKS_PER_SEC << " ms"
       << '\\n';
}