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;

#define ll long long
#define fio                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define pb push_back
#define vi vector<ll>
#define pp push_pop
#define pll pair<ll, ll>
#define in insert
#define ini(a, n) memset(a, n, sizeof(a))
#define ff first
#define ss second
#define inf 10000000000000000
#define all(p) p.begin(), p.end()
#define que_max priority_queue<int>
#define que_min priority_queue<int, vi, greater<int>>
#define print(a)                                                               \\
  for (auto i : a) {                                                           \\
    cout << i << " ";                                                          \\
  }
#define print1(a)                                                              \\
  for (auto i : a) {                                                           \\
    cout << i.ff << " " << i.ss << endl;                                       \\
  }

typedef tree<pll, null_type, less<pll>, rb_tree_tag,
             tree_order_statistics_node_update>
    Set;
unordered_map<ll, set<pll, greater<pll>>> g;
unordered_map<ll, ll> vis;

const ll mod = 1e9 + 7;

ll power(ll x, ll y, ll p) {
  ll r = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1)
      r = (r * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return r;
}
const ll N = 2e5 + 10;
bool imp = false;
ll n, m, k;
ll xx1, yy1, x1, x2;
vector<vector<char>> a;
vector<vi> v, dp;
ll dx[4] = {-1, +1, 0, 0};
ll dy[4] = {0, 0, +1, -1};

void bfs(ll x, ll y) {
  queue<pll> q;
  q.push({x, y});
  v[x][y] = 1;
  dp[x][y] = 0;

  while (!q.empty()) {
    x = q.front().ff;
    y = q.front().ss;
    ll d = dp[x][y];
    q.pop();

    for (ll i = 0; i < 4; i++) {

      for (ll j = 1; j <= k; j++) {

        ll xp = x + dx[i] * j;
        ll yp = y + dy[i] * j;

        if (xp < 0 or xp >= n or yp < 0 or yp >= m)
          break;

        if (a[xp][yp] == '@')
          break;
        //                if() break;
        if (v[xp][yp] == 1 and (d + 1) > dp[xp][yp])
          break;

        if (v[xp][yp] == 1)
          continue;

        v[xp][yp] = 1;
        dp[xp][yp] = d + 1;
        q.push({xp, yp});
      }
    }
  }
}

void solve() {
  cin >> n >> m >> k;
  a.resize(n + 1, vector<char>(m + 1));
  cin >> xx1 >> yy1 >> x1 >> x2;

  v.resize(n + 1, vi(m + 1, -1));
  dp.resize(n + 1, vi(m + 1, INT_MAX));

  for (ll i = 0; i < n; i++) {
    for (ll j = 0; j < m; j++)
      cin >> a[i][j];
  }

  bfs(xx1 - 1, yy1 - 1);

  if (v[x1 - 1][x2 - 1] == -1) {
    cout << -1 << endl;
  } else {
    cout << dp[x1 - 1][x2 - 1] << endl;
  }
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  fio int t = 1;
  if (imp)
    cin >> t;
  while (t--) {
    // cout<<"Case #"<<tc<<": ";
    g.clear();
    vis.clear();
    solve();
    // tc++;
  }
}


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;

#define ll long long
#define fio                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define pb push_back
#define vi vector<ll>
#define pp push_pop
#define pll pair<ll, ll>
#define in insert
#define ini(a, n) memset(a, n, sizeof(a))
#define ff first
#define ss second
#define inf 10000000000000000
#define all(p) p.begin(), p.end()
#define que_max priority_queue<int>
#define que_min priority_queue<int, vi, greater<int>>
#define print(a)                                                               \\
  for (auto i : a) {                                                           \\
    cout << i << " ";                                                          \\
  }
#define print1(a)                                                              \\
  for (auto i : a) {                                                           \\
    cout << i.ff << " " << i.ss << endl;                                       \\
  }

typedef tree<pll, null_type, less<pll>, rb_tree_tag,
             tree_order_statistics_node_update>
    Set;
unordered_map<ll, set<pll, greater<pll>>> g;
unordered_map<ll, ll> vis;

const ll mod = 1e9 + 7;

ll power(ll x, ll y, ll p) {
  ll r = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1)
      r = (r * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return r;
}
const ll N = 2e5 + 10;
bool imp = false;
ll n, m, k;
ll xx1, yy1, x1, x2;
vector<vector<char>> a;
vector<vi> v, dp;
ll dx[4] = {-1, +1, 0, 0};
ll dy[4] = {0, 0, +1, -1};

void bfs(ll x, ll y) {
  queue<pll> q;
  q.push({x, y});
  v[x][y] = 1;
  dp[x][y] = 0;

  while (!q.empty()) {
    x = q.front().ff;
    y = q.front().ss;
    ll d = dp[x][y];
    q.pop();

    for (ll i = 0; i < 4; i++) {

      for (ll j = 1; j <= k; j++) {

        ll xp = x + dx[i] * j;
        ll yp = y + dy[i] * j;

        if (xp < 0 or xp >= n or yp < 0 or yp >= m)
          break;

        if (a[xp][yp] == '@')
          break;
        //                if() break;
        if (v[xp][yp] == 1 and (d + 1) > dp[xp][yp])
          break;

        v[xp][yp] = 1;
        dp[xp][yp] = d + 1;
        q.push({xp, yp});
      }
    }
  }
}

void solve() {
  cin >> n >> m >> k;
  a.resize(n + 1, vector<char>(m + 1));
  cin >> xx1 >> yy1 >> x1 >> x2;

  v.resize(n + 1, vi(m + 1, -1));
  dp.resize(n + 1, vi(m + 1, INT_MAX));

  for (ll i = 0; i < n; i++) {
    for (ll j = 0; j < m; j++)
      cin >> a[i][j];
  }

  bfs(xx1 - 1, yy1 - 1);

  if (v[x1 - 1][x2 - 1] == -1) {
    cout << -1 << endl;
  } else {
    cout << dp[x1 - 1][x2 - 1] << endl;
  }
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  fio int t = 1;
  if (imp)
    cin >> t;
  while (t--) {
    // cout<<"Case #"<<tc<<": ";
    g.clear();
    vis.clear();
    solve();
    // tc++;
  }
}