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

/*input
1 6 4
1 1 1 6
......
*/

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <unistd.h>

#define rep(i, j, n) for (i = j; i < n; i++)
#define repi(i, j, n) for (i = j; i > n; i--)
#define inf 1e9
#define M 1000000007
#define pie 3.141592653589793238
#define ll long long
#define ld long double
#define vll vector<ll>
#define pll pair<ll, ll>
#define vpll vector<pll>
#define pb push_back
#define F first
#define S second
#define endl '\\n'
#define UQ(x) (x).resize(distance((x).begin(), unique(all((x)))))

using namespace std;
using namespace __gnu_pbds;

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

mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
mt19937_64
    rnd64(chrono::high_resolution_clock::now().time_since_epoch().count());

ll power(ll a, ll m, ll mod) {
  ll ans = 1;
  while (m) {
    if (m % 2) {
      ans *= a;
      ans %= (mod);
    }
    a = (a * a) % (mod);
    m >>= 1;
  }
  return ans;
}

void init(ll *a, ll n, ll val) {
  for (ll i = 0; i < n; i++)
    a[i] = val;
}

ll dx[4] = {1, 0, -1, 0};
ll dy[4] = {0, 1, 0, -1};

void solve() {
  ll i, j, _;
  ll h, w, k, x, y;
  cin >> h >> w >> k;
  char ch;
  ll x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2, --x1, --x2, --y1, --y2;
  ll a[h][w];
  ll b[h][w];
  rep(i, 0, h) {
    rep(j, 0, w) {
      cin >> ch;
      a[i][j] = (ch == '.');
      b[i][j] = inf;
    }
  }
  if (!a[x1][y1] || !a[x2][y2]) {
    cout << -1 << endl;
    return;
  }
  b[x1][y1] = 0;
  list<pll> queue;
  queue.pb({x1, y1});
  while (!queue.empty()) {
    pll z = queue.front();
    queue.pop_front();
    rep(i, 0, 4) {
      x = z.F, y = z.S;
      rep(j, 0, k) {
        x += dx[i], y += +dy[i];
        if (x < 0 || y < 0 || x > h - 1 || y > w - 1 || !a[x][y])
          break;
        if (b[x][y] < b[z.F][z.S] + 1)
          break;
        if (b[x][y] < inf)
          continue;
        b[x][y] = b[z.F][z.S] + 1;
        queue.pb({x, y});
      }
    }
  }
  // rep(i,0,h){
  //     rep(j,0,w)cout<<b[i][j]<<' ';cout<<endl;
  // }
  cout << ((b[x2][y2] == inf) ? -1 : b[x2][y2]) << endl;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  ll T = 1;
  // cin>>T;
  while (T--) {
    solve();
  }
  return 0;
}


Error: TLE

Fixed submission:

/*input
1 6 4
1 1 1 6
......
*/

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <unistd.h>

#define rep(i, j, n) for (i = j; i < n; i++)
#define repi(i, j, n) for (i = j; i > n; i--)
#define inf 1e9
#define M 1000000007
#define pie 3.141592653589793238
#define ll long long
#define ld long double
#define vll vector<ll>
#define pll pair<ll, ll>
#define vpll vector<pll>
#define pb push_back
#define F first
#define S second
#define endl '\\n'
#define UQ(x) (x).resize(distance((x).begin(), unique(all((x)))))

using namespace std;
using namespace __gnu_pbds;

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

mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
mt19937_64
    rnd64(chrono::high_resolution_clock::now().time_since_epoch().count());

ll power(ll a, ll m, ll mod) {
  ll ans = 1;
  while (m) {
    if (m % 2) {
      ans *= a;
      ans %= (mod);
    }
    a = (a * a) % (mod);
    m >>= 1;
  }
  return ans;
}

void init(ll *a, ll n, ll val) {
  for (ll i = 0; i < n; i++)
    a[i] = val;
}

ll dx[4] = {1, 0, -1, 0};
ll dy[4] = {0, 1, 0, -1};

void solve() {
  ll i, j, _;
  ll h, w, k, x, y;
  cin >> h >> w >> k;
  char ch;
  ll x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2, --x1, --x2, --y1, --y2;
  ll a[h][w];
  ll b[h][w];
  rep(i, 0, h) {
    rep(j, 0, w) {
      cin >> ch;
      a[i][j] = (ch == '.');
      b[i][j] = inf;
    }
  }
  if (!a[x1][y1] || !a[x2][y2]) {
    cout << -1 << endl;
    return;
  }
  b[x1][y1] = 0;
  list<pll> queue;
  queue.pb({x1, y1});
  while (!queue.empty()) {
    pll z = queue.front();
    queue.pop_front();
    rep(i, 0, 4) {
      x = z.F, y = z.S;
      rep(j, 0, k) {
        x += dx[i], y += +dy[i];
        if (x < 0 || y < 0 || x > h - 1 || y > w - 1 || !a[x][y])
          break;
        if (b[x][y] < b[z.F][z.S] + 1)
          break;
        b[x][y] = b[z.F][z.S] + 1;
        queue.pb({x, y});
      }
    }
  }
  // rep(i,0,h){
  //     rep(j,0,w)cout<<b[i][j]<<' ';cout<<endl;
  // }
  cout << ((b[x2][y2] == inf) ? -1 : b[x2][y2]) << endl;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  ll T = 1;
  // cin>>T;
  while (T--) {
    solve();
  }
  return 0;
}