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

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

int main() {
  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];

  int inf = 1e9;
  vector<vector<int>> dist(H, vector<int>(W, inf));
  dist[x1][y1] = 0;
  queue<int> q;
  q.push(x1 * W + y1);
  while (!q.empty()) {
    int x = q.front() / W;
    int y = q.front() % W;
    q.pop();
    for (int i = 1; i <= K; i++) {
      int nx = x - i, ny = y;
      if (nx <= -1 || nx >= H || ny <= -1 || ny >= W)
        break;
      if (grid[nx][ny] == '@')
        break;
      if (dist[nx][ny] <= dist[x][y])
        break;
      if (dist[nx][ny] > dist[x][y] + 1) {
        dist[nx][ny] = dist[x][y] + 1;
        q.push(nx * W + ny);
      }
    }
    for (int i = 1; i <= K; i++) {
      int nx = x + i, ny = y;
      if (nx <= -1 || nx >= H || ny <= -1 || ny >= W)
        break;
      if (grid[nx][ny] == '@')
        break;
      if (dist[nx][ny] <= dist[x][y])
        break;
      if (dist[nx][ny] > dist[x][y] + 1) {
        dist[nx][ny] = dist[x][y] + 1;
        q.push(nx * W + ny);
      }
    }
    for (int i = 1; i <= K; i++) {
      int nx = x, ny = y - i;
      if (nx <= -1 || nx >= H || ny <= -1 || ny >= W)
        break;
      if (grid[nx][ny] == '@')
        break;
      if (dist[nx][ny] <= dist[x][y])
        break;
      if (dist[nx][ny] > dist[x][y] + 1) {
        dist[nx][ny] = dist[x][y] + 1;
        q.push(nx * W + ny);
      }
    }
    for (int i = 1; i <= K; i++) {
      int nx = x, ny = y + i;
      if (nx <= -1 || nx >= H || ny <= -1 || ny >= W)
        break;
      if (grid[nx][ny] == '@')
        break;

      if (dist[nx][ny] <= dist[x][y])
        break;
      if (dist[nx][ny] > dist[x][y] + 1) {
        dist[nx][ny] = dist[x][y] + 1;
        q.push(nx * W + ny);
      }
    }
  }

  if (dist[x2][y2] == inf)
    cout << -1 << endl;
  else
    cout << dist[x2][y2] << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  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];

  int inf = 1e9;
  vector<vector<int>> dist(H, vector<int>(W, inf));
  dist[x1][y1] = 0;
  queue<int> q;
  q.push(x1 * W + y1);
  while (!q.empty()) {
    int x = q.front() / W;
    int y = q.front() % W;
    q.pop();
    for (int i = 1; i <= K; i++) {
      int nx = x - i, ny = y;
      if (nx <= -1 || nx >= H || ny <= -1 || ny >= W)
        break;
      if (grid[nx][ny] == '@')
        break;
      if (dist[nx][ny] > dist[x][y] + 1) {
        dist[nx][ny] = dist[x][y] + 1;
        q.push(nx * W + ny);
      }
    }
    for (int i = 1; i <= K; i++) {
      int nx = x + i, ny = y;
      if (nx <= -1 || nx >= H || ny <= -1 || ny >= W)
        break;
      if (grid[nx][ny] == '@')
        break;
      if (dist[nx][ny] > dist[x][y] + 1) {
        dist[nx][ny] = dist[x][y] + 1;
        q.push(nx * W + ny);
      }
    }
    for (int i = 1; i <= K; i++) {
      int nx = x, ny = y - i;
      if (nx <= -1 || nx >= H || ny <= -1 || ny >= W)
        break;
      if (grid[nx][ny] == '@')
        break;
      if (dist[nx][ny] > dist[x][y] + 1) {
        dist[nx][ny] = dist[x][y] + 1;
        q.push(nx * W + ny);
      }
    }
    for (int i = 1; i <= K; i++) {
      int nx = x, ny = y + i;
      if (nx <= -1 || nx >= H || ny <= -1 || ny >= W)
        break;
      if (grid[nx][ny] == '@')
        break;
      if (dist[nx][ny] > dist[x][y] + 1) {
        dist[nx][ny] = dist[x][y] + 1;
        q.push(nx * W + ny);
      }
    }
  }

  if (dist[x2][y2] == inf)
    cout << -1 << endl;
  else
    cout << dist[x2][y2] << endl;
  return 0;
}