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

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

#define lli long long int
#define REP(i, s, n) for (int i = s; i < n; i++)
#define NUM 2520
#define INF (1LL << 50)
#define DEBUG 0
#define mp(a, b) make_pair(a, b)
#define SORT(V) sort(V.begin(), V.end())
#define PI (3.141592653589794)
#define TO_STRING(VariableName) #VariableName
#define LOG(x)                                                                 \\
  if (DEBUG)                                                                   \\
    cout << TO_STRING(x) << "=" << x << " " << endl;
#define LOG2(x, y)                                                             \\
  if (DEBUG)                                                                   \\
    cout << TO_STRING(x) << "=" << x << " " << TO_STRING(y) << "=" << y << endl;
#define LOG3(x, y, z)                                                          \\
  if (DEBUG)                                                                   \\
    cout << TO_STRING(x) << "=" << x << " " << TO_STRING(y) << "=" << y << " " \\
         << TO_STRING(z) << "=" << z << endl;
#define LOG4(w, x, y, z)                                                       \\
  if (DEBUG)                                                                   \\
    cout << TO_STRING(w) << "=" << w << " " << TO_STRING(x) << "=" << x << " " \\
         << TO_STRING(y) << "=" << y << " " << TO_STRING(z) << "=" << z        \\
         << endl;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {

  lli h, w, k;
  cin >> h >> w >> k;

  lli sx, sy, ex, ey;
  cin >> sy >> sx >> ey >> ex;
  sx--, sy--, ex--, ey--;

  vector<vector<lli>> d(h, vector<lli>(w));
  vector<vector<lli>> dd(h, vector<lli>(w));

  REP(i, 0, h) REP(j, 0, w) {
    char tmp;
    cin >> tmp;
    if (tmp == '@')
      d[i][j] = 1;
    else
      d[i][j] = 0;
    dd[i][j] = -1;
  }

  queue<lli> qx, qy;
  qx.push(sx);
  qy.push(sy);
  dd[sy][sx] = 0;

  while (qx.size()) {
    lli topX = qx.front();
    lli topY = qy.front();
    LOG3(topX, topY, dd[topY][topX]);
    qx.pop(), qy.pop();

    lli dx[4] = {-1, 0, 1, 0};
    lli dy[4] = {0, 1, 0, -1};
    REP(i, 0, 4) {
      REP(K, 1, k + 1) {
        lli nx = topX + dx[i] * K;
        lli ny = topY + dy[i] * K;
        LOG2(nx, ny);
        if (nx < 0 || w <= nx || ny < 0 || h <= ny)
          break;
        if (d[ny][nx] == 1)
          break;
        if (dd[ny][nx] != -1 && dd[ny][nx] < dd[topY][topX] + 1)
          break;
        if (dd[ny][nx] != -1)
          continue;
        qx.push(nx);
        qy.push(ny);
        dd[ny][nx] = dd[topY][topX] + 1;
      }
    }
  }
  LOG2(ey, ex);
  if (dd[ey][ex] == -1) {
    cout << -1 << endl;
  } else {
    cout << dd[ey][ex] << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

#define lli long long int
#define REP(i, s, n) for (int i = s; i < n; i++)
#define NUM 2520
#define INF (1LL << 50)
#define DEBUG 0
#define mp(a, b) make_pair(a, b)
#define SORT(V) sort(V.begin(), V.end())
#define PI (3.141592653589794)
#define TO_STRING(VariableName) #VariableName
#define LOG(x)                                                                 \\
  if (DEBUG)                                                                   \\
    cout << TO_STRING(x) << "=" << x << " " << endl;
#define LOG2(x, y)                                                             \\
  if (DEBUG)                                                                   \\
    cout << TO_STRING(x) << "=" << x << " " << TO_STRING(y) << "=" << y << endl;
#define LOG3(x, y, z)                                                          \\
  if (DEBUG)                                                                   \\
    cout << TO_STRING(x) << "=" << x << " " << TO_STRING(y) << "=" << y << " " \\
         << TO_STRING(z) << "=" << z << endl;
#define LOG4(w, x, y, z)                                                       \\
  if (DEBUG)                                                                   \\
    cout << TO_STRING(w) << "=" << w << " " << TO_STRING(x) << "=" << x << " " \\
         << TO_STRING(y) << "=" << y << " " << TO_STRING(z) << "=" << z        \\
         << endl;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {

  lli h, w, k;
  cin >> h >> w >> k;

  lli sx, sy, ex, ey;
  cin >> sy >> sx >> ey >> ex;
  sx--, sy--, ex--, ey--;

  vector<vector<lli>> d(h, vector<lli>(w));
  vector<vector<lli>> dd(h, vector<lli>(w));

  REP(i, 0, h) REP(j, 0, w) {
    char tmp;
    cin >> tmp;
    if (tmp == '@')
      d[i][j] = 1;
    else
      d[i][j] = 0;
    dd[i][j] = -1;
  }

  queue<lli> qx, qy;
  qx.push(sx);
  qy.push(sy);
  dd[sy][sx] = 0;

  while (qx.size()) {
    lli topX = qx.front();
    lli topY = qy.front();
    LOG3(topX, topY, dd[topY][topX]);
    qx.pop(), qy.pop();

    lli dx[4] = {-1, 0, 1, 0};
    lli dy[4] = {0, 1, 0, -1};
    REP(i, 0, 4) {
      REP(K, 1, k + 1) {
        lli nx = topX + dx[i] * K;
        lli ny = topY + dy[i] * K;
        LOG2(nx, ny);
        if (nx < 0 || w <= nx || ny < 0 || h <= ny)
          break;
        if (d[ny][nx] == 1)
          break;
        if (dd[ny][nx] != -1 && dd[ny][nx] < dd[topY][topX] + 1)
          break;
        qx.push(nx);
        qy.push(ny);
        dd[ny][nx] = dd[topY][topX] + 1;
      }
    }
  }
  LOG2(ey, ex);
  if (dd[ey][ex] == -1) {
    cout << -1 << endl;
  } else {
    cout << dd[ey][ex] << endl;
  }

  return 0;
}