MCPcopy Create free account
hub / github.com/geekcomputers/Python / manhattan

Method manhattan

8_puzzle.py:28–40  ·  view source on GitHub ↗

Calculate Manhattan distance using actual goal positions.

(self)

Source from the content-addressed store, hash-verified

26 return self.moves + self.manhattan()
27
28 def manhattan(self) -> int:
29 """Calculate Manhattan distance using actual goal positions."""
30 distance = 0
31 # Create a lookup table for goal tile positions
32 goal_pos = {self.goal[i][j]: (i, j) for i in range(3) for j in range(3)}
33
34 for i in range(3):
35 for j in range(3):
36 value = self.board[i][j]
37 if value != 0: # skip the empty tile
38 x, y = goal_pos[value]
39 distance += abs(x - i) + abs(y - j)
40 return distance
41
42
43 def is_goal(self) -> bool:

Callers 1

priorityMethod · 0.95

Calls

no outgoing calls

Tested by

no test coverage detected