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

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

vector<pair<int, int>> dir = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int h, w, k;
  cin >> h >> w >> k;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  --x1;
  --y1;
  --x2;
  --y2;
  vector<string> grid(h);
  for (int i = 0; i < h; ++i) {
    cin >> grid[i];
  }
  // do bfs from (x1, y1)
  auto valid = [&](const pair<int, int> &p) {
    return p.first >= 0 && p.first < h && p.second >= 0 && p.second < w;
  };
  vector<vector<int>> dist(h, vector<int>(w, 1e9));
  function<void(pair<int, int>)> bfs = [&](pair<int, int> s) {
    int startX = s.first, startY = s.second;
    dist[s.first][s.second] = 0;
    queue<pair<int, int>> Q;
    Q.emplace(s);
    while (!Q.empty()) {
      int x = Q.front().first;
      int y = Q.front().second;
      Q.pop();
      for (int i = 0; i < 4; i++) {
        for (int j = 1; j <= k; j++) {
          int xx = x + dir[i].first * j;
          int yy = y + dir[i].second * j;
          if (!valid({xx, yy}))
            continue;
          if (grid[xx][yy] == '@' || dist[xx][yy] <= dist[x][y]) {
            break;
          }
          if (dist[xx][yy] == dist[x][y] + 1) {
            continue;
          }
          dist[xx][yy] = dist[x][y] + 1;
          Q.push({xx, yy});
        }
      }
    }
  };
  bfs({x1, y1});
  if (dist[x2][y2] >= 1e9)
    dist[x2][y2] = -1;
  cout << dist[x2][y2] << '\\n';
}


Error: TLE

Fixed submission:

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

vector<pair<int, int>> dir = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int h, w, k;
  cin >> h >> w >> k;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  --x1;
  --y1;
  --x2;
  --y2;
  vector<string> grid(h);
  for (int i = 0; i < h; ++i) {
    cin >> grid[i];
  }
  // do bfs from (x1, y1)
  auto valid = [&](const pair<int, int> &p) {
    return p.first >= 0 && p.first < h && p.second >= 0 && p.second < w;
  };
  auto bfs = [&](pair<int, int> s) {
    queue<pair<int, int>> q;
    vector<vector<bool>> vis(h, vector<bool>(w));
    vector<vector<int>> dist(h, vector<int>(w));
    //		map<pair<int, int>, bool> vis;
    //		map<pair<int, int>, int> dist;
    vector<bool> bad(4);
    q.emplace(s);
    dist[s.first][s.second] = 0;
    vis[s.first][s.second] = 1;
    while (!q.empty()) {
      auto u = q.front();
      q.pop();
      for (int i = 1; i <= k; ++i) {
        for (int j = 0; j < 4; ++j) {
          if (bad[j])
            continue;
          int nr = u.first + i * dir[j].first;
          int nc = u.second + i * dir[j].second;
          if (!valid(make_pair(nr, nc)) || vis[nr][nc])
            continue;
          if (grid[nr][nc] != '.') {
            bad[j] = 1;
            continue;
          }
          vis[nr][nc] = 1;
          dist[nr][nc] = dist[u.first][u.second] + 1;
          if (nr == x2 && nc == y2) {
            return dist[nr][nc];
          }
          q.emplace(make_pair(nr, nc));
        }
      }
      bad.assign(4, 0);
    }
    return -1;
  };
  cout << bfs(make_pair(x1, y1)) << '\\n';
}