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

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

#define mod 1'000'000'007
typedef long long ll;

template <typename T> void deb(initializer_list<T> l) {
  for (auto &e : l)
    cout << e << ' ';
  cout << endl;
}

int n, m, k;

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

bool ok(int i, int j) { return i >= 0 && j >= 0 && i < n && j < m; }

void solve() {
  cin >> n >> m >> k;
  k = min(k, max(n - 1, m - 1));
  vector<vector<bool>> a(n, vector<bool>(m));
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  --x1;
  --y1;
  --x2;
  --y2;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      char c;
      cin >> c;
      a[i][j] = c == '.';
    }
  }

  vector<vector<int>> dist(n, vector<int>(m, -1));
  vector<vector<bool>> vis(n, vector<bool>(m, false));
  vis[x1][y1] = true;
  dist[x1][y1] = 0;
  queue<pair<int, int>> q;
  q.push({x1, y1});

  while (!q.empty()) {
    auto [i, j] = q.front();
    q.pop();
    // deb({i, j, dist[i][j]});

    for (auto &[di, dj] : dir) {
      int x = i;
      int y = j;
      for (int o = 1; o <= k; ++o) {
        x += di;
        y += dj;
        if (!ok(x, y))
          break;
        if (!a[x][y])
          break;
        if (vis[x][y] && dist[x][y] <= dist[i][j])
          break;
        if (vis[x][y])
          continue;

        vis[x][y] = true;
        dist[x][y] = dist[i][j] + 1;
        q.push({x, y});
      }
    }
  }

  // vector<pair<int, int>> x{{2, 1}, {2, 0}, {1, }}
  cout << dist[x2][y2] << '\\n';
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  int t = 1;
  // cin >> t;
  while (t--)
    solve();

  return 0;
}


Error: TLE

Fixed submission:

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

#define mod 1'000'000'007
typedef long long ll;

template <typename T> void deb(initializer_list<T> l) {
  for (auto &e : l)
    cout << e << ' ';
  cout << endl;
}

int n, m, k;

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

bool ok(int i, int j) { return i >= 0 && j >= 0 && i < n && j < m; }

void solve() {
  cin >> n >> m >> k;
  k = min(k, max(n - 1, m - 1));
  vector<vector<bool>> a(n, vector<bool>(m));
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  --x1;
  --y1;
  --x2;
  --y2;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      char c;
      cin >> c;
      a[i][j] = c == '.';
    }
  }

  vector<vector<int>> dist(n, vector<int>(m, -1));
  vector<vector<bool>> vis(n, vector<bool>(m, false));
  vis[x1][y1] = true;
  dist[x1][y1] = 0;
  queue<pair<int, int>> q;
  q.push({x1, y1});

  while (!q.empty()) {
    auto [i, j] = q.front();
    q.pop();
    // deb({i, j, dist[i][j]});

    for (auto &[di, dj] : dir) {
      int x = i;
      int y = j;
      for (int o = 1; o <= k; ++o) {
        x += di;
        y += dj;
        if (!ok(x, y))
          break;
        if (!a[x][y])
          break;
        if (vis[x][y])
          continue;

        vis[x][y] = true;
        dist[x][y] = dist[i][j] + 1;
        q.push({x, y});
      }
    }
  }

  // vector<pair<int, int>> x{{2, 1}, {2, 0}, {1, }}
  cout << dist[x2][y2] << '\\n';
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  int t = 1;
  // cin >> t;
  while (t--)
    solve();

  return 0;
}