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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define all(V) V.begin(), V.end()
const ll MOD = 998244353;

bool chmin(ll &a, ll b) {
  bool x = a > b;
  a = min(a, b);
  return x;
}

int main() {
  ll H, W, K;
  cin >> H >> W >> K;
  ll x1, x2, y1, y2;
  cin >> x1 >> y1;
  cin >> x2 >> y2;
  x1--, x2--, y1--, y2--;
  vector<string> S(H);
  vector<vector<vector<ll>>> M(4, vector<vector<ll>>(H, vector<ll>(W, MOD)));
  for (ll i = 0; i < H; i++) {
    cin >> S[i];
  }
  queue<tuple<ll, ll, ll, ll>> Q;
  Q.push(make_tuple(0, x1, y1, 0));
  Q.push(make_tuple(0, x1, y1, 1));
  Q.push(make_tuple(0, x1, y1, 2));
  Q.push(make_tuple(0, x1, y1, 3));
  ll d, x, y, t;

  M[0][x1][y1] = M[1][x1][y1] = M[2][x1][y1] = M[3][x1][y1] = 0;

  while (!Q.empty()) {
    tie(t, x, y, d) = Q.front();
    Q.pop();
    if (d == 0) {
      for (ll i = 1; i <= K; i++) {
        if (x - i < 0)
          break;
        if (M[0][x - i][y] <= t)
          break;
        if (S[x - i][y] == '@')
          break;

        M[0][x - i][y] = t + 1;
        if (i == K)
          Q.push(make_tuple(t + 1, x - i, y, 0));

        if (chmin(M[1][x - i][y], t + 1)) {
          Q.push(make_tuple(t + 1, x - i, y, 1));
        }
        if (chmin(M[2][x - i][y], t + 1)) {
          Q.push(make_tuple(t + 1, x - i, y, 2));
        }
        if (chmin(M[3][x - i][y], t + 1)) {
          Q.push(make_tuple(t + 1, x - i, y, 3));
        }
      }
    }

    else if (d == 1) {
      for (ll i = 1; i <= K; i++) {
        if (x + i >= H)
          break;
        if (M[1][x + i][y] <= t)
          break;
        if (S[x + i][y] == '@')
          break;

        M[1][x + i][y] = t + 1;
        if (i == K)
          Q.push(make_tuple(t + 1, x + i, y, 1));

        if (chmin(M[0][x + i][y], t + 1)) {
          Q.push(make_tuple(t + 1, x + i, y, 0));
        }
        if (chmin(M[2][x + i][y], t + 1)) {
          Q.push(make_tuple(t + 1, x + i, y, 2));
        }
        if (chmin(M[3][x + i][y], t + 1)) {
          Q.push(make_tuple(t + 1, x + i, y, 3));
        }
      }
    }

    else if (d == 2) {
      for (ll j = 1; j <= K; j++) {
        if (y - j < 0)
          break;
        if (M[2][x][y - j] <= t)
          break;
        if (S[x][y - j] == '@')
          break;

        M[2][x][y - j] = t + 1;
        if (j == K)
          Q.push(make_tuple(t + 1, x, y - j, 2));

        if (chmin(M[1][x][y - j], t + 1)) {
          Q.push(make_tuple(t + 1, x, y - j, 1));
        }
        if (chmin(M[0][x][y - j], t + 1)) {
          Q.push(make_tuple(t + 1, x, y - j, 0));
        }
        if (chmin(M[3][x][y - j], t + 1)) {
          Q.push(make_tuple(t + 1, x, y - j, 3));
        }
      }
    } else {
      for (ll j = 1; j <= K; j++) {
        if (y + j >= W)
          break;
        if (M[3][x][y + j] <= t)
          break;
        if (S[x][y + j] == '@')
          break;

        M[3][x][y + j] = t + 1;
        if (j == K)
          Q.push(make_tuple(t + 1, x, y + j, 3));

        if (chmin(M[1][x][y + j], t + 1)) {
          Q.push(make_tuple(t + 1, x, y + j, 1));
        }
        if (chmin(M[0][x][y + j], t + 1)) {
          Q.push(make_tuple(t + 1, x, y + j, 0));
        }
        if (chmin(M[2][x][y + j], t + 1)) {
          Q.push(make_tuple(t + 1, x, y + j, 2));
        }
      }
    }
  }
  d = min({M[0][x2][y2], M[1][x2][y2], M[2][x2][y2], M[3][x2][y2]});
  if (d >= MOD) {
    cout << -1 << endl;
  } else {
    cout << d << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define all(V) V.begin(), V.end()
const ll MOD = 998244353;

bool chmin(ll &a, ll b) {
  bool x = a > b;
  a = min(a, b);
  return x;
}

int main() {
  ll H, W, K;
  cin >> H >> W >> K;
  ll x1, x2, y1, y2;
  cin >> x1 >> y1;
  cin >> x2 >> y2;
  x1--, x2--, y1--, y2--;
  vector<string> S(H);
  vector<vector<vector<ll>>> M(4, vector<vector<ll>>(H, vector<ll>(W, MOD)));
  for (ll i = 0; i < H; i++) {
    cin >> S[i];
  }
  queue<tuple<ll, ll, ll, ll>> Q;
  Q.push(make_tuple(0, x1, y1, 0));
  Q.push(make_tuple(0, x1, y1, 1));
  Q.push(make_tuple(0, x1, y1, 2));
  Q.push(make_tuple(0, x1, y1, 3));
  ll d, x, y, t;

  M[0][x1][y1] = M[1][x1][y1] = M[2][x1][y1] = M[3][x1][y1] = 0;

  while (!Q.empty()) {
    tie(t, x, y, d) = Q.front();
    Q.pop();
    if (d == 0) {
      for (ll i = 1; i <= K; i++) {
        if (x - i < 0)
          break;
        if (M[0][x - i][y] < t)
          break;
        if (S[x - i][y] == '@')
          break;

        M[0][x - i][y] = t + 1;
        if (i == K)
          Q.push(make_tuple(t + 1, x - i, y, 0));

        if (chmin(M[1][x - i][y], t + 1)) {
          Q.push(make_tuple(t + 1, x - i, y, 1));
        }
        if (chmin(M[2][x - i][y], t + 1)) {
          Q.push(make_tuple(t + 1, x - i, y, 2));
        }
        if (chmin(M[3][x - i][y], t + 1)) {
          Q.push(make_tuple(t + 1, x - i, y, 3));
        }
      }
    }

    else if (d == 1) {
      for (ll i = 1; i <= K; i++) {
        if (x + i >= H)
          break;
        if (M[1][x + i][y] < t)
          break;
        if (S[x + i][y] == '@')
          break;

        M[1][x + i][y] = t + 1;
        if (i == K)
          Q.push(make_tuple(t + 1, x + i, y, 1));

        if (chmin(M[0][x + i][y], t + 1)) {
          Q.push(make_tuple(t + 1, x + i, y, 0));
        }
        if (chmin(M[2][x + i][y], t + 1)) {
          Q.push(make_tuple(t + 1, x + i, y, 2));
        }
        if (chmin(M[3][x + i][y], t + 1)) {
          Q.push(make_tuple(t + 1, x + i, y, 3));
        }
      }
    }

    else if (d == 2) {
      for (ll j = 1; j <= K; j++) {
        if (y - j < 0)
          break;
        if (M[2][x][y - j] < t)
          break;
        if (S[x][y - j] == '@')
          break;

        M[2][x][y - j] = t + 1;
        if (j == K)
          Q.push(make_tuple(t + 1, x, y - j, 2));

        if (chmin(M[1][x][y - j], t + 1)) {
          Q.push(make_tuple(t + 1, x, y - j, 1));
        }
        if (chmin(M[0][x][y - j], t + 1)) {
          Q.push(make_tuple(t + 1, x, y - j, 0));
        }
        if (chmin(M[3][x][y - j], t + 1)) {
          Q.push(make_tuple(t + 1, x, y - j, 3));
        }
      }
    } else {
      for (ll j = 1; j <= K; j++) {
        if (y + j >= W)
          break;
        if (M[3][x][y + j] < t)
          break;
        if (S[x][y + j] == '@')
          break;

        M[3][x][y + j] = t + 1;
        if (j == K)
          Q.push(make_tuple(t + 1, x, y + j, 3));

        if (chmin(M[1][x][y + j], t + 1)) {
          Q.push(make_tuple(t + 1, x, y + j, 1));
        }
        if (chmin(M[0][x][y + j], t + 1)) {
          Q.push(make_tuple(t + 1, x, y + j, 0));
        }
        if (chmin(M[2][x][y + j], t + 1)) {
          Q.push(make_tuple(t + 1, x, y + j, 2));
        }
      }
    }
  }
  d = min({M[0][x2][y2], M[1][x2][y2], M[2][x2][y2], M[3][x2][y2]});
  if (d >= MOD) {
    cout << -1 << endl;
  } else {
    cout << d << endl;
  }
}