Neuron 101

Solving by search / Heuristic function

Türkçe

Plate17

Unit 2 · Topic 6 of 10

Heuristic function

Medium, 7 min, 9 steps

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

Step 1 of 9 Estimate

An estimate in every cell

The numbers are h: the Manhattan distance from the cell to the goal, ignoring walls and mud, the sum of horizontal and vertical steps. The real cost cannot be smaller.

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

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