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

#include <bits/stdc++.h>

using namespace std;
typedef long long LL;
typedef pair<int, int> pii;

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second

#ifdef OJ
#define debug(x)                                                               \\
  do {                                                                         \\
    cerr << #x << ": " << x << "\\n";                                           \\
  } while (0)
#define debugv(x)                                                              \\
  do {                                                                         \\
    cerr << #x << ": ";                                                        \\
    for (auto &e : (x))                                                        \\
      cerr << e << " ";                                                        \\
    cerr << "\\n";                                                              \\
  } while (0)
#else
#define debug(x)
#define debugv(x)
#endif

#define all(x) (x).begin(), (x).end()
#define rsort(x) sort((x).rbegin(), (x).rend())
#define fori(i, n) for (int i = 0; i < (int)(n); ++i)
#define ford(i, n, e) for (int i = (int)(n); i >= (int)(e); --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)
template <typename T> bool mmax(T &m, const T q) {
  if (m < q) {
    m = q;
    return true;
  } else
    return false;
}
template <typename T> bool mmin(T &m, const T q) {
  if (m > q) {
    m = q;
    return true;
  } else
    return false;
}
#define RETURN(x)                                                              \\
  do {                                                                         \\
    cout << (x) << '\\n';                                                       \\
    return 0;                                                                  \\
  } while (0)

const int MOD = 1e9 + 7;
const int MAXN = 2e5 + 3;
const int INF = 1e9;

int main(int argc, char **argv) {
#ifdef OJ
  freopen("input.txt", "rt", stdin);
  // freopen("output.txt", "wt", stdout);
#endif
  ios::sync_with_stdio(false);
  cin.tie(0);

  int h, w, k;
  cin >> h >> w >> k;
  pii start, end;
  cin >> start.fi >> start.se;
  start.fi--;
  start.se--;
  cin >> end.fi >> end.se;
  end.fi--;
  end.se--;

  vector<string> s(h);
  fori(i, h) cin >> s[i];

  vector<set<int>> row(h), col(w);
  fori(i, h) fori(j, w) {
    row[i].insert(j);
    col[j].insert(i);
  }

  auto valid = [&](int i, int j) {
    return (i >= 0 && j >= 0 && i < h && j < w);
  };

  auto remove = [&](int i, int j) {
    row[i].erase(j);
    col[j].erase(i);
  };

  auto dist = [](int i1, int j1, int i2, int j2) {
    return abs(i1 - i2) + abs(j1 - j2);
  };

  // vector<vector<int>> visited(h, vector<int>(w));
  queue<pair<pii, int>> qu;
  qu.push({start, 0});
  remove(start.fi, start.se);

  vector<int> dx{-1, 1, 0, 0};
  vector<int> dy{0, 0, -1, 1};
  while (qu.size()) {
    auto p = qu.front().fi;
    int d = qu.front().se;
    qu.pop();

    if (p == end)
      RETURN(d);
    int i = p.fi, j = p.se;
    // right
    while (1) {
      auto it = row[i].lower_bound(j);
      if (it == row[i].end() || !valid(i, *it) || s[i][*it] == '@' ||
          dist(i, j, i, *it) > k)
        break;
      qu.push({{i, *it}, d + 1});
      remove(i, *it);
    }

    // left
    while (1) {
      auto it = row[i].lower_bound(j);
      if (it == row[i].begin())
        break;
      --it;
      if (!valid(i, *it) || s[i][*it] == '@' || dist(i, j, i, *it) > k)
        break;
      qu.push({{i, *it}, d + 1});
      remove(i, *it);
    }

    // down
    while (1) {
      auto it = col[j].lower_bound(i);
      if (it == col[j].end() || !valid(*it, j) || s[*it][j] == '@' ||
          dist(i, j, *it, j) > k)
        break;
      qu.push({{*it, j}, d + 1});
      remove(*it, j);
    }

    // up
    while (1) {
      auto it = col[j].lower_bound(i);
      if (it == col[j].begin())
        break;
      --it;
      if (!valid(*it, j) || s[*it][j] == '@' || dist(i, j, *it, j) > k)
        break;
      qu.push({{*it, j}, d + 1});
      remove(*it, j);
    }
  }
  cout << -1;

  // int ntest; cin >> ntest; while(ntest--)
  //{
  // }
  cout << '\\n';
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long LL;
typedef pair<int, int> pii;

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second

#ifdef OJ
#define debug(x)                                                               \\
  do {                                                                         \\
    cerr << #x << ": " << x << "\\n";                                           \\
  } while (0)
#define debugv(x)                                                              \\
  do {                                                                         \\
    cerr << #x << ": ";                                                        \\
    for (auto &e : (x))                                                        \\
      cerr << e << " ";                                                        \\
    cerr << "\\n";                                                              \\
  } while (0)
#else
#define debug(x)
#define debugv(x)
#endif

#define all(x) (x).begin(), (x).end()
#define rsort(x) sort((x).rbegin(), (x).rend())
#define fori(i, n) for (int i = 0; i < (int)(n); ++i)
#define ford(i, n, e) for (int i = (int)(n); i >= (int)(e); --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)
template <typename T> bool mmax(T &m, const T q) {
  if (m < q) {
    m = q;
    return true;
  } else
    return false;
}
template <typename T> bool mmin(T &m, const T q) {
  if (m > q) {
    m = q;
    return true;
  } else
    return false;
}
#define RETURN(x)                                                              \\
  do {                                                                         \\
    cout << (x) << '\\n';                                                       \\
    return 0;                                                                  \\
  } while (0)

const int MOD = 1e9 + 7;
const int MAXN = 2e5 + 3;
const int INF = 1e9;

int main(int argc, char **argv) {
#ifdef OJ
  freopen("input.txt", "rt", stdin);
  // freopen("output.txt", "wt", stdout);
#endif
  ios::sync_with_stdio(false);
  cin.tie(0);

  int h, w, k;
  cin >> h >> w >> k;
  pii start, end;
  cin >> start.fi >> start.se;
  start.fi--;
  start.se--;
  cin >> end.fi >> end.se;
  end.fi--;
  end.se--;

  vector<string> s(h);
  fori(i, h) cin >> s[i];

  vector<vector<int>> visited(h, vector<int>(w));
  queue<pair<pii, int>> qu;
  visited[start.fi][start.se] = 1;
  qu.push({start, 0});

  vector<int> dx{-1, 1, 0, 0};
  vector<int> dy{0, 0, -1, 1};
  while (qu.size()) {
    auto p = qu.front().fi;
    int d = qu.front().se;
    qu.pop();

    if (p == end)
      RETURN(d);
    fori(i, 4) {
      for (int t = 1; t <= k; t++) {
        int nx = p.fi + t * dx[i];
        int ny = p.se + t * dy[i];
        if (nx < 0 || ny < 0 || nx >= h || ny >= w || s[nx][ny] == '@')
          break;
        if (!visited[nx][ny]) {
          visited[nx][ny] = 1;
          qu.push({{nx, ny}, d + 1});
        }
      }
    }
  }
  cout << -1;

  // int ntest; cin >> ntest; while(ntest--)
  //{
  // }
  cout << '\\n';
  return 0;
}