Example 07 — Swap Game BFS output

8-step scene. BFS trên $3\times3$ swap space với input $[3,1,2/4,5,6/7,8,9]$. Thử: Next/Prev, /, Space để Play. Dừng ở step 6 để predict. Click widget trước để focus keyboard nav.

depth
seen1
queue (fifo →)
Step 1 / 8

STEP 1 — INIT

Khởi tạo: $\text{queue} = [(start, d{=}0)]$, $\text{seen} = \{start\}$. Chưa có gì xảy ra.

Pop đầu queue: $(3,1,2,\dots)$ ở $d{=}0$. Queue rỗng tạm thời.

$start \ne target$. Không return. Chuyển sang expand.

Duyệt 12 edge, sinh 12 state mới, tất cả chưa trong $seen$. Push vào queue với $d{=}1$.

Pop $(1,3,2,\dots)$ ở $d{=}1$. Còn 11 phần tử trong queue.

Ta sắp expand $(1,3,2,\dots)$. Một trong 12 neighbor chính là target. Edge nào?

Khi tới edge $(1,2)$ trên $A$: swap cho $(1,2,3,\dots)$ = target. Chưa trong seen ⇒ push với $d{=}2$.

BFS pop hết state $d{=}1$, rồi pop $(1,2,3,\dots)$ ở $d{=}2$. $s == target$ ⇒ return 2.

🔮 Swap nào trên $(1,3,2,\dots)$ cho ra $(1,2,3,\dots)$?

Notes on what you're seeing