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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const ll MOD = 1e9 + 7;
const ll INF = 1e18;
#define rep(i, m, n) for (ll i = (m); i <= (n); i++)
#define zep(i, m, n) for (ll i = (m); i < (n); i++)
#define rrep(i, m, n) for (ll i = (m); i >= (n); i--)
#define print(x) cout << (x) << endl;
#define printa(x, m, n)                                                        \\
  for (int i = (m); i <= n; i++) {                                             \\
    cout << (x[i]) << " ";                                                     \\
  }                                                                            \\
  cout << endl;

vector<int> dx = {-1, 1, 0, 0};
vector<int> dy = {0, 0, -1, 1};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll h, w, k;
  cin >> h >> w >> k;
  int sx, sy, gx, gy;
  cin >> sx >> sy >> gx >> gy;
  sx--;
  sy--;
  gx--;
  gy--;
  char c[h][w];
  zep(i, 0, h) zep(j, 0, w) cin >> c[i][j];

  int ans[h][w][2];
  memset(ans, -1, sizeof(ans));
  ans[sx][sy][0] = 0;
  ans[sx][sy][1] = 0;

  // int rem[h][w][2];
  // rem[sx][sy][0] = k;
  // rem[sx][sy][1] = k;

  bool use[h][w][2];
  memset(use, 0, sizeof(use));
  use[sx][sy][0] = true;
  use[sx][sy][1] = true;

  queue<vector<int>> q;
  q.push({sx, sy, 0});
  q.push({sx, sy, 1});

  while (!q.empty()) {
    auto v = q.front();
    q.pop();
    int x = v[0], y = v[1], d = v[2];

    zep(i, 0, 4) {
      if ((d == 0 && i >= 2) || (d == 1 && i <= 1)) {
        rep(j, 1, k) {
          int nx = x + j * dx[i];
          int ny = y + j * dy[i];

          if (0 <= nx && nx < h && 0 <= ny && ny < w) {
            if (c[nx][ny] == '@')
              break;

            if (!use[nx][ny][1 - d]) {
              use[nx][ny][1 - d] = true;
              ans[nx][ny][1 - d] = ans[x][y][d] + 1;
              // rem[nx][ny][1 - d] = k - j;
              q.push({nx, ny, 1 - d});
            } else {
              if (ans[nx][ny][1 - d] < ans[x][y][d] + 1)
                break;
            }

            if (j == k && !use[nx][ny][d]) {
              use[nx][ny][d] = true;
              ans[nx][ny][d] = ans[x][y][d] + 1;
              // rem[nx][ny][d] = k;
              q.push({nx, ny, d});
            }
          }
        }
      }
    }
  }

  // zep(i, 0, h){zep(j, 0, w){cout << ans[i][j][0] << " ";}cout << endl;}
  // cout << endl;
  // zep(i, 0, h){zep(j, 0, w){cout << ans[i][j][1] << " ";}cout << endl;}

  int res = h * w + 100;
  if (use[gx][gy][0]) {
    res = min(res, ans[gx][gy][0]);
  }
  if (use[gx][gy][1]) {
    res = min(res, ans[gx][gy][1]);
  }
  print((res == h * w + 100) ? -1 : res)

      return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const ll MOD = 1e9 + 7;
const ll INF = 1e18;
#define rep(i, m, n) for (ll i = (m); i <= (n); i++)
#define zep(i, m, n) for (ll i = (m); i < (n); i++)
#define rrep(i, m, n) for (ll i = (m); i >= (n); i--)
#define print(x) cout << (x) << endl;
#define printa(x, m, n)                                                        \\
  for (int i = (m); i <= n; i++) {                                             \\
    cout << (x[i]) << " ";                                                     \\
  }                                                                            \\
  cout << endl;

vector<ll> dx = {-1, 1, 0, 0};
vector<ll> dy = {0, 0, -1, 1};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll h, w, k;
  cin >> h >> w >> k;
  ll sx, sy, gx, gy;
  cin >> sx >> sy >> gx >> gy;
  sx--;
  sy--;
  gx--;
  gy--;
  char c[h][w];
  zep(i, 0, h) zep(j, 0, w) cin >> c[i][j];

  int ans[h][w][2];
  memset(ans, -1, sizeof(ans));
  ans[sx][sy][0] = 0;
  ans[sx][sy][1] = 0;

  bool use[h][w][2];
  memset(use, 0, sizeof(use));
  use[sx][sy][0] = true;
  use[sx][sy][1] = true;

  queue<vector<ll>> q;
  q.push({sx, sy, 0});
  q.push({sx, sy, 1});

  while (!q.empty()) {
    auto v = q.front();
    q.pop();
    ll x = v[0], y = v[1], d = v[2];

    zep(i, 0, 4) {
      if ((d == 0 && i >= 2) || (d == 1 && i <= 1)) {
        rep(j, 1, k) {
          ll nx = x + j * dx[i];
          ll ny = y + j * dy[i];

          if (0 <= nx && nx < h && 0 <= ny && ny < w) {
            if (c[nx][ny] == '@')
              break;

            if (!use[nx][ny][1 - d]) {
              use[nx][ny][1 - d] = true;
              ans[nx][ny][1 - d] = ans[x][y][d] + 1;
              q.push({nx, ny, 1 - d});
            }

            if (j == k && !use[nx][ny][d]) {
              use[nx][ny][d] = true;
              ans[nx][ny][d] = ans[x][y][d] + 1;
              q.push({nx, ny, d});
            }
          }
        }
      }
    }
  }

  // zep(i, 0, h){zep(j, 0, w){cout << ans[i][j][0] << " ";}cout << endl;}
  // cout << endl;
  // zep(i, 0, h){zep(j, 0, w){cout << ans[i][j][1] << " ";}cout << endl;}

  int res = h * w + 1;
  if (use[gx][gy][0]) {
    res = min(res, ans[gx][gy][0]);
  }
  if (use[gx][gy][1]) {
    res = min(res, ans[gx][gy][1]);
  }
  print((res == h * w + 1) ? -1 : res)

      return 0;
}