A* search

A* 탐색

Choose the next cell using path cost so far plus estimated remaining cost.

···
html
<div class="v"><header><b>A* GRID</b><span id="state"></span></header><main><div class="agrid" id="agrid"></div><div class="acost" id="acost"></div></main><footer>frontier ordered by f = g + h</footer></div>
css
.v{width:min(92vw,660px);height:min(86vh,310px);box-sizing:border-box;padding:clamp(9px,2.5vmin,18px);border:1px solid var(--line);border-radius:14px;background:var(--surface);display:flex;flex-direction:column;gap:7px;color:var(--fg);font:500 clamp(15px,4.5vmin,19px)/1.25 var(--font-sans, sans-serif)}.v header,.v footer{display:flex;justify-content:space-between;align-items:center;gap:8px;white-space:nowrap}.v header b{color:var(--accent);font-size:.9em}.v header span,.v footer{color:var(--muted);font-size:.82em}.v main{flex:1;min-height:0;position:relative;overflow:hidden}.v .mono{font-family:ui-monospace,SFMono-Regular,Consolas,monospace}.v .active{background:var(--accent)!important;color:var(--bg)!important;border-color:var(--accent)!important}.v .muted{opacity:.45} .agrid{height:78%;display:grid;grid-template-columns:repeat(5,1fr);grid-template-rows:repeat(4,1fr);gap:2px;max-width:230px;margin:auto}.agrid b{border:1px solid var(--line);border-radius:3px;display:grid;place-items:center;font:700 .72em ui-monospace,monospace}.agrid b.wall{background:var(--line)}.agrid b.path{background:color-mix(in srgb,var(--accent) 25%,var(--surface))}.acost{text-align:center;color:var(--accent);font:600 .78em ui-monospace,monospace}
js
const route=[0,1,2,7,12,13,14,19],walls=[6,11,16];let step=0;function draw(){const at=route[step],x=at%5,y=Math.floor(at/5),g=step,h=Math.abs(4-x)+Math.abs(3-y);document.getElementById("agrid").innerHTML=Array.from({length:20},(_,i)=>"<b class='"+(walls.includes(i)?"wall":i===at?"active":route.slice(0,step).includes(i)?"path":"")+"'>"+(i===0?"S":i===19?"G":i===at?"•":"")+"</b>").join("");document.getElementById("acost").textContent="g "+g+" + h "+h+" = f "+(g+h);document.getElementById("state").textContent="expand "+(step+1)+" / "+route.length;step=(step+1)%route.length}draw();setInterval(draw,720)

A* orders frontier states by f = g + h: path cost so far plus an estimate to the goal. It updates a neighbor’s cost and parent when it finds a shorter route.

An admissible heuristic that never overestimates remaining cost can preserve optimality. A weak heuristic explores more states; an overestimate can sacrifice the best path.

When to use

Use it for pathfinding on maps or grids when a useful goal-directed estimate is available.

Open as page ↗