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

#include <bits/stdc++.h>
using namespace std;

#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define endl "\\n"

const int N = 1e6 + 5;

int n, m, k;
int x11, y11, x2, y2;
int dx[4] = {-1, +1, 0, 0};
int dy[4] = {0, 0, +1, -1};
vector<vector<char>> a;
vector<vector<int>> vis, ways, dist;
set<int> leftR[N], leftC[N];

void bfs(int x, int y) {
  queue<pair<int, int>> q;
  q.push({x, y});
  vis[x][y] = 1;
  dist[x][y] = 0;
  leftR[y].erase(x);
  leftC[x].erase(y);
  while (!q.empty()) {
    int x = q.front().first;
    int y = q.front().second;
    q.pop();
    {
      auto it = leftC[x].lower_bound(y);
      vector<int> del;
      while (true) {
        if (it == leftC[x].end())
          break;
        int dista = *it - y;
        if (dista > k)
          break;
        if (a[x][*it] == '@')
          break;
        del.push_back(*it);
        int nx = x, ny = *it;
        dist[nx][ny] = dist[x][y] + 1;
        vis[nx][ny] = 1;
        q.push({nx, ny});
        it++;
      }
      for (auto &it : del) {
        leftR[it].erase(x);
        leftC[x].erase(it);
      }
    }
    {
      auto it = leftR[y].lower_bound(x);
      vector<int> del;
      while (true) {
        if (it == leftR[y].end())
          break;
        int dista = *it - x;
        if (dista > k)
          break;
        if (a[*it][y] == '@')
          break;
        del.push_back(*it);
        int nx = *it, ny = y;
        dist[nx][ny] = dist[x][y] + 1;
        vis[nx][ny] = 1;
        q.push({nx, ny});
        it++;
      }
      for (auto &it : del) {
        leftC[it].erase(y);
        leftR[y].erase(it);
      }
    }
    {
      auto it = leftC[x].lower_bound(y);
      vector<int> del;
      while (true) {
        if (it == leftC[x].begin())
          break;
        it--;
        int dista = y - *it;
        if (dista > k)
          break;
        if (a[x][*it] == '@')
          break;
        del.push_back(*it);
        int nx = x, ny = *it;
        dist[nx][ny] = dist[x][y] + 1;
        vis[nx][ny] = 1;
        q.push({nx, ny});
      }
      for (auto &it : del) {
        leftR[it].erase(x);
        leftC[x].erase(it);
      }
    }
    {
      auto it = leftR[y].lower_bound(x);
      vector<int> del;
      while (true) {
        if (it == leftR[y].begin())
          break;
        it--;
        int dista = x - *it;
        if (dista > k)
          break;
        if (a[*it][y] == '@')
          break;
        del.push_back(*it);
        int nx = *it, ny = y;
        dist[nx][ny] = dist[x][y] + 1;
        vis[nx][ny] = 1;
        q.push({nx, ny});
      }
      for (auto &it : del) {
        leftC[it].erase(y);
        leftR[y].erase(it);
      }
    }
  }
}

int32_t main() {
  IOS;
  cin >> n >> m >> k;
  cin >> x11 >> y11 >> x2 >> y2;
  a = vector<vector<char>>(n + 1, vector<char>(m + 1));
  vis = ways = dist = vector<vector<int>>(n + 1, vector<int>(m + 1, 0));
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      leftC[i].insert(j);
      leftR[j].insert(i);
      cin >> a[i][j];
    }
  }
  bfs(x11, y11);
  if (!vis[x2][y2])
    cout << -1;
  else
    cout << dist[x2][y2];
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define endl "\\n"

const int N = 1e5 + 5;

int n, m, k;
int x11, y11, x2, y2;
int dx[4] = {-1, +1, 0, 0};
int dy[4] = {0, 0, +1, -1};
vector<vector<char>> a;
vector<vector<int>> vis, ways, dist;

void bfs(int x, int y) {
  queue<pair<int, int>> q;
  q.push({x, y});
  vis[x][y] = 1;
  dist[x][y] = 0;
  while (!q.empty()) {
    int x = q.front().first;
    int y = q.front().second;
    q.pop();
    for (int dir = 0; dir < 4; dir++) {
      for (int len = 1; len <= k; len++) {
        int nx = x + dx[dir] * len;
        int ny = y + dy[dir] * len;
        if (nx < 1 || ny < 1 || nx > n || ny > m)
          break;
        if (vis[nx][ny] && dist[x][y] + 1 > dist[nx][ny])
          break;
        if (a[nx][ny] == '@')
          break;
        dist[nx][ny] = dist[x][y] + 1;
        vis[nx][ny] = 1;
        q.push({nx, ny});
      }
    }
  }
}

int32_t main() {
  IOS;
  cin >> n >> m >> k;
  cin >> x11 >> y11 >> x2 >> y2;
  a = vector<vector<char>>(n + 1, vector<char>(m + 1));
  vis = ways = dist = vector<vector<int>>(n + 1, vector<int>(m + 1, 0));
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      cin >> a[i][j];
  bfs(x11, y11);
  if (!vis[x2][y2])
    cout << -1;
  else
    cout << dist[x2][y2];
  return 0;
}