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

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

const int INF = 1e9;
struct R2 {
  int y, x, UDLW;
};

signed main() {
  int h, w, k;
  cin >> h >> w >> k;
  vector<vector<int>> d(h, vector<int>(w, INF));
  queue<R2> que;
  int x1, y1, x2, y2;
  cin >> y1 >> x1 >> y2 >> x2;
  y1--;
  x1--;
  y2--;
  x2--;
  int sy, sx, gy, gx;
  if (rand() & 1)
    sy = y1, sx = x1, gy = y2, gx = x2;
  else
    sy = y2, sx = x2, gy = y1, gx = x1;
  d[sy][sx] = 0;
  que.push({sy, sx, 0});
  vector<string> s(h);
  for (int i = 0; i < h; i++)
    cin >> s[i];
  while (que.size()) {
    R2 p = que.front();
    que.pop();
    if (p.UDLW != 1)
      for (int i = 1; i <= k; i++) {
        if (p.y + i >= h || s[p.y + i][p.x] == '@')
          break;
        if (d[p.y + i][p.x] > d[p.y][p.x] + 1) {
          d[p.y + i][p.x] = d[p.y][p.x] + 1;
          que.push({p.y + i, p.x, 2});
        }
      }
    if (p.UDLW != 2)
      for (int i = 1; i <= k; i++) {
        if (p.y - i < 0 || s[p.y - i][p.x] == '@')
          break;
        if (d[p.y - i][p.x] > d[p.y][p.x] + 1) {
          d[p.y - i][p.x] = d[p.y][p.x] + 1;
          que.push({p.y - i, p.x, 1});
        }
      }
    if (p.UDLW != 4)
      for (int i = 1; i <= k; i++) {
        if (p.x + i >= w || s[p.y][p.x + i] == '@')
          break;
        if (d[p.y][p.x + i] > d[p.y][p.x] + 1) {
          d[p.y][p.x + i] = d[p.y][p.x] + 1;
          que.push({p.y, p.x + i, 3});
        }
      }
    if (p.UDLW != 3)
      for (int i = 1; i <= k; i++) {
        if (p.x - i < 0 || s[p.y][p.x - i] == '@')
          break;
        if (d[p.y][p.x - i] > d[p.y][p.x] + 1) {
          d[p.y][p.x - i] = d[p.y][p.x] + 1;
          que.push({p.y, p.x - i, 4});
        }
      }
    if (d[gy][gx] != INF)
      break;
  }
  cout << (d[gy][gx] == INF ? -1 : d[gy][gx]) << endl;
}


Error: TLE

Fixed submission:

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

const int INF = 1e9;
struct R2 {
  int y, x;
};

signed main() {
  int h, w, k;
  cin >> h >> w >> k;
  vector<vector<int>> d(h, vector<int>(w, INF));
  queue<R2> que;
  int x1, y1, x2, y2;
  cin >> y1 >> x1 >> y2 >> x2;
  y1--;
  x1--;
  y2--;
  x2--;
  int sy, sx, gy, gx;
  if (rand() & 1)
    sy = y1, sx = x1, gy = y2, gx = x2;
  else
    sy = y2, sx = x2, gy = y1, gx = x1;
  d[sy][sx] = 0;
  que.push({sy, sx});
  vector<string> s(h);
  for (int i = 0; i < h; i++)
    cin >> s[i];
  while (que.size()) {
    R2 p = que.front();
    que.pop();
    for (int i = 1; i <= k; i++) {
      if (p.y + i >= h || s[p.y + i][p.x] == '@')
        break;
      if (d[p.y + i][p.x] > d[p.y][p.x] + 1) {
        d[p.y + i][p.x] = d[p.y][p.x] + 1;
        que.push({p.y + i, p.x});
      }
    }
    for (int i = 1; i <= k; i++) {
      if (p.y - i < 0 || s[p.y - i][p.x] == '@')
        break;
      if (d[p.y - i][p.x] > d[p.y][p.x] + 1) {
        d[p.y - i][p.x] = d[p.y][p.x] + 1;
        que.push({p.y - i, p.x});
      }
    }
    for (int i = 1; i <= k; i++) {
      if (p.x + i >= w || s[p.y][p.x + i] == '@')
        break;
      if (d[p.y][p.x + i] > d[p.y][p.x] + 1) {
        d[p.y][p.x + i] = d[p.y][p.x] + 1;
        que.push({p.y, p.x + i});
      }
    }
    for (int i = 1; i <= k; i++) {
      if (p.x - i < 0 || s[p.y][p.x - i] == '@')
        break;
      if (d[p.y][p.x - i] > d[p.y][p.x] + 1) {
        d[p.y][p.x - i] = d[p.y][p.x] + 1;
        que.push({p.y, p.x - i});
      }
    }
    if (d[gy][gx] != INF)
      break;
  }
  cout << (d[gy][gx] == INF ? -1 : d[gy][gx]) << endl;
}