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

/* Author : Aaryan Srivastava ^__^ */
#include <bits/stdc++.h>
#include <chrono>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <random>
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repA(i, x, y) for (int i = (x); i <= (y); i++)
#define repD(i, x, y) for (int i = (x); i >= (y); i--)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define setval(a, val) memset(a, val, sizeof(a))
#define Randomize                                                              \\
  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#define trav(x, a) for (auto &x : a)
#define sz(a) ((int)a.size())
typedef long long ll;
#define int ll
using namespace std;

const int N = 3e5 + 5;
const int mod = 1e9 + 7;
const ll inf = 1e18;
const int SZ = 101;
const long double eps = 1e-9;

using namespace __gnu_pbds;
using ordered_set = tree<int, null_type, less<int>, rb_tree_tag,
                         tree_order_statistics_node_update>;

typedef pair<int, int> pii;
typedef pair<int, pii> ipii;
typedef pair<pii, int> piii;
typedef unsigned long long ull;
typedef long double ld;

ll po(ll x, ll y, ll p = mod) {
  ll res = 1;
  x %= p;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}

int h, w, K, x1, Y1, x2, y2;
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
vector<vector<int>> use, dir[4], dis;
vector<vector<int>> vis;
void kek() {
  cout << -1;
  exit(0);
}

void solve() {
  cin >> h >> w >> K >> x1 >> Y1 >> x2 >> y2;
  --x1, --x2, --Y1, --y2;
  use.resize(h, vector<int>(w, 0));
  dis.resize(h, vector<int>(w, inf));
  vis.resize(h, vector<int>(w, false));

  rep(k, 4) dir[k].resize(h, vector<int>(w, 0));
  string s[h];
  rep(i, h) {
    cin >> s[i];
    rep(j, w) use[i][j] = (s[i][j] == '@');
  }

  {//  look to right
   rep(i, h){int lst = w - 1;
  repD(j, w - 1, 0) {
    dir[0][i][j] = (use[i][j] ? -1 : lst);
    if (use[i][j])
      lst = j - 1;
  }
}
}

{// look to left
 rep(i, h){int lst = 0;
rep(j, w) {
  dir[1][i][j] = (use[i][j] ? -1 : lst);
  if (use[i][j])
    lst = j + 1;
}
}
}

{

    // look down
    rep(j, w){int lst = h - 1;
repD(i, h - 1, 0) {
  dir[2][i][j] = (use[i][j] ? -1 : lst);
  if (use[i][j])
    lst = i - 1;
}
}
}

{

  // look up
  rep(j, w) {
    int lst = 0;
    rep(i, h) {
      dir[3][i][j] = (use[i][j] ? -1 : lst);
      if (use[i][j])
        lst = i + 1;
    }
  }
}

if (use[x1][Y1] || use[x2][y2])
  kek();
queue<pii> q;
dis[x1][Y1] = 0;

// rep(k , 4){
//     rep(i , h) rep(j , w) cout << dir[k][i][j] << " \\n"[j == w - 1];
//     cout << '\\n';
// }

q.push({x1, Y1});

while (!q.empty()) {
  auto u = q.front();
  q.pop();
  int x = u.ff, y = u.ss;
  if (vis[x][y] == 2)
    continue;
  vis[x][y] = 2;
  if (use[x][y])
    continue;
  if (x == x2 && y == y2) {
    cout << dis[x][y];
    return;
  }
  // cout << x + 1 << '-' << y + 1 << '\\n';

  rep(k, 4) {
    int lx, rx, ly, ry;
    if (k >= 2)
      ly = ry = y, lx = max({0ll, x - K, dir[3][x][y]}),
      rx = min({h - 1, x + K, dir[2][x][y]});
    else
      lx = rx = x, ly = max({0ll, y - K, dir[1][x][y]}),
      ry = min({w - 1, y + K, dir[0][x][y]});
    // cout << x + 1 << ' ' << y + 1 << ':' << k << ' ' << lx + 1 << ' ' << rx +
    // 1 << ' ' << ly + 1 << ' ' << ry + 1 << '\\n'; continue ;

    repD(i, rx, x) repD(j, ry, y) {
      if (vis[i][j])
        continue;
      vis[i][j] = 1;
      // cout << i + 1 << ' ' << j + 1 << '\\n';
      if (i == x2 && j == y2) {
        cout << dis[x][y] + 1;
        return;
      }

      if (k < 2) {
        dir[1][i][j] = j;
        if (j < ry)
          dir[0][i][j] = j;
      } else {
        dir[3][i][j] = i;
        if (i < rx)
          dir[2][i][j] = i;
      }

      q.push({i, j});
      dis[i][j] = min(dis[i][j], dis[x][y] + 1);
    }

    repA(i, lx, x) repA(j, ly, y) {

      if (vis[i][j])
        continue;
      vis[i][j] = 1;
      // cout << i + 1 << ' ' << j + 1 << '\\n';
      if (i == x2 && j == y2) {
        cout << dis[x][y] + 1;
        return;
      }

      if (k < 2) {
        dir[0][i][j] = j;
        if (j > ly)
          dir[1][i][j] = j;
      } else {
        dir[2][i][j] = i;
        if (i > lx)
          dir[3][i][j] = i;
      }
      q.push({i, j});
      dis[i][j] = min(dis[i][j], dis[x][y] + 1);
    }
  }
}

// rep(i , h) rep(j , w) cout << dis[i][j] << " \\n"[j == w-1];
cout << -1;
}

int32_t main(int32_t argc, char *argv[]) {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int TC = 1, t = 0;
  // cin >> TC ;
  while (t++ < TC) {
    // cout << "Case #" << t << ": " ;
    solve();
    cout << "\\n";
  }
  return 0;
}


Error: TLE

Fixed submission:

/* Author : Aaryan Srivastava ^__^ */
#include <bits/stdc++.h>
#include <chrono>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <random>
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repA(i, x, y) for (int i = (x); i <= (y); i++)
#define repD(i, x, y) for (int i = (x); i >= (y); i--)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define setval(a, val) memset(a, val, sizeof(a))
#define Randomize                                                              \\
  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#define trav(x, a) for (auto &x : a)
#define sz(a) ((int)a.size())
typedef long long ll;
#define int ll
using namespace std;

const int N = 3e5 + 5;
const int mod = 1e9 + 7;
const ll inf = 1e18;
const int SZ = 101;
const long double eps = 1e-9;

using namespace __gnu_pbds;
using ordered_set = tree<int, null_type, less<int>, rb_tree_tag,
                         tree_order_statistics_node_update>;

typedef pair<int, int> pii;
typedef pair<int, pii> ipii;
typedef pair<pii, int> piii;
typedef unsigned long long ull;
typedef long double ld;

ll po(ll x, ll y, ll p = mod) {
  ll res = 1;
  x %= p;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}

int h, w, K, x1, Y1, x2, y2;
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
vector<vector<int>> use, dir[4], dis;
vector<vector<int>> vis;
void kek() {
  cout << -1;
  exit(0);
}

void solve() {
  cin >> h >> w >> K >> x1 >> Y1 >> x2 >> y2;
  --x1, --x2, --Y1, --y2;
  use.resize(h, vector<int>(w, 0));
  dis.resize(h, vector<int>(w, inf));
  vis.resize(h, vector<int>(w, false));

  rep(k, 4) dir[k].resize(h, vector<int>(w, 0));
  string s[h];
  rep(i, h) {
    cin >> s[i];
    rep(j, w) use[i][j] = (s[i][j] == '@');
  }

  {//  look to right
   rep(i, h){int lst = w - 1;
  repD(j, w - 1, 0) {
    dir[0][i][j] = (use[i][j] ? -1 : lst);
    if (use[i][j])
      lst = j - 1;
  }
}
}

{// look to left
 rep(i, h){int lst = 0;
rep(j, w) {
  dir[1][i][j] = (use[i][j] ? -1 : lst);
  if (use[i][j])
    lst = j + 1;
}
}
}

{

    // look down
    rep(j, w){int lst = h - 1;
repD(i, h - 1, 0) {
  dir[2][i][j] = (use[i][j] ? -1 : lst);
  if (use[i][j])
    lst = i - 1;
}
}
}

{

  // look up
  rep(j, w) {
    int lst = 0;
    rep(i, h) {
      dir[3][i][j] = (use[i][j] ? -1 : lst);
      if (use[i][j])
        lst = i + 1;
    }
  }
}

if (use[x1][Y1] || use[x2][y2])
  kek();
queue<pii> q;
dis[x1][Y1] = 0;

// rep(k , 4){
//     rep(i , h) rep(j , w) cout << dir[k][i][j] << " \\n"[j == w - 1];
//     cout << '\\n';
// }

q.push({x1, Y1});

while (!q.empty()) {
  auto u = q.front();
  q.pop();
  int x = u.ff, y = u.ss;
  if (vis[x][y] == 2)
    continue;
  vis[x][y] = 2;
  if (use[x][y])
    continue;
  if (x == x2 && y == y2) {
    cout << dis[x][y];
    return;
  }
  // cout << x + 1 << '-' << y + 1 << '\\n';

  rep(k, 4) {
    int lx, rx, ly, ry;
    if (k >= 2)
      ly = ry = y, lx = max({0ll, x - K, dir[3][x][y]}),
      rx = min({h - 1, x + K, dir[2][x][y]});
    else
      lx = rx = x, ly = max({0ll, y - K, dir[1][x][y]}),
      ry = min({w - 1, y + K, dir[0][x][y]});
    // cout << x + 1 << ' ' << y + 1 << ':' << k << ' ' << lx + 1 << ' ' << rx +
    // 1 << ' ' << ly + 1 << ' ' << ry + 1 << '\\n'; continue ;

    repD(i, rx, x) repD(j, ry, y) {
      if (vis[i][j])
        continue;
      vis[i][j] = 1;
      // cout << i + 1 << ' ' << j + 1 << '\\n';
      if (i == x2 && j == y2) {
        cout << dis[x][y] + 1;
        return;
      }
      if (k < 2) {
        dir[1][i][j] = j;
      } else
        dir[3][i][j] = i;

      q.push({i, j});
      dis[i][j] = min(dis[i][j], dis[x][y] + 1);
    }

    repA(i, lx, x) repA(j, ly, y) {

      if (vis[i][j])
        continue;
      vis[i][j] = 1;
      // cout << i + 1 << ' ' << j + 1 << '\\n';
      if (i == x2 && j == y2) {
        cout << dis[x][y] + 1;
        return;
      }

      if (k < 2) {
        dir[0][i][j] = j;
      } else
        dir[2][i][j] = i;

      q.push({i, j});
      dis[i][j] = min(dis[i][j], dis[x][y] + 1);
    }
  }
}

// rep(i , h) rep(j , w) cout << dis[i][j] << " \\n"[j == w-1];
cout << -1;
}

int32_t main(int32_t argc, char *argv[]) {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int TC = 1, t = 0;
  // cin >> TC ;
  while (t++ < TC) {
    // cout << "Case #" << t << ": " ;
    solve();
    cout << "\\n";
  }
  return 0;
}