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

#include <cmath>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

int main() {
  int H, W, K;
  cin >> H >> W >> K;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  vector<vector<char>> Map(H, vector<char>(W));
  for (int i = 0; i < H; i++)
    for (int j = 0; j < W; j++)
      cin >> Map[i][j];
  queue<vector<int>> Q;
  Q.push({y1 - 1, x1 - 1, 0, 0});
  while (!Q.empty()) {
    int x = Q.front()[0];
    int y = Q.front()[1];
    int d = Q.front()[2];
    int npd = Q.front()[3];
    Q.pop();
    if (x == y2 - 1 && y == x2 - 1) {
      cout << d << endl;
      return 0;
    }

    int nx, ny, pd;
    vector<vector<int>> subs;
    if (npd != 1 && abs(npd) != 2) {
      nx = x - 1, ny = y, pd = 1;
      subs = {};
      while (nx >= 0 && abs(nx - x) <= K && Map[ny][nx] != '@') {
        if (abs(nx - x) == K)
          pd = -1;
        if (Map[ny][nx] == '.') {
          subs.push_back({nx, ny, d + 1, pd});
          Map[ny][nx] = 'x';
        }
        nx--;
      }
      for (int i = subs.size() - 1; i >= 0; i--)
        Q.push(subs[i]);
    }
    if (npd != 2 && abs(npd) != 1) {
      nx = x + 1, ny = y, pd = 2;
      subs = {};
      while (nx < W && abs(nx - x) <= K && Map[ny][nx] != '@') {
        if (abs(nx - x) == K)
          pd = -2;
        if (Map[ny][nx] == '.') {
          subs.push_back({nx, ny, d + 1, pd});
          Map[ny][nx] = 'x';
        }

        nx++;
      }
      for (int i = subs.size() - 1; i >= 0; i--)
        Q.push(subs[i]);
    }
    if (npd != 3 && abs(npd) != 4) {
      nx = x, ny = y - 1, pd = 3;
      subs = {};
      while (ny >= 0 && abs(ny - y) <= K && Map[ny][nx] != '@') {
        if (abs(ny - y) == K)
          pd = -3;
        if (Map[ny][nx] == '.') {
          subs.push_back({nx, ny, d + 1, pd});
          Map[ny][nx] = 'x';
        }
        ny--;
      }
      for (int i = subs.size() - 1; i >= 0; i--)
        Q.push(subs[i]);
    }
    if (npd != 4 && abs(npd) != 3) {
      nx = x, ny = y + 1, pd = 4;
      subs = {};
      while (ny < H && abs(ny - y) <= K && Map[ny][nx] != '@') {
        if (abs(ny - y) == K)
          pd = -4;
        if (Map[ny][nx] == '.') {
          subs.push_back({nx, ny, d + 1, pd});
          Map[ny][nx] = 'x';
        }
        ny++;
      }
      for (int i = subs.size() - 1; i >= 0; i--)
        Q.push(subs[i]);
    }
  }
  cout << -1 << endl;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

int main() {
  int H, W, K;
  cin >> H >> W >> K;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  vector<vector<char>> Map(H, vector<char>(W));
  for (int i = 0; i < H; i++)
    for (int j = 0; j < W; j++)
      cin >> Map[i][j];
  queue<vector<int>> Q;
  Q.push({y1 - 1, x1 - 1, 0, 0});
  while (!Q.empty()) {
    int x = Q.front()[0];
    int y = Q.front()[1];
    int d = Q.front()[2];
    int npd = Q.front()[3];
    Q.pop();
    if (Map[y][x] != '.')
      continue;
    if (x == y2 - 1 && y == x2 - 1) {
      cout << d << endl;
      return 0;
    }
    Map[y][x] = 'x';

    int nx, ny, pd;
    vector<vector<int>> subs;
    if (npd != 1 && abs(npd) != 2) {
      nx = x - 1, ny = y, pd = 1;
      subs = {};
      while (nx >= 0 && abs(nx - x) <= K && Map[ny][nx] != '@') {
        if (abs(nx - x) == K)
          pd = -1;
        if (Map[ny][nx] == '.')
          subs.push_back({nx, ny, d + 1, pd});
        nx--;
      }
      for (int i = subs.size() - 1; i >= 0; i--)
        Q.push(subs[i]);
    }
    if (npd != 2 && abs(npd) != 1) {
      nx = x + 1, ny = y, pd = 2;
      subs = {};
      while (nx < W && abs(nx - x) <= K && Map[ny][nx] != '@') {
        if (abs(nx - x) == K)
          pd = -2;
        if (Map[ny][nx] == '.')
          subs.push_back({nx, ny, d + 1, pd});
        nx++;
      }
      for (int i = subs.size() - 1; i >= 0; i--)
        Q.push(subs[i]);
    }
    if (npd != 3 && abs(npd) != 4) {
      nx = x, ny = y - 1, pd = 3;
      subs = {};
      while (ny >= 0 && abs(ny - y) <= K && Map[ny][nx] != '@') {
        if (abs(ny - y) == K)
          pd = -3;
        if (Map[ny][nx] == '.')
          subs.push_back({nx, ny, d + 1, pd});
        ny--;
      }
      for (int i = subs.size() - 1; i >= 0; i--)
        Q.push(subs[i]);
    }
    if (npd != 4 && abs(npd) != 3) {
      nx = x, ny = y + 1, pd = 4;
      subs = {};
      while (ny < H && abs(ny - y) <= K && Map[ny][nx] != '@') {
        if (abs(ny - y) == K)
          pd = -4;
        if (Map[ny][nx] == '.')
          subs.push_back({nx, ny, d + 1, pd});
        ny++;
      }
      for (int i = subs.size() - 1; i >= 0; i--)
        Q.push(subs[i]);
    }
  }
  cout << -1 << endl;
}