本文へ移動
日本語
「最短経路」は Pro に含まれます。解除するとプレビューが表示されます。

仕組み

格子上の最短経路を示す教科書風の図です。障害物は塗りつぶしたマス、S と G は始点と終点です。変更のたびに A* 探索が走り、調べたマスが到達順に現れ、続いて最短経路がアクセント色でマスの中心を通って描かれます。4 方向では長さは歩数、8 方向では斜めの一歩を √2 と数え、障害物の角は横切れません。

向いている場面

  • Teach graph search: how a shortest path bends around obstacles, how its length changes, and how many cells the search had to examine.
  • Compare 4- and 8-direction movement and see why a diagonal step costs √2.

向かない場面

  • For routes on a real map, use a map with roads: this grid has uniform cells only.

使い方

import React, { useState } from 'react'
import { KeepoutWireRouter } from './KeepoutWireRouter.jsx'

const start = { start: [1, 5], goal: [18, 5], walls: [[9, 3], [9, 4], [9, 5], [9, 6]] }

export function PathLesson() {
  const [grid, setGrid] = useState(start)
  const [length, setLength] = useState(null)
  return (
    <KeepoutWireRouter
      value={grid}
      onChange={(next, details) => {
        setGrid(next)
        setLength(details.path?.length ?? null)
      }}
      defaultMoves={4}
    />
  )
}

プロパティ

プロパティ型既定値説明
value{ start: [x, y], goal: [x, y], walls: Array<[x, y]> }—Controlled grid: the start and goal cells and the obstacle cells, as zero-based column and row.
defaultValue{ start, goal, walls }two walls, S at [2, 4], G at [17, 3]Starting grid when uncontrolled.
onChange(value, { reason, path }) => void—Called after every edit. reason is 'paint', 'drag', 'key', 'clear' or 'cancel'; path is null when G cannot be reached, otherwise { cells, length, straight, diagonal, examined }.
moves4 | 8—Controlled move set: 4 directions, or 8 with diagonal steps of length √2 that may not cut an obstacle's corner.
defaultMoves4 | 88Starting move set when uncontrolled.
onMovesChange(moves) => void—Called when the move set is changed.
columnsnumber20Number of columns (4–40).
rowsnumber9Number of rows (3–24).
labelsobject—Replaces any built-in text: title, formula4, formula8, legendWall, legendExplored, legendPath, grid, help, keys, moves, moves4, moves8, clear, startMark, goalMark, readout, noPath, straight, diagonal, explored, cell ('{x}', '{y}'), wallOn, wallOff, startAt, goalAt ('{cell}'), result ('{length}', '{explored}') and blocked.
localestring'en'Language tag for the numbers.
classNamestring—Extra class on the card, for example to set its width (30rem by default, never wider than its container).

アクセシビリティ

  • The grid is an application region with its instructions as its description. A cell cursor moves with the arrows (Home and End go to the row's ends); Space or Enter toggles an obstacle; S and G put the start or the goal on the cursor. A hint with these keys shows while the grid has keyboard focus.
  • A polite status announces each change with the new path length and the number of cells examined, or that there is no path.
  • The move set is a radio group: arrows switch between 4 and 8 directions. Clear is a button, marked unavailable when there are no obstacles.
  • Painting uses pointer capture and touch-action none on the grid only; a cancelled gesture restores the grid it started from.

モーション

After an edit the examined cells appear in search order within 420ms and the path fades in for --ds-duration-base; while painting, everything updates at once. With reduced motion there is no animation.

プロジェクトに追加

Pro メンバーはプロンプトをコピーし、コーディングエージェントが無料コンポーネントと同じ手順でソースを導入します。

KeepoutWireRouter.jsxkeepout-wire-router.csstokens.cssREADME.md

2 つのファイルを一緒に導入します。スタイルは tokens.css の変数を読み、予備の値も持つため、単体でも表示できます。

フォーム入力
「空き時間グリッド」は Pro に含まれます。解除するとプレビューが表示されます。
「図形のブール演算」は Pro に含まれます。解除するとプレビューが表示されます。
「カスケード選択」は Pro に含まれます。解除するとプレビューが表示されます。