A*

A* Algorithm — 8 Puzzle Problem

SPPU | Third Year | Sem VI | 310253 — Artificial Intelligence

Master these core concepts before running the simulation. Each card builds on the previous one.

A 3×3 sliding puzzle with 8 numbered tiles and one empty space. You slide tiles into the empty space to rearrange them into a target configuration.

Initial State
2
8
3
1
6
4
7
5
Goal State
1
2
3
8
4
7
6
5

Our problem: rearrange from the jumbled start to the target in the fewest moves.

A quick, educated guess about how close you are to your goal.

Everyday Analogy

You're in Pune, driving to Mumbai. Straight-line distance is 150 km. The actual road distance is 160–170 km. That 150 km estimate is your heuristic — it's always optimistic, never overestimates the real distance.

Key property — Admissibility: A good heuristic never overestimates the true cost. This guarantees A* will find the optimal (shortest) solution.

f(n) = g(n) + h(n)
g(n) Moves made so far Like your odometer — counts distance already travelled
h(n) Estimated moves remaining Like GPS — "roughly this much left"
f(n) Total estimated cost Full trip estimate = distance done + distance left
Mini Example — Our Initial State

g(n) = 0 (no moves yet), h(n) = 4 (tiles 2, 8, 1, 6 are misplaced), so f(n) = 0 + 4 = 4

Count how many tiles are NOT in their goal position (skip the blank).

Position Initial Goal Match?
[0,0]21✗ Misplaced
[0,1]82✗ Misplaced
[0,2]33✓ Correct
[1,0]18✗ Misplaced
[1,1]6_✗ Misplaced
[1,2]44✓ Correct
[2,0]77✓ Correct
[2,1]_6— Blank (skip)
[2,2]55✓ Correct

Misplaced: tiles 2, 8, 1, 6 → h(n) = 4

Why it's admissible: Each misplaced tile needs at least one move to fix, so this count never overestimates the actual number of moves needed.

A search algorithm that finds the optimal (shortest) path from start to goal.

  1. Start: Put the initial state in the OPEN list
  2. Pick: Select the state with the smallest f(n) from OPEN
  3. Check: Is it the goal? If yes, we're done!
  4. Expand: Generate all neighbor states (slide tiles into the blank). Add new ones to OPEN. Move the current state to CLOSED.
  5. Repeat: Go back to step 2

A* is smart because it uses f(n) to prioritize the most promising states — balancing "moves already made" with "estimated moves remaining."

OPEN "To-Do List"

States waiting to be explored. We always pick the one with the lowest f(n) next.

CLOSED "Done List"

States already explored. Once a state is in CLOSED, we never revisit it.

Restaurant Analogy

OPEN is the waitlist — the host seats the highest-priority customer (lowest f(n)) first. Once served, they go to the served list (CLOSED). You never re-serve someone already done.

Current State

Goal State

Target: h(n) = 0
Step 0 of ?
What's happening?
Press Next Step (→) to begin the A* algorithm, or Solve Completely (Enter) to see the full solution animated.