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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T> using vec = vector<T>;
template <class T> using vvec = vector<vec<T>>;

using P = pair<int, int>;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int H, W, K;
  cin >> H >> W >> K;
  vvec<char> C(H, vec<char>(W));
  int sx, sy, gx, gy;
  cin >> sx >> sy >> gx >> gy;
  sx--;
  sy--;
  gx--;
  gy--;
  vec<set<int>> row(H), col(W);
  for (int i = 0; i < H; i++)
    for (int j = 0; j < W; j++) {
      cin >> C[i][j];
      row[i].insert(j);
      col[j].insert(i);
    }
  for (int i = 0; i < H; i++) {
    row[i].insert(-1);
    row[i].insert(W);
  }
  for (int i = 0; i < W; i++) {
    col[i].insert(-1);
    col[i].insert(H);
  }
  int inf = 1e9;
  vvec<int> dp(H, vec<int>(W, inf));
  dp[sx][sy] = 0;
  row[sx].erase(sy);
  col[sy].erase(sx);
  queue<P> Q;
  Q.push({sx, sy});
  auto in = [&](int x, int y) { return 0 <= x && x < H && 0 <= y && y < W; };

  while (!Q.empty()) {
    auto [x, y] = Q.front();
    Q.pop();
    //        cerr << x << " " << y << "\\n";
    int d = dp[x][y];
    // right
    auto it = row[x].upper_bound(y);
    while (it != row[x].end()) {
      int ny = *it;
      if (!in(x, ny))
        break;
      if (C[x][ny] == '@')
        break;
      if (abs(y - ny) > K)
        break;
      if (dp[x][ny] == inf) {
        dp[x][ny] = d + 1;
        Q.push({x, ny});
        col[ny].erase(x);
      }
      it = row[x].erase(it);
    }
    // left
    it--;
    while (it != row[x].end()) {
      int ny = *it;
      if (!in(x, ny))
        break;
      if (C[x][ny] == '@')
        break;
      if (abs(y - ny) > K)
        break;
      if (dp[x][ny] == inf) {
        dp[x][ny] = d + 1;
        Q.push({x, ny});
        col[ny].erase(x);
      }
      it = row[x].erase(it);
      it--;
    }

    // down
    it = col[y].upper_bound(x);
    while (it != row[y].end()) {
      int nx = *it;
      if (!in(nx, y))
        break;
      if (C[nx][y] == '@')
        break;
      if (abs(x - nx) > K)
        break;
      if (dp[nx][y] == inf) {
        dp[nx][y] = d + 1;
        Q.push({nx, y});
        row[nx].erase(y);
      }
      it = col[y].erase(it);
    }
    // down
    it--;
    while (it != row[y].end()) {
      int nx = *it;
      if (!in(nx, y))
        break;
      if (C[nx][y] == '@')
        break;
      if (abs(x - nx) > K)
        break;
      if (dp[nx][y] == inf) {
        dp[nx][y] = d + 1;
        Q.push({nx, y});
        row[nx].erase(y);
      }
      it = col[y].erase(it);
      it--;
    }
  }

  cout << (dp[gx][gy] != inf ? dp[gx][gy] : -1) << "\\n";
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T> using vec = vector<T>;
template <class T> using vvec = vector<vec<T>>;

using P = pair<int, int>;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int H, W, K;
  cin >> H >> W >> K;
  vvec<char> C(H, vec<char>(W));
  int sx, sy, gx, gy;
  cin >> sx >> sy >> gx >> gy;
  sx--;
  sy--;
  gx--;
  gy--;
  vec<set<int>> row(H), col(W);
  for (int i = 0; i < H; i++)
    for (int j = 0; j < W; j++) {
      cin >> C[i][j];
      row[i].insert(j);
      col[j].insert(i);
    }
  for (int i = 0; i < H; i++) {
    row[i].insert(-1);
    row[i].insert(W);
  }
  for (int i = 0; i < W; i++) {
    col[i].insert(-1);
    col[i].insert(H);
  }
  int inf = 1e9;
  vvec<int> dp(H, vec<int>(W, inf));
  dp[sx][sy] = 0;
  row[sx].erase(sy);
  col[sy].erase(sx);
  queue<P> Q;
  Q.push({sx, sy});
  auto in = [&](int x, int y) { return 0 <= x && x < H && 0 <= y && y < W; };

  while (!Q.empty()) {
    auto [x, y] = Q.front();
    Q.pop();
    //        cerr << x << " " << y << "\\n";
    int d = dp[x][y];
    // right
    auto it = row[x].upper_bound(y);
    while (it != row[x].end()) {
      int ny = *it;
      if (!in(x, ny))
        break;
      if (C[x][ny] == '@')
        break;
      if (abs(y - ny) > K)
        break;
      if (dp[x][ny] == inf) {
        dp[x][ny] = d + 1;
        Q.push({x, ny});
        col[ny].erase(x);
      }
      it = row[x].erase(it);
    }
    // left
    it--;
    while (it != row[x].end()) {
      int ny = *it;
      if (!in(x, ny))
        break;
      if (C[x][ny] == '@')
        break;
      if (abs(y - ny) > K)
        break;
      if (dp[x][ny] == inf) {
        dp[x][ny] = d + 1;
        Q.push({x, ny});
        col[ny].erase(x);
      }
      it = row[x].erase(it);
      it--;
    }

    // down
    it = col[y].upper_bound(x);
    while (it != row[y].end()) {
      int nx = *it;
      if (!in(nx, y))
        break;
      if (C[nx][y] == '@')
        break;
      if (abs(x - nx) > K)
        break;
      if (dp[nx][y] == inf) {
        dp[nx][y] = d + 1;
        Q.push({nx, y});
        row[nx].erase(y);
      }
      it = col[y].erase(it);
    }
    // down
    it--;
    while (it != row[y].end()) {
      int nx = *it;
      if (!in(nx, y))
        break;
      if (C[nx][y] == '@')
        break;
      if (abs(x - nx) > K)
        break;
      if (dp[nx][y] == inf) {
        dp[nx][y] = d + 1;
        Q.push({nx, y});
        row[nx].erase(y);
      }
      it = col[y].erase(it);
      it--;
    }
  }

  for (int i = 0; i < H; i++)
    for (int j = 0; j < W; j++)
      cerr << dp[i][j] << (j != W - 1 ? " " : "\\n");
  cout << (dp[gx][gy] != inf ? dp[gx][gy] : -1) << "\\n";
}