File tree Expand file tree Collapse file tree 1 file changed +8
-3
lines changed Expand file tree Collapse file tree 1 file changed +8
-3
lines changed Original file line number Diff line number Diff line change @@ -26,15 +26,20 @@ def priority(self) -> int:
2626 return self .moves + self .manhattan ()
2727
2828 def manhattan (self ) -> int :
29- """Calculate Manhattan distance from current to goal state ."""
29+ """Calculate Manhattan distance using actual goal positions ."""
3030 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+
3134 for i in range (3 ):
3235 for j in range (3 ):
33- if self .board [i ][j ] != 0 :
34- x , y = divmod (self .board [i ][j ] - 1 , 3 )
36+ value = self .board [i ][j ]
37+ if value != 0 : # skip the empty tile
38+ x , y = goal_pos [value ]
3539 distance += abs (x - i ) + abs (y - j )
3640 return distance
3741
42+
3843 def is_goal (self ) -> bool :
3944 """Check if current state matches goal."""
4045 return self .board == self .goal
You can’t perform that action at this time.
0 commit comments