競技プログラミング向けのC++23ライブラリ。API、使用例、検証結果を確認できます。
通常版・コンパイラ共通 · この版の一覧へ
通常版・GCC専用 · この版の一覧へ
通常版・Clang専用 · この版の一覧へ
通常版・MSVC専用 · この版の一覧へ
速度優先版・コンパイラ共通 · この版の一覧へ
速度優先版・GCC専用 · この版の一覧へ
速度優先版・Clang専用 · この版の一覧へ
速度優先版・MSVC専用 · この版の一覧へ
1手で同じ方向へ任意の距離を進めるとき、壁を避けて着くまでの最少手数を求めます。縦横移動と斜め移動に対応します。本体
grid_slide_distance(grid, start, target, axes, wall = '#') は指定した終点への手数を返します。grid_slide_distances(grid, start, axes, wall = '#') は全マスへの手数を返します。添字は row * width + column です。グリッドは各行を std::string とし、座標は $0$ 始まりの (行, 列) です。axes の既定値は GridSlideAxes::orthogonal で縦横に移動します。斜め移動には GridSlideAxes::diagonal を指定します。壁と到達不能の値は -1 です。
壁で区切られた直線を1つのノードとし、2つの直線が交わるマスを移れる場所として幅優先探索します。縦 $H$、横 $W$ に対し $O(HW)$ 時間・領域です。移動距離に上限がある場合
const std::array<std::string, 3> grid{"...", "...", "..."};
const auto distance = sirkit::grid_slide_distance(
grid, {0, 0}, {2, 2}, sirkit::GridSlideAxes::diagonal);
// 斜めに1手。
grid_slide_distances(grid,start,axes=GridSlideAxes::orthogonal,wall='#'): 全マスまでの最少手数。 grid_slide_distance(grid,start,target,axes=GridSlideAxes::orthogonal,wall='#'): 指定したマスまでの最少手数。 各手で壁に当たる手前まで、同じ方向へ任意の正のマス数だけ進める。 orthogonalは縦横、diagonalは斜め。座標は0始まりの(行,列)。 壁や到達不能なら-1。全距離の添字はrow*width+column。O(HW)時間・領域。
通常版の検証結果です。
| 問題 | プログラム | 現行ソースの状態 | 提出結果 |
|---|---|---|---|
| Bishop 2 | grid_slide_distances.cpp | 未提出 | — |
✓ 現行ACには、提出本文を照合したACと、利用者が現行版の提出を確認したAtCoderのACを含めます。
| 検査 | 状態 |
|---|---|
| 正常系 | 未実施 |
| 失敗系 | 未測定 |
| Library Checker公式ケース | 無し |
| 性能測定 | 未実施 |
| ASan/UBSan | 未実施 |
| カバレッジ | 未実施 |
到達不能と証明した箇所を除き、通常版本体のカバレッジは行・直接分岐とも100%が合格条件です。未計測の場合も不合格です。
// SPDX-License-Identifier: CC0-1.0
// ドキュメント: https://github.com/reinsirk/sirkit-cpp-library/blob/main/Document/grid_slide_distances.md
//
// API一覧
// grid_slide_distances(grid,start,axes=GridSlideAxes::orthogonal,wall='#'): 全マスまでの最少手数。
// grid_slide_distance(grid,start,target,axes=GridSlideAxes::orthogonal,wall='#'): 指定したマスまでの最少手数。
// 各手で壁に当たる手前まで、同じ方向へ任意の正のマス数だけ進める。
// orthogonalは縦横、diagonalは斜め。座標は0始まりの(行,列)。
// 壁や到達不能なら-1。全距離の添字はrow*width+column。O(HW)時間・領域。
// SIRKIT_NAMESPACE_V1: 公開APIはsirkit、既存のgeoはsirkit::geo、内部部品はsirkit::detail。
#include <algorithm>
#include <array>
#include <cstddef>
#include <limits>
#include <span>
#include <stdexcept>
#include <string>
#include <utility>
#include <vector>
// SIRKIT_NAMESPACE_OPEN_V1
namespace sirkit {
#ifndef SIRKIT_GRID_STRIDE_LAYOUT_DEFINED
#define SIRKIT_GRID_STRIDE_LAYOUT_DEFINED
namespace detail
{
inline int grid_stride_area(std::size_t height, std::size_t width)
{
if (height == 0 || width == 0)
{
throw std::invalid_argument("grid stride: empty grid");
}
constexpr auto kMax = static_cast<std::size_t>(std::numeric_limits<int>::max() - 1);
if (height > kMax / width)
{
throw std::length_error("grid stride: too many cells");
}
return static_cast<int>(height * width);
}
struct StrideGrid
{
std::span<const std::string> rows;
int height, width, area;
char wall;
[[nodiscard]] int id(std::pair<int, int> point) const
{
const auto [r, c] = point;
#ifndef SKIP_RUNTIME_CHECKS
if (r < 0 || r >= height || c < 0 || c >= width)
{
throw std::out_of_range("grid stride: coordinate out of range");
}
#endif
return r * width + c;
}
};
inline StrideGrid make_stride_grid(std::span<const std::string> rows, char wall)
{
const std::size_t width = rows.empty() ? 0 : rows.front().size();
const int area = grid_stride_area(rows.size(), width);
if (std::ranges::any_of(rows, [width](const std::string &row)
{
return row.size() != width;
}))
{
throw std::invalid_argument("grid stride: nonrectangular grid");
}
return {rows, static_cast<int>(rows.size()), static_cast<int>(width), area, wall};
}
} // namespace detail
#endif
#ifndef SIRKIT_GRID_SLIDE_DISTANCES_DEFINED
#define SIRKIT_GRID_SLIDE_DISTANCES_DEFINED
enum class GridSlideAxes
{
orthogonal,
diagonal,
};
namespace detail
{
inline std::vector<int> grid_slide_search(const StrideGrid &grid, int source, int target, GridSlideAxes axes)
{
const auto [rows, height, width, area, wall] = grid;
const auto blocked = [&](int id)
{
return rows[id / width][id % width] == wall;
};
std::vector<int> result(area, -1);
if (blocked(source) || (target >= 0 && blocked(target)))
{
return result;
}
result[source] = 0;
if (source == target)
{
return result;
}
using Point = std::pair<int, int>;
const auto directions = axes == GridSlideAxes::diagonal
? std::array{Point{1, 1}, Point{1, -1}}
: std::array{Point{0, 1}, Point{1, 0}};
struct Segment
{
int start, length;
};
std::array<std::vector<Segment>, 2> segments;
std::vector<std::array<int, 2>> label(area, {-1, -1});
for (int r = 0; r < height; ++r)
{
for (int c = 0; c < width; ++c)
{
if (rows[r][c] == wall)
{
continue;
}
const int cell = r * width + c;
for (int i = 0; i < 2; ++i)
{
const auto [dr, dc] = directions[i];
const int col = c - dc;
int id = -1;
if (r >= dr && 0 <= col && col < width)
{
id = label[(r - dr) * width + col][i];
}
if (id == -1)
{
id = static_cast<int>(segments[i].size());
segments[i].push_back({cell, 0});
}
label[cell][i] = id;
++segments[i][id].length;
}
}
}
std::array distance{std::vector<int>(segments[0].size(), -1), std::vector<int>(segments[1].size(), -1)};
std::vector<Point> pending;
for (int axis = 0; axis < 2; ++axis)
{
const int id = label[source][axis];
distance[axis][id] = 1;
pending.emplace_back(axis, id);
}
for (std::size_t i = 0; i < pending.size(); ++i)
{
const auto [axis, id] = pending[i];
const int depth = distance[axis][id];
if (target >= 0 && label[target][axis] == id)
{
result[target] = depth;
return result;
}
const int other = axis ^ 1;
const auto [start, length] = segments[axis][id];
const auto [dr, dc] = directions[axis];
const int stride = dr * width + dc;
for (int step = 0; step < length; ++step)
{
const int cell = start + step * stride;
const int next = label[cell][other];
if (distance[other][next] == -1)
{
distance[other][next] = depth + 1;
pending.emplace_back(other, next);
}
}
}
// Every open cell touches two segments, so either both of them are reachable or neither is.
for (int i = 0; i < area; ++i)
{
const auto [first, second] = label[i];
if (i != source && first != -1)
{
result[i] = std::min(distance[0][first], distance[1][second]);
}
}
return result;
}
} // namespace detail
inline std::vector<int> grid_slide_distances(std::span<const std::string> grid, std::pair<int, int> start,
GridSlideAxes axes = GridSlideAxes::orthogonal, char wall = '#')
{
const auto view = detail::make_stride_grid(grid, wall);
return detail::grid_slide_search(view, view.id(start), -1, axes);
}
inline int grid_slide_distance(std::span<const std::string> grid, std::pair<int, int> start,
std::pair<int, int> target, GridSlideAxes axes = GridSlideAxes::orthogonal,
char wall = '#')
{
const auto view = detail::make_stride_grid(grid, wall);
const int goal = view.id(target);
return detail::grid_slide_search(view, view.id(start), goal, axes)[goal];
}
#endif
} // namespace sirkit
// SIRKIT_NAMESPACE_CLOSE_V1
// SIRKIT_NAMESPACE_END_V1