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

#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define FOR(a, b, c) for (int a = b; a <= c; ++a)
#define FORW(a, b, c) for (int a = b; a >= c; --a)
#define pb push_back
#define SZ(a) ((int)a.size())

const int oo = 1e9;
const int Maxn = 1e5;
const int N = 2e5 + 100;
const double PI = (-2.00) * asin(-1);

typedef pair<int, pair<int, int>> iii;
typedef pair<int, int> ii;

int n, m, k, x1, x2, y11, y2;
vector<vector<int>> check, a, check1;
vector<ii> vi[2];

int solve() {
  int lst = 0, nxt = 1;
  check[x1][y11] = 1;
  vi[lst].pb({x1, y11});
  int cnt = 0;
  while (SZ(vi[lst]) > 0) {
    cnt += 1;
    vi[nxt].clear();
    for (auto v : vi[lst]) {
      check[v.fi][v.se] = 1;
      if (v == ii(x2, y2))
        return cnt - 1;
    }
    for (auto v : vi[lst]) {
      for (int i = v.fi - 1; i >= 1 && v.fi - i <= k; i -= 1) {
        if (a[i][v.se] || check[i][v.se])
          break;
        if (check1[i][v.se])
          continue;
        check1[i][v.se] = 1;
        vi[nxt].pb({i, v.se});
      }
      for (int i = v.fi + 1; i <= n && i - v.fi <= k; i++) {
        if (a[i][v.se] || check[i][v.se])
          break;
        if (check1[i][v.se])
          continue;
        check1[i][v.se] = 1;
        vi[nxt].pb({i, v.se});
      }
      // columns
      for (int i = v.se - 1; i >= 1 && v.se - i <= k; i -= 1) {
        if (a[v.fi][i] || check[v.fi][i])
          break;
        if (check1[v.fi][i])
          continue;
        check1[v.fi][i] = 1;
        vi[nxt].pb({v.fi, i});
      }
      for (int i = v.se + 1; i <= m && i - v.se <= k; i += 1) {
        if (a[v.fi][i] || check[v.fi][i])
          break;
        if (check1[v.fi][i])
          continue;
        check1[v.fi][i] = 1;
        vi[nxt].pb({v.fi, i});
      }
    }
    swap(lst, nxt);
  }
  return -1;
}
signed main() {
#ifdef TEST
  freopen("test.inp", "r", stdin);
#endif // TEST
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  // 0 = up
  // 1 = down
  // 2 = right
  // 3 = left
  cin >> n >> m >> k;
  a.resize(n + 10);
  FOR(i, 1, n) { FOR(j, 1, m + 10) a[i].pb(0); }
  check.resize(n + 10);
  FOR(i, 1, n) FOR(j, 1, m + 10) check[i].pb(0);

  check1.resize(n + 10);
  FOR(i, 1, n) FOR(j, 1, m + 10) check1[i].pb(0);

  cin >> x1 >> y11 >> x2 >> y2;
  FOR(i, 1, n) {
    FOR(j, 1, m) {
      char x;
      cin >> x;
      a[i][j] = (x == '@');
    }
  }
  cout << solve();
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define FOR(a, b, c) for (int a = b; a <= c; ++a)
#define FORW(a, b, c) for (int a = b; a >= c; --a)
#define pb push_back
#define SZ(a) ((int)a.size())

const int oo = 1e9;
const int Maxn = 1e5;
const int N = 2e5 + 100;
const double PI = (-2.00) * asin(-1);

typedef pair<int, pair<int, int>> iii;
typedef pair<int, int> ii;

int n, m, k, x1, x2, y11, y2;
vector<vector<int>> check, a;
vector<ii> vi[2];

int solve() {
  int lst = 0, nxt = 1;
  check[x1][y11] = 1;
  vi[lst].pb({x1, y11});
  int cnt = 0;
  while (SZ(vi[lst]) > 0) {
    cnt += 1;
    vi[nxt].clear();
    for (auto v : vi[lst]) {
      check[v.fi][v.se] = 1;
      if (v == ii(x2, y2))
        return cnt - 1;
    }
    for (auto v : vi[lst]) {
      for (int i = v.fi - 1; i >= 1 && v.fi - i <= k; i -= 1) {
        if (a[i][v.se] || check[i][v.se])
          break;
        vi[nxt].pb({i, v.se});
      }
      for (int i = v.fi + 1; i <= n && i - v.fi <= k; i++) {
        if (a[i][v.se] || check[i][v.se])
          break;
        vi[nxt].pb({i, v.se});
      }
      // columns
      for (int i = v.se - 1; i >= 1 && v.se - i <= k; i -= 1) {
        if (a[v.fi][i] || check[v.fi][i])
          break;
        vi[nxt].pb({v.fi, i});
      }
      for (int i = v.se + 1; i <= m && i - v.se <= k; i += 1) {
        if (a[v.fi][i] || check[v.fi][i])
          break;
        vi[nxt].pb({v.fi, i});
      }
    }
    swap(lst, nxt);
  }
  return -1;
}
signed main() {
#ifdef TEST
  freopen("test.inp", "r", stdin);
#endif // TEST
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  // 0 = up
  // 1 = down
  // 2 = right
  // 3 = left
  cin >> n >> m >> k;
  a.resize(n + 10);
  FOR(i, 1, n) { FOR(j, 1, m + 10) a[i].pb(0); }
  check.resize(n + 10);
  FOR(i, 1, n) FOR(j, 1, m + 10) check[i].pb(0);

  cin >> x1 >> y11 >> x2 >> y2;
  FOR(i, 1, n) {
    FOR(j, 1, m) {
      char x;
      cin >> x;
      a[i][j] = (x == '@');
    }
  }
  cout << solve();
}