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

#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int h, w, k, x1, y1, x2, y2;
  cin >> h >> w >> k >> x1 >> y1 >> x2 >> y2;
  char grid[h + 3][w + 3];
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      cin >> grid[i][j];
    }
  }
  int dist[h + 3][w + 3];
  pair<int, int> par[h + 3][w + 3];
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      dist[i][j] = -1;
      par[i][j] = make_pair(-1, -1);
    }
  }
  dist[x1][y1] = 0;
  par[x1][y1] = make_pair(0, 0);
  queue<pair<int, int>> q;
  q.push(make_pair(x1, y1));
  bool ok = false;
  while (!q.empty()) {
    int x = q.front().first, y = q.front().second;
    q.pop();
    for (int i = y + 1; i <= min(w, y + k); i++) {
      if (grid[x][i] == '@' || par[x][y] == par[x][i]) {
        break;
      }
      if (dist[x][i] == -1 || dist[x][i] > dist[x][y] + 1) {
        dist[x][i] = dist[x][y] + 1;
        par[x][i] = make_pair(x, y);
        q.push(make_pair(x, i));
      }
      if (x == x2 && i == y2) {
        ok = true;
        break;
      }
    }
    if (ok) {
      break;
    }
    for (int i = y - 1; i >= max(1, y - k); i--) {
      if (grid[x][i] == '@' || par[x][y] == par[x][i]) {
        break;
      }
      if (dist[x][i] == -1 || dist[x][i] > dist[x][y] + 1) {
        dist[x][i] = dist[x][y] + 1;
        par[x][i] = make_pair(x, y);
        q.push(make_pair(x, i));
      }
      if (x == x2 && i == y2) {
        ok = true;
        break;
      }
    }
    if (ok) {
      break;
    }
    for (int i = x + 1; i <= min(h, x + k); i++) {
      if (grid[i][y] == '@' || par[x][y] == par[i][y]) {
        break;
      }
      if (dist[i][y] == -1 || dist[i][y] > dist[x][y] + 1) {
        dist[i][y] = dist[x][y] + 1;
        par[i][y] = make_pair(x, y);
        q.push(make_pair(i, y));
      }
      if (i == x2 && y == y2) {
        ok = true;
        break;
      }
    }
    if (ok) {
      break;
    }
    for (int i = x - 1; i >= max(1, x - k); i--) {
      if (grid[i][y] == '@' || par[x][y] == par[i][y]) {
        break;
      }
      if (dist[i][y] == -1 || dist[i][y] > dist[x][y] + 1) {
        dist[i][y] = dist[x][y] + 1;
        par[i][y] = make_pair(x, y);
        q.push(make_pair(i, y));
      }
      if (i == x2 && y == y2) {
        ok = true;
        break;
      }
    }
    if (ok) {
      break;
    }
  }
  cout << dist[x2][y2];
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int h, w, k, x1, y1, x2, y2;
  cin >> h >> w >> k >> x1 >> y1 >> x2 >> y2;
  char grid[h + 3][w + 3];
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      cin >> grid[i][j];
    }
  }
  int dist[h + 3][w + 3];
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      dist[i][j] = -1;
    }
  }
  dist[x1][y1] = 0;
  queue<pair<int, int>> q;
  q.push(make_pair(x1, y1));
  bool ok = false;
  while (!q.empty()) {
    int x = q.front().first, y = q.front().second;
    q.pop();
    for (int i = y + 1; i <= min(w, y + k); i++) {
      if (grid[x][i] == '@') {
        break;
      }
      if (dist[x][i] == -1 || dist[x][i] > dist[x][y] + 1) {
        dist[x][i] = dist[x][y] + 1;
        q.push(make_pair(x, i));
      }
      if (x == x2 && i == y2) {
        ok = true;
        break;
      }
    }
    if (ok) {
      break;
    }
    for (int i = y - 1; i >= max(1, y - k); i--) {
      if (grid[x][i] == '@') {
        break;
      }
      if (dist[x][i] == -1 || dist[x][i] > dist[x][y] + 1) {
        dist[x][i] = dist[x][y] + 1;
        q.push(make_pair(x, i));
      }
      if (x == x2 && i == y2) {
        ok = true;
        break;
      }
    }
    if (ok) {
      break;
    }
    for (int i = x + 1; i <= min(h, x + k); i++) {
      if (grid[i][y] == '@') {
        break;
      }
      if (dist[i][y] == -1 || dist[i][y] > dist[x][y] + 1) {
        dist[i][y] = dist[x][y] + 1;
        q.push(make_pair(i, y));
      }
      if (i == x2 && y == y2) {
        ok = true;
        break;
      }
    }
    if (ok) {
      break;
    }
    for (int i = x - 1; i >= max(1, x - k); i--) {
      if (grid[i][y] == '@') {
        break;
      }
      if (dist[i][y] == -1 || dist[i][y] > dist[x][y] + 1) {
        dist[i][y] = dist[x][y] + 1;
        q.push(make_pair(i, y));
      }
      if (i == x2 && y == y2) {
        ok = true;
        break;
      }
    }
    if (ok) {
      break;
    }
  }
  cout << dist[x2][y2];
  return 0;
}