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

#include <bits/stdc++.h>
using namespace std;
#define finish(x) return cout << x << endl, 0
#define ll long long

const int N = 1000001;

int n, m, k;
vector<set<int>> row, col, trow, tcol;
vector<string> c;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int x1, y1, x2, y2;
  cin >> n >> m >> k >> x1 >> y1 >> x2 >> y2;
  c.resize(n);
  for (auto &i : c)
    cin >> i;
  x1--;
  y1--;
  x2--;
  y2--;
  row.resize(n);
  trow.resize(n);
  col.resize(m);
  tcol.resize(m);
  for (int i = 0; i < n; i++) {
    row[i].insert(-1e9);
    row[i].insert(1e9);
    trow[i].insert(-1e9);
    trow[i].insert(1e9);
  }
  for (int i = 0; i < m; i++) {
    col[i].insert(-1e9);
    col[i].insert(1e9);
    tcol[i].insert(-1e9);
    tcol[i].insert(1e9);
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (c[i][j] == '.') {
        row[i].insert(j);
        col[j].insert(i);
      } else {
        trow[i].insert(j);
        tcol[j].insert(i);
      }
    }
  }
  vector<vector<int>> dist(n, vector<int>(m, (int)1e9));
  vector<pair<int, int>> bfs;
  dist[x1][y1] = 0;
  bfs.push_back(make_pair(x1, y1));
  row[x1].erase(y1);
  col[y1].erase(x1);
  for (int i = 0; i < (int)bfs.size(); i++) {
    int x = bfs[i].first, y = bfs[i].second;
    int limit = *trow[x].lower_bound(y);
    while (true) {
      int new_y = *row[x].lower_bound(y);
      if (new_y >= limit)
        break;
      if (new_y - y > k)
        break;
      dist[x][new_y] = dist[x][y] + 1;
      row[x].erase(new_y);
      col[new_y].erase(x);
      bfs.push_back(make_pair(x, new_y));
    }
    limit = *tcol[y].lower_bound(x);
    while (true) {
      int new_x = *col[y].lower_bound(x);
      if (new_x >= limit)
        break;
      if (new_x - x > k)
        break;
      dist[new_x][y] = dist[x][y] + 1;
      row[new_x].erase(y);
      col[y].erase(new_x);
      bfs.push_back(make_pair(new_x, y));
    }
    limit = *prev(trow[x].upper_bound(y));
    while (true) {
      int new_y = *prev(row[x].upper_bound(y));
      if (new_y <= limit)
        break;
      if (y - new_y > k)
        break;
      dist[x][new_y] = dist[x][y] + 1;
      row[x].erase(new_y);
      col[new_y].erase(x);
      bfs.push_back(make_pair(x, new_y));
    }
    limit = *prev(tcol[y].upper_bound(x));
    while (true) {
      int new_x = *prev(col[y].upper_bound(x));
      if (new_x <= limit)
        break;
      if (x - new_x > k)
        break;
      dist[new_x][y] = dist[x][y] + 1;
      row[new_x].erase(y);
      col[y].erase(new_x);
      bfs.push_back(make_pair(new_x, y));
    }
  }
  if (dist[x2][y2] == 1e9)
    dist[x2][y2] = -1;
  cout << dist[x2][y2] << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define finish(x) return cout << x << endl, 0
#define ll long long

const int N = 1000001;

int n, m, k;
pair<int, int> dist[N][4];
vector<string> c;
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};

int idx(int x, int y) { return x * m + y; }
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int x1, y1, x2, y2;
  cin >> n >> m >> k >> x1 >> y1 >> x2 >> y2;
  c.resize(n);
  for (auto &i : c)
    cin >> i;
  x1--;
  y1--;
  x2--;
  y2--;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      for (int r = 0; r < 4; r++) {
        dist[idx(i, j)][r] = make_pair((int)1e9, 0);
      }
    }
  }
  deque<vector<int>> bfs;
  for (int i = 0; i < 4; i++) {
    bfs.push_back({x1, y1, i});
    dist[idx(x1, y1)][i] = make_pair(0, k - 1);
  }
  while (bfs.size()) {
    auto cur = bfs.front();
    bfs.pop_front();
    int x = cur[0], y = cur[1], dir = cur[2];
    for (int r = 0; r < 4; r++) {
      int new_x = x + dx[r], new_y = y + dy[r];
      if (new_x < 0 || new_x >= n || new_y < 0 || new_y >= m)
        continue;
      if (c[new_x][new_y] == '@')
        continue;
      if (r == dir && dist[idx(x, y)][dir].second != k - 1) {
        pair<int, int> new_dist;
        new_dist = dist[idx(x, y)][dir];
        new_dist.second++;
        if (dist[idx(new_x, new_y)][r] < new_dist) {
          continue;
        }
        dist[idx(new_x, new_y)][r] = new_dist;
        bfs.push_front({new_x, new_y, r});
      } else {
        pair<int, int> new_dist;
        new_dist = dist[idx(x, y)][dir];
        new_dist.first++;
        new_dist.second = 0;
        if (dist[idx(new_x, new_y)][r] < new_dist)
          continue;
        dist[idx(new_x, new_y)][r] = new_dist;
        bfs.push_back({new_x, new_y, r});
      }
    }
  }
  int ans = 1e9;
  for (int i = 0; i < 4; i++) {
    ans = min(ans, dist[idx(x2, y2)][i].first);
  }
  if (ans == 1e9)
    ans = -1;
  cout << ans << endl;
}