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

#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;
typedef long double ldb;
typedef pair<ll, ll> pll;
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define V vector
#define endl "\\n"
#define CLEAR(a) memset(a, 0, sizeof(a))
#define all(x) x.begin(), x.end()
#define sz(x) ((ll)x.size())
#define rep0(i, n) for (ll i = 0; i < n; i++)
#define rep1(i, n) for (ll i = 1; i <= n; i++)
#define per(i, a, b) for (ll i = a; i >= b; i--)
#define ios_tie()                                                              \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);                                                              \\
  cerr.tie(nullptr)
#define trace1(a) cout << #a << ":" << a << endl;
#define trace2(a, b) cout << #a << ":" << a << " | " << #b << ":" << b << endl;
#define trace3(a, b, c)                                                        \\
  cout << #a << ":" << a << " | " << #b << ":" << b << " | " << #c << ":" << c \\
       << endl;
#define trace4(a, b, c, d)                                                     \\
  cout << #a << ":" << a << " | " << #b << ":" << b << " | " << #c << ":" << c \\
       << " | " << #d << ":" << d << endl;

const ll inf = 1e18;
const ll mod = 1e9 + 7;
const ll maxn = 1e5 + 10;
const ldb PI = acos(-1);

void lol(ll s) {
  cerr << "check........................................... " << s << endl;
}
ll power(ll x, ll y) {
  ll res = 1;
  x %= mod;
  while (y) {
    if (y & 1)
      res = (res * x) % mod;
    y >>= 1LL;
    x = (x * x) % mod;
  }
  return res;
}
ll inv(ll x) {
  return power(x, mod - 2);
} //(1/x)%y = x^(y-2) % y;   // pow(x,y)%m = pow(x,y%(m-1))%m
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

// I can solve mirror cube in 2 mins but can't solve div2 C in 1hr.
void solve() {
  ll n, m, k;
  cin >> n >> m >> k;
  char mat[n + 1][m + 1];
  ll x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  ll mn[n + 1][m + 1];
  rep1(i, n) {
    rep1(j, m) {
      cin >> mat[i][j];
      mn[i][j] = inf;
    }
  }
  queue<pll> q;
  q.push({x1, y1});
  mn[x1][y1] = 0;
  ll dx[4] = {-1, 1, 0, 0};
  ll dy[4] = {0, 0, 1, -1};
  while (!q.empty()) {
    ll i = q.front().ff;
    ll j = q.front().ss;
    q.pop();
    rep0(l, 4) {
      for (ll a = 1; a <= k; a++) {
        ll newj = j + a * dx[l];
        ll newi = i + a * dy[l];
        if (newi >= 1 && newi <= n && newj >= 1 && newj <= m &&
            mat[newi][newj] == '.') {
          if (mn[newi][newj] < mn[i][j] + 1) {
            break;
          }
          if (mn[newi][newj] != inf) {
            continue;
          }
          q.push({newi, newj});
          mn[newi][newj] = mn[i][j] + 1;
        } else {
          break;
        }
      }
    }
  }
  if (mn[x2][y2] == inf)
    mn[x2][y2] = -1;
  cout << mn[x2][y2] << endl;
}
int main() {
  ios_tie();
  // freopen("input.txt", "r", stdin);
  // freopen("myOut.txt", "w", stdout);
  ll _ = 1;
  // cin>>_;
  rep1(i, _) {
    // cout<<"Case #"<<i<<": ";
    solve();
  }
  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;
typedef long double ldb;
typedef pair<ll, ll> pll;
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define V vector
#define endl "\\n"
#define CLEAR(a) memset(a, 0, sizeof(a))
#define all(x) x.begin(), x.end()
#define sz(x) ((ll)x.size())
#define rep0(i, n) for (ll i = 0; i < n; i++)
#define rep1(i, n) for (ll i = 1; i <= n; i++)
#define per(i, a, b) for (ll i = a; i >= b; i--)
#define ios_tie()                                                              \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);                                                              \\
  cerr.tie(nullptr)
#define trace1(a) cout << #a << ":" << a << endl;
#define trace2(a, b) cout << #a << ":" << a << " | " << #b << ":" << b << endl;
#define trace3(a, b, c)                                                        \\
  cout << #a << ":" << a << " | " << #b << ":" << b << " | " << #c << ":" << c \\
       << endl;
#define trace4(a, b, c, d)                                                     \\
  cout << #a << ":" << a << " | " << #b << ":" << b << " | " << #c << ":" << c \\
       << " | " << #d << ":" << d << endl;

const ll inf = 1e18;
const ll mod = 1e9 + 7;
const ll maxn = 1e5 + 10;
const ldb PI = acos(-1);

void lol(ll s) {
  cerr << "check........................................... " << s << endl;
}
ll power(ll x, ll y) {
  ll res = 1;
  x %= mod;
  while (y) {
    if (y & 1)
      res = (res * x) % mod;
    y >>= 1LL;
    x = (x * x) % mod;
  }
  return res;
}
ll inv(ll x) {
  return power(x, mod - 2);
} //(1/x)%y = x^(y-2) % y;   // pow(x,y)%m = pow(x,y%(m-1))%m
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

// I can solve mirror cube in 2 mins but can't solve div2 C in 1hr.
void solve() {
  ll n, m, k;
  cin >> n >> m >> k;
  char mat[n + 1][m + 1];
  ll x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  ll mn[n + 1][m + 1];
  rep1(i, n) {
    rep1(j, m) {
      cin >> mat[i][j];
      mn[i][j] = -1;
    }
  }
  queue<pll> q;
  q.push({x1, y1});
  mn[x1][y1] = 0;
  ll dx[4] = {-1, 1, 0, 0};
  ll dy[4] = {0, 0, 1, -1};
  while (!q.empty()) {
    ll i = q.front().ff;
    ll j = q.front().ss;
    q.pop();
    rep0(l, 4) {
      rep1(a, k) {
        ll newi = i + a * dx[l];
        ll newj = j + a * dy[l];
        if (newi >= 1 && newi <= n && newj >= 1 && newj <= m &&
            mat[newi][newj] == '.') {
          if (mn[newi][newj] != -1)
            continue;
          q.push({newi, newj});
          mn[newi][newj] = mn[i][j] + 1;
        } else {
          break;
        }
      }
    }
  }
  // rep1(i,n) {
  //     rep1(j,m) {
  //         cout<<mn[i][j]<< " " ;
  //     }
  //     cout<<endl;
  // }
  cout << mn[x2][y2] << endl;
}
int main() {
  ios_tie();
  // freopen("input.txt", "r", stdin);
  // freopen("myOut.txt", "w", stdout);
  ll _ = 1;
  // cin>>_;
  rep1(i, _) {
    // cout<<"Case #"<<i<<": ";
    solve();
  }
  return 0;
}