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

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#define FOR_LT(i, beg, end) for (int i = (int)(beg); i < (int)(end); i++)
#define FOR_LE(i, beg, end) for (int i = (int)(beg); i <= (int)(end); i++)
#define FOR_DW(i, beg, end) for (int i = (int)(beg); (int)(end) <= i; i--)
#define REP(n) for (int repeat_index = 0; repeat_index < (int)n; repeat_index++)

using namespace std;

struct P {
  int s;
  bool d[4];
};

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout << fixed << setprecision(20);

  int h, w, k;
  cin >> h >> w >> k;

  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  y1--;
  x2--;
  y2--;

  vector<vector<bool>> map(h, vector<bool>(w));
  FOR_LT(x, 0, h) {
    FOR_LT(y, 0, w) {
      char c;
      cin >> c;
      if (c == '@')
        map[x][y] = true;
    }
  }

  pair<int, int> kD[4] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};

  vector<vector<P>> ps(h, vector<P>(w));
  queue<pair<int, int>> search;
  search.push({x1, y1});
  while (!search.empty()) {
    auto c = search.front();
    // cout << c.first << " " << c.second << endl;
    search.pop();

    auto &cp = ps[c.first][c.second];
    if (c.first == x2 && c.second == y2) {
      cout << cp.s << endl;
      return 0;
    }

    FOR_LT(i, 0, 4) {
      // cout << "d : " << i << " " << cp.d[i] << endl;
      if (cp.d[i])
        continue;

      FOR_LE(j, 1, k) {
        int nx = c.first + kD[i].first * j;
        int ny = c.second + kD[i].second * j;
        // cout << "s : " << nx << " " << ny << endl;
        if (nx < 0 || h <= nx || ny < 0 || w <= ny)
          break;
        if (map[nx][ny])
          break;

        if (ps[nx][ny].s != 0 && ps[nx][ny].s < cp.s + 1)
          break;

        if (ps[nx][ny].s == 0) {
          search.push({nx, ny});
          ps[nx][ny].s = cp.s + 1;
        }
        ps[nx][ny].d[i ^ 1] = true;
      }
    }
  }

  cout << -1 << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#define FOR_LT(i, beg, end) for (int i = (int)(beg); i < (int)(end); i++)
#define FOR_LE(i, beg, end) for (int i = (int)(beg); i <= (int)(end); i++)
#define FOR_DW(i, beg, end) for (int i = (int)(beg); (int)(end) <= i; i--)
#define REP(n) for (int repeat_index = 0; repeat_index < (int)n; repeat_index++)

using namespace std;

struct P {
  int s;
  bool d[4];
};

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout << fixed << setprecision(20);

  int h, w, k;
  cin >> h >> w >> k;

  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  y1--;
  x2--;
  y2--;

  vector<vector<bool>> map(h, vector<bool>(w));
  FOR_LT(x, 0, h) {
    FOR_LT(y, 0, w) {
      char c;
      cin >> c;
      if (c == '@')
        map[x][y] = true;
    }
  }

  pair<int, int> kD[4] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};

  vector<vector<P>> ps(h, vector<P>(w));
  queue<pair<int, int>> search;
  search.push({x1, y1});
  while (!search.empty()) {
    auto c = search.front();
    // cout << c.first << " " << c.second << endl;
    search.pop();

    auto &cp = ps[c.first][c.second];
    if (c.first == x2 && c.second == y2) {
      cout << cp.s << endl;
      return 0;
    }

    FOR_LT(i, 0, 4) {
      // cout << "d : " << i << " " << cp.d[i] << endl;
      if (cp.d[i])
        continue;

      FOR_LE(j, 1, k) {
        int nx = c.first + kD[i].first * j;
        int ny = c.second + kD[i].second * j;
        // cout << "s : " << nx << " " << ny << endl;
        if (nx < 0 || h <= nx || ny < 0 || w <= ny)
          break;
        if (map[nx][ny])
          break;

        if (ps[nx][ny].s != 0 && ps[nx][ny].s < cp.s + 1)
          break;

        ps[nx][ny].s = cp.s + 1;
        ps[nx][ny].d[i ^ 1] = true;
        search.push({nx, ny});
      }
    }
  }

  cout << -1 << endl;

  return 0;
}