Neuron 101

Solving by search / A*

Türkçe

Plate18

Unit 2 · Topic 7 of 10

A*

Medium, 8 min, 9 steps

S
Keyboard: left and right arrows change the step, space plays and pauses.

Step 1 of 9 Maze

The maze

S is the start and the flag is the goal. Black blocks are walls, brown cells are mud: entering mud costs 3, entering a plain cell costs 1. A* will look both back and ahead.

Tap a part of the scene to see its details here.

Open in Colab
  1. import heapq
  2. def astar(harita, S, G):
  3. kuyruk = [(h(S, G), 0, S)]
  4. g = {S: 0}
  5. while kuyruk:
  6. _, gx, x = heapq.heappop(
  7. kuyruk)
  8. if x == G:
  9. return gx
  10. for k in komsular(harita, x):
  11. yg = gx + maliyet(k)
  12. if yg < g.get(k, 1e9):
  13. g[k] = yg
  14. f = yg + h(k, G)
  15. heapq.heappush(
  16. kuyruk,
  17. (f, yg, k))
acilan
0
sinir
1
yolUzunlugu
0
maliyet
0