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

#pragma GCC optimize("Ofast", "unroll-loops")

#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define ld long double

int H, W;
vector<string> grid;
int K;
int gx, gy; // 目的地
int sx, sy;
vector<vector<bool>> visited;
vector<vector<int>> qed;

void input(void) {
  cin >> H >> W >> K;
  cin >> sx >> sy >> gx >> gy;
  --sx, --sy, --gx, --gy;
  grid.resize(H);
  for (auto &gi : grid)
    cin >> gi;
  visited.resize(H, vector<bool>(W, false));
  qed.resize(H, vector<int>(W, (1 << 30)));
}

void bfs() {
  queue<tuple<int, int, int>> q;
  visited[sx][sy] = true;
  q.push(make_tuple(sx, sy, 0));
  qed[sx][sy] = 0;
  while (!q.empty()) {
    auto tmp = q.front();
    q.pop();
    int x = get<0>(tmp);
    int y = get<1>(tmp);
    int d = get<2>(tmp);
    if (x == gx && y == gy) {
      cout << d << endl;
      return;
    }
    for (int i = 1; i <= K; ++i) {
      if (y - i < 0)
        break;
      if (grid[x][y - i] == '@')
        break;
      if (visited[x][y - i]) {
        if (qed[x][y - i] == d + 1)
          continue;
        break;
      }
      visited[x][y - i] = true;
      q.push(make_tuple(x, y - i, d + 1));
      qed[x][y - i] = d + 1;
    }
    for (int i = 1; i <= K; ++i) {
      if (y + i >= W)
        break;
      if (grid[x][y + i] == '@')
        break;
      if (visited[x][y + i]) {
        if (qed[x][y + i] == d + 1)
          continue;
        break;
      }
      visited[x][y + i] = true;
      q.push(make_tuple(x, y + i, d + 1));
      qed[x][y + i] = d + 1;
    }
    for (int i = 1; i <= K; ++i) {
      if (x - i < 0)
        break;
      if (grid[x - i][y] == '@')
        break;
      if (visited[x - i][y]) {
        if (qed[x - i][y] == d + 1)
          continue;
        break;
      }
      visited[x - i][y] = true;
      q.push(make_tuple(x - i, y, d + 1));
      qed[x - i][y] = d + 1;
    }
    for (int i = 1; i <= K; ++i) {
      if (x + i >= H)
        break;
      if (grid[x + i][y] == '@')
        break;
      if (visited[x + i][y]) {
        if (qed[x + i][y] == d + 1)
          continue;
        break;
      }
      visited[x + i][y] = true;
      q.push(make_tuple(x + i, y, d + 1));
      qed[x + i][y] = d + 1;
    }
  }
  cout << -1 << endl;
}

int main(void) {
  input();
  bfs();
  return 0;
}

Error: TLE

Fixed submission:

#pragma GCC optimize("Ofast", "unroll-loops")

#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define ld long double

int H, W;
vector<string> grid;
int K;
int gx, gy; // 目的地
int sx, sy;
vector<vector<bool>> visited;

void input(void) {
  cin >> H >> W >> K;
  cin >> sx >> sy >> gx >> gy;
  --sx, --sy, --gx, --gy;
  grid.resize(H);
  for (auto &gi : grid)
    cin >> gi;
  visited.resize(H, vector<bool>(W, false));
}

void bfs() {
  queue<tuple<int, int, int>> q;
  visited[sx][sy] = true;
  q.push(make_tuple(sx, sy, 0));
  while (!q.empty()) {
    auto tmp = q.front();
    q.pop();
    int x = get<0>(tmp);
    int y = get<1>(tmp);
    int d = get<2>(tmp);
    if (x == gx && y == gy) {
      cout << d << endl;
      return;
    }
    for (int i = 1; i <= K; ++i) {
      if (y - i < 0)
        break;
      if (grid[x][y - i] == '@')
        break;
      if (visited[x][y - i])
        continue;
      visited[x][y - i] = true;
      q.push(make_tuple(x, y - i, d + 1));
    }
    for (int i = 1; i <= K; ++i) {
      if (y + i >= W)
        break;
      if (grid[x][y + i] == '@')
        break;
      if (visited[x][y + i])
        continue;
      visited[x][y + i] = true;
      q.push(make_tuple(x, y + i, d + 1));
    }
    for (int i = 1; i <= K; ++i) {
      if (x - i < 0)
        break;
      if (grid[x - i][y] == '@')
        break;
      if (visited[x - i][y])
        continue;
      visited[x - i][y] = true;
      q.push(make_tuple(x - i, y, d + 1));
    }
    for (int i = 1; i <= K; ++i) {
      if (x + i >= H)
        break;
      if (grid[x + i][y] == '@')
        break;
      if (visited[x + i][y])
        continue;
      visited[x + i][y] = true;
      q.push(make_tuple(x + i, y, d + 1));
    }
  }
  cout << -1 << endl;
}

int main(void) {
  input();
  bfs();
  return 0;
}