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

/*** author: yuji9511 ***/
#include <bits/stdc++.h>
using namespace std;
// using int = long long;
// using lpair = pair<ll, ll>;
const int MOD = 1e9 + 7;
#define rep(i, m, n) for (int i = (m); i < (n); i++)
#define rrep(i, m, n) for (int i = (m); i >= (n); i--)
#define printa(x, n)                                                           \\
  for (int i = 0; i < n; i++) {                                                \\
    cout << (x[i]) << " \\n"[i == n - 1];                                       \\
  };
void print() {}
template <class H, class... T> void print(H &&h, T &&...t) {
  cout << h << " \\n"[sizeof...(t) == 0];
  print(forward<T>(t)...);
}
string S[1000010];
bool visit2[1000010] = {};
int dist[1000010] = {};
typedef struct {
  int x;
  int y;
  int d;
  int mode;
} P;

void solve() {
  int H, W, K;
  cin >> H >> W >> K;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  y1--;
  x2--;
  y2--;
  rep(i, 0, H) cin >> S[i];
  queue<P> que;
  que.push({x1, y1, 0, -1});
  int dx[4] = {0, 0, 1, -1};
  int dy[4] = {1, -1, 0, 0};
  bool reach = false;
  rep(i, 0, H * W) { dist[i] = 1e9; }

  while (not que.empty()) {
    P l1 = que.front();
    que.pop();
    int lx = l1.x, ly = l1.y;
    if (visit2[lx * W + ly])
      continue;
    visit2[lx * W + ly] = true;
    if (lx == x2 && ly == y2) {
      reach = true;
      dist[lx * W + ly] = l1.d;
      break;
    }
    rep(k, 0, 4) {
      if (l1.mode == k)
        continue;
      rep(d, 1, K + 1) {
        int xx = lx + dx[k] * d;
        int yy = ly + dy[k] * d;
        if (xx >= 0 && xx < H && yy >= 0 && yy < W &&
            visit2[xx * W + yy] == false && S[xx][yy] == '.') {
          if (d == K) {
            que.push({xx, yy, l1.d + 1, -1});
          } else {
            que.push({xx, yy, l1.d + 1, k});
          }
        } else {
          break;
        }
      }
    }
  }
  if (not reach) {
    print(-1);
    return;
  } else {
    print(dist[x2 * W + y2]);
  }
}

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


Error: TLE

Fixed submission:

/*** author: yuji9511 ***/
#include <bits/stdc++.h>
using namespace std;
// using int = long long;
// using lpair = pair<ll, ll>;
const int MOD = 1e9 + 7;
#define rep(i, m, n) for (int i = (m); i < (n); i++)
#define rrep(i, m, n) for (int i = (m); i >= (n); i--)
#define printa(x, n)                                                           \\
  for (int i = 0; i < n; i++) {                                                \\
    cout << (x[i]) << " \\n"[i == n - 1];                                       \\
  };
void print() {}
template <class H, class... T> void print(H &&h, T &&...t) {
  cout << h << " \\n"[sizeof...(t) == 0];
  print(forward<T>(t)...);
}
string S[1000010];
bool visit2[1000010] = {};
int dist[1000010] = {};
typedef struct {
  int x;
  int y;
  int d;
} P;

void solve() {
  int H, W, K;
  cin >> H >> W >> K;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  y1--;
  x2--;
  y2--;
  rep(i, 0, H) cin >> S[i];
  queue<P> que;
  que.push({x1, y1, 0});
  int dx[4] = {0, 0, 1, -1};
  int dy[4] = {1, -1, 0, 0};
  bool reach = false;
  rep(i, 0, H * W) { dist[i] = 1e9; }

  while (not que.empty()) {
    P l1 = que.front();
    que.pop();
    int lx = l1.x, ly = l1.y;
    if (visit2[lx * W + ly])
      continue;
    visit2[lx * W + ly] = true;
    if (lx == x2 && ly == y2) {
      reach = true;
      dist[lx * W + ly] = l1.d;
      break;
    }
    rep(k, 0, 4) {
      rep(d, 1, K + 1) {
        int xx = lx + dx[k] * d;
        int yy = ly + dy[k] * d;
        if (xx >= 0 && xx < H && yy >= 0 && yy < W &&
            visit2[xx * W + yy] == false && S[xx][yy] == '.') {
          que.push({xx, yy, l1.d + 1});
        } else {
          break;
        }
      }
    }
  }
  if (not reach) {
    print(-1);
    return;
  } else {
    print(dist[x2 * W + y2]);
  }
}

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