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

#include <bits/stdc++.h>
using namespace std;
#define deb(x) cout << #x << " = " << x << "\\n";

signed main() {
  // freopen("in.txt", "r",stdin);
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  int n, m, k;
  cin >> n >> m >> k;

  int a, b, c, d;
  cin >> a >> b >> c >> d;
  a--, b--, c--, d--;

  string s[n];
  for (int i = 0; i < n; i++) {
    cin >> s[i];
  }
  vector<vector<bool>> vis(n, vector<bool>(m, false));
  vector<vector<bool>> enq(n, vector<bool>(m, false));
  queue<vector<int>> q; //{ x cord, y cord, dis}
  q.push({a, b, 0});
  enq[a][b] = true;
  int max_stroke;

  while (!q.empty()) {
    int x = q.front()[0], y = q.front()[1];
    int dis = q.front()[2];
    q.pop();
    if (x == c && y == d) {
      cout << dis << "\\n";
      return 0;
    }
    if (vis[x][y])
      continue;
    vis[x][y] = true;

    max_stroke = x;
    for (int i = 1; i <= k; i++) {
      if (x - i >= 0 && s[x - i][y] != '@' && vis[x - i][y] == false)
        max_stroke = x - i;
      else
        break;
    }
    for (int i = max_stroke; i < x; i++) {
      if (!enq[i][y])
        q.push({i, y, dis + 1}), enq[i][y] = true;
    }
    max_stroke = x;
    for (int i = 1; i <= k; i++) {
      if (x + i < n && s[x + i][y] != '@' && vis[x + i][y] == false)
        max_stroke = x + i;
      else
        break;
    }
    for (int i = max_stroke; i > x; i--) {
      if (!enq[i][y])
        q.push({i, y, dis + 1}), enq[i][y] = true;
    }
    max_stroke = y;
    for (int i = 1; i <= k; i++) {
      if (y - i >= 0 && s[x][y - i] != '@' && vis[x][y - i] == false)
        max_stroke = y - i;
      else
        break;
    }
    for (int i = max_stroke; i < y; i++) {
      if (!enq[x][i])
        q.push({x, i, dis + 1}), enq[x][i] = true;
    }
    max_stroke = y;
    for (int i = 1; i <= k; i++) {
      if (y + i < m && s[x][y + i] != '@' && vis[x][y + i] == false)
        max_stroke = y + i;
      else
        break;
    }
    for (int i = max_stroke; i > y; i--) {
      if (!enq[x][i])
        q.push({x, i, dis + 1}), enq[x][i] = true;
    }
  }
  cout << "-1\\n";
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define deb(x) cout << #x << " = " << x << "\\n";

signed main() {
  // freopen("in.txt", "r",stdin);
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  int n, m, k;
  cin >> n >> m >> k;

  int a, b, c, d;
  cin >> a >> b >> c >> d;
  a--, b--, c--, d--;

  string s[n];
  for (int i = 0; i < n; i++) {
    cin >> s[i];
  }
  vector<vector<bool>> vis(n, vector<bool>(m, false));
  queue<vector<int>> q; //{ x cord, y cord, dis}
  q.push({a, b, 0});

  int max_stroke;

  while (!q.empty()) {
    int x = q.front()[0], y = q.front()[1];
    int dis = q.front()[2];
    q.pop();
    if (x == c && y == d) {
      cout << dis << "\\n";
      return 0;
    }
    if (vis[x][y])
      continue;
    vis[x][y] = true;

    max_stroke = x;
    for (int i = 1; i <= k; i++) {
      if (x - i >= 0 && s[x - i][y] != '@' && vis[x - i][y] == false)
        max_stroke = x - i;
      else
        break;
    }
    for (int i = max_stroke; i < x; i++) {
      q.push({i, y, dis + 1});
    }
    max_stroke = x;
    for (int i = 1; i <= k; i++) {
      if (x + i < n && s[x + i][y] != '@' && vis[x + i][y] == false)
        max_stroke = x + i;
      else
        break;
    }
    for (int i = max_stroke; i > x; i--) {
      q.push({i, y, dis + 1});
    }
    max_stroke = y;
    for (int i = 1; i <= k; i++) {
      if (y - i >= 0 && s[x][y - i] != '@' && vis[x][y - i] == false)
        max_stroke = y - i;
      else
        break;
    }
    for (int i = max_stroke; i < y; i++) {
      q.push({x, i, dis + 1});
    }
    max_stroke = y;
    for (int i = 1; i <= k; i++) {
      if (y + i < m && s[x][y + i] != '@' && vis[x][y + i] == false)
        max_stroke = y + i;
      else
        break;
    }
    for (int i = max_stroke; i > y; i--) {
      q.push({x, i, dis + 1});
    }
  }
  cout << "-1\\n";
}