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

// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using coord = pair<int, int>;
constexpr char block = '@';
constexpr char vacant = '.';
constexpr int TBC = INT_MAX;

int BFS(const vector<string> &board, coord s, coord g, int sz_step,
        vector<vector<int>> *dist) {
  const int H = board.size();
  const int W = board.at(0).size();

  const vector<coord> diff = {
      make_pair(0, -1), make_pair(-1, 0), make_pair(1, 0),
      make_pair(0, 1) // 上下4方向
                      // make_pair(-1,-1), make_pair(1,-1), make_pair(-1,1),
                      // make_pair(1,1) // 斜め4方向
  };                  // 探索方向

  vector<vector<int>> checked(H, vector<int>(W, TBC));
  queue<coord> bfs;
  dist->at(s.first).at(s.second) = 0;
  bfs.push(s);

  while (!bfs.empty()) {
    coord p = bfs.front();
    auto [x, y] = p;
    bfs.pop();

    if (p == g) {
      break;
    } else {
      for (const auto &[dx_e, dy_e] : diff) {
        for (int sz = 1; sz <= sz_step; sz++) {
          int dx = sz * dx_e;
          int dy = sz * dy_e;
          if (x + dx <= -1 || x + dx >= H || y + dy <= -1 || y + dy >= W)
            break;
          else if (board.at(x + dx).at(y + dy) == block) {
            break;
          } else if (dist->at(x + dx).at(y + dy) > dist->at(x).at(y) + 1) {
            dist->at(x + dx).at(y + dy) = dist->at(x).at(y) + 1;
            bfs.push(make_pair(x + dx, y + dy));
          } else if (dist->at(x + dx).at(y + dy) < dist->at(x).at(y) + 1) {
            break;
          }
        }
      }
    }
  }

  return dist->at(g.first).at(g.second);
}

int main() {
  int H, W, K;
  cin >> H >> W >> K;

  coord s, g;
  cin >> s.first >> s.second >> g.first >> g.second;
  s.first--;
  s.second--;
  g.first--;
  g.second--; // 1-indexed -> 0-indexed

  vector<string> board(H);
  for (int i = 0; i < H; i++) {
    cin >> board.at(i);
  }
  vector<vector<int>> dist(H, vector<int>(W, TBC));

  int dist_s2g = BFS(board, s, g, K, &dist);

  int ans = (dist_s2g != TBC) ? dist_s2g : -1;
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using coord = pair<int, int>;
constexpr char block = '@';
constexpr char vacant = '.';
const int TBC = INT_MAX;

int BFS(const vector<string> &board, coord s, coord g, int sz_step,
        vector<vector<int>> *dist) {
  const int H = board.size();
  const int W = board.at(0).size();

  const vector<coord> diff = {
      make_pair(0, -1), make_pair(-1, 0), make_pair(1, 0),
      make_pair(0, 1) // 上下4方向
                      // make_pair(-1,-1), make_pair(1,-1), make_pair(-1,1),
                      // make_pair(1,1) // 斜め4方向
  };                  // 探索方向

  vector<vector<int>> checked(H, vector<int>(W, TBC));
  queue<coord> bfs;
  dist->at(s.first).at(s.second) = 0;
  bfs.push(s);

  while (!bfs.empty()) {
    coord p = bfs.front();
    auto [x, y] = p;
    bfs.pop();

    if (p == g) {
      break;
    } else {
      for (const auto &[dx_e, dy_e] : diff) {
        for (int sz = 1; sz <= sz_step; sz++) {
          int dx = sz * dx_e;
          int dy = sz * dy_e;
          if (x + dx <= -1 || x + dx >= H || y + dy <= -1 || y + dy >= W)
            break;
          else if (board.at(x + dx).at(y + dy) == block) {
            break;
          } else if (board.at(x + dx).at(y + dy) != block &&
                     dist->at(x + dx).at(y + dy) > dist->at(x).at(y) + 1) {
            dist->at(x + dx).at(y + dy) = dist->at(x).at(y) + 1;
            bfs.push(make_pair(x + dx, y + dy));
          }
        }
      }
    }
  }

  return dist->at(g.first).at(g.second);
}

int main() {
  int H, W, K;
  cin >> H >> W >> K;

  coord s, g;
  cin >> s.first >> s.second >> g.first >> g.second;
  s.first--;
  s.second--;
  g.first--;
  g.second--; // 1-indexed -> 0-indexed

  vector<string> board(H);
  for (int i = 0; i < H; i++) {
    cin >> board.at(i);
  }
  vector<vector<int>> dist(H, vector<int>(W, TBC));

  int dist_s2g = BFS(board, s, g, K, &dist);

  int ans = (dist_s2g != TBC) ? dist_s2g : -1;
  cout << ans << endl;
}