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

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

int main() {
  int h, w, k;
  cin >> h >> w >> k;
  vector<string> grid(h);
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--, y1--, x2--, y2--;
  for (int i = 0; i < h; i++)
    cin >> grid[i];
  vector<vector<int>> dp(h, vector<int>(w, 1e6 + 1));
  queue<pair<int, int>> q;
  vector<vector<bool>> vis(h, vector<bool>(w, 0));
  dp[x1][y1] = 0;
  q.push({x1, y1});
  while (!q.empty()) {
    pair<int, int> here = q.front();
    q.pop();
    int x = here.first, y = here.second;
    vis[x][y] = 1;
    // cout << x << " " << y << endl;
    for (int i = 1; i <= k && x + i < h; i++) {
      if (grid[x + i][y] == '@')
        break;
      if (dp[x][y] + 1 <= dp[x + i][y]) {
        dp[x + i][y] = dp[x][y] + 1;
        if (!vis[x + i][y])
          q.push({x + i, y}), vis[x + i][y] = 1;
      } else
        break;
    }
    for (int i = 1; i <= k && y + i < w; i++) {
      if (grid[x][y + i] == '@')
        break;
      if (dp[x][y] + 1 <= dp[x][y + i]) {
        dp[x][y + i] = dp[x][y] + 1;
        if (!vis[x][y + i])
          q.push({x, y + i}), vis[x][y + i] = 1;
      } else
        break;
    }
    for (int i = 1; i <= k && x - i >= 0; i++) {
      if (grid[x - i][y] == '@')
        break;
      if (dp[x][y] + 1 <= dp[x - i][y]) {
        dp[x - i][y] = dp[x][y] + 1;
        if (!vis[x - i][y])
          q.push({x - i, y}), vis[x - i][y] = 1;
      } else
        break;
    }
    for (int i = 1; i <= k && y - i >= 0; i++) {
      if (grid[x][y - i] == '@')
        break;
      if (dp[x][y] + 1 <= dp[x][y - i]) {
        dp[x][y - i] = dp[x][y] + 1;
        if (!vis[x][y - i])
          q.push({x, y - i}), vis[x][y - i] = 1;
      } else
        break;
    }
  }
  // for (int i=0; i<h; i++) {
  //   for (int j=0; j<w; j++)
  //     cout << dp[i][j] << " ";
  //   cout << endl;
  // }
  if (dp[x2][y2] == (int)1e6 + 1)
    cout << -1;
  else
    cout << dp[x2][y2];
  return 0;
}


Error: TLE

Fixed submission:

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

int main() {
  int h, w, k;
  cin >> h >> w >> k;
  vector<string> grid(h);
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--, y1--, x2--, y2--;
  for (int i = 0; i < h; i++)
    cin >> grid[i];
  vector<vector<int>> dp(h, vector<int>(w, 1e6 + 1));
  queue<pair<int, int>> q;
  vector<vector<bool>> vis(h, vector<bool>(w, 0));
  dp[x1][y1] = 0;
  q.push({x1, y1});
  while (!q.empty()) {
    pair<int, int> here = q.front();
    q.pop();
    int x = here.first, y = here.second;
    vis[x][y] = 1;
    // cout << x << " " << y << endl;
    for (int i = 1; i <= k && x + i < h; i++) {
      if (grid[x + i][y] == '@')
        break;
      if (dp[x][y] + 1 < dp[x + i][y]) {
        dp[x + i][y] = dp[x][y] + 1;
        if (!vis[x + i][y])
          q.push({x + i, y});
        else
          break;
      }
      // else break;
    }
    for (int i = 1; i <= k && y + i < w; i++) {
      if (grid[x][y + i] == '@')
        break;
      if (dp[x][y] + 1 < dp[x][y + i]) {
        dp[x][y + i] = dp[x][y] + 1;
        if (!vis[x][y + i])
          q.push({x, y + i});
        else
          break;
      }
      // else break;
    }
    for (int i = 1; i <= k && x - i >= 0; i++) {
      if (grid[x - i][y] == '@')
        break;
      if (dp[x][y] + 1 < dp[x - i][y]) {
        dp[x - i][y] = dp[x][y] + 1;
        if (!vis[x - i][y])
          q.push({x - i, y});
        else
          break;
      }
      // else break;
    }
    for (int i = 1; i <= k && y - i >= 0; i++) {
      if (grid[x][y - i] == '@')
        break;
      if (dp[x][y] + 1 < dp[x][y - i]) {
        dp[x][y - i] = dp[x][y] + 1;
        if (!vis[x][y - i])
          q.push({x, y - i});
        else
          break;
      }
      // else break;
    }
  }
  if (dp[x2][y2] == (int)1e6 + 1)
    cout << -1;
  else
    cout << dp[x2][y2];
  return 0;
}