मैंने लंबे समय से कोशिश की है इसलिए एक विधि बनाएं जो मैनहट्टन की दूरी को दो गेम बोर्ड (2 डी सरणी) के बीच ढूंढती है, लेकिन मैं यह नहीं समझ सकता कि कैसे। मैं समझता हूं कि इसे क्या करना चाहिए: मान लें कि यदि टाइल एनआर 5 में self.board में निर्देशांक (2,2) हैं और दूसरे बोर्ड में टाइल 5 में निर्देशांक (1,1) हैं, तो यह x1-x2 + y1- लेता है y2 और हमें कुल 2 (चालों की संख्या) मिलते हैं। मुझे कोई फर्क नहीं पड़ता अगर हमें एक ऋणात्मक संख्या मिलती है क्योंकि हम पूर्ण का उपयोग कर रहे हैं।

मैं इसे "प्रिंट (आठगेमनोड ()। मैनहट्टन_डिस्टेंस ([[6,4,7], [8,5,0], [3,2,1]])) के साथ परीक्षण कर रहा हूं।

def manhattan_distance(self,other):
    '''
    Returns the sum of the number of moves a tile must do to move from where it is in self to
    where it is in the other board if there were no other tiles on the board
    :param other: the other board to compare to
    :return: the sum of the number of moves for each tile
    '''
    dist = 0
    for i in range (3):
        for j in range (3):
            if self.board[i][j] == 0: # If it is the empty cell
                continue
            else:
                targetX = #The x-coordinate of the "other" board
                targetY = #The y-coordinate of the "other" board
                dx = i - targetX #The x-distance to expected coordinate
                dy = j - targetY #The y-distance to expected coordinate
                dist += abs(dx) + abs(dy)

पूरा कोड:

DEFAULT_BOARD = [[1, 2, 3], [4, 5, 6], [7, 8, 0]]
'''
A default board if another board is not specified in the constructor
'''

def __init__(self, the_board = DEFAULT_BOARD):
    '''
    Makes a new node from an initial board
    :param the_board: the initial board
    '''
    if EightGameNode.legal_board(the_board):
        # if the board is legal go on
        self.board = the_board
    else:
        # otherwise use the default board
        self.board = EightGameNode.DEFAULT_BOARD
    self.empty = EightGameNode.get_empty(self.board)
    # set the empty space of the board        

def hamming_distance(self,other):
    '''
    Returns the number of tiles which are not correctly placed compared to other (which may be a goal)
    :param other: the other board to compare to
    :return: the number of tiles which are not correctly placed
    '''
    dist = 0
    for i in range (3):
        for j in range (3):
            if self.board[i][j] != other.board[i][j]:
                dist += 1
    return dist

**def manhattan_distance(self,other):**
    '''
    Returns the sum of the number of moves a tile must do to move from where it is in self to
    where it is in the other board if there were no other tiles on the board
    :param other: the other board to compare to
    :return: the sum of the number of moves for each tile
    '''
    dist = 0
    for i in range (3):
        for j in range (3):
            if self.board[i][j] == 0: # If it is the empty cell
                continue
            else:
                targetX = #The x-coordinate of the "other" board
                targetY = #The y-coordinate of the "other" board
                dx = i - targetX #The x-distance to expected coordinate
                dy = j - targetY #The y-distance to expected coordinate
                dist += abs(dx) + abs(dy) 
    return dist

मैं इसे प्रिंट के साथ परीक्षण कर रहा हूं (EightGameNode ()। manhattan_distance ([[६,४,७], [८,५,०], [३,२,१]]))

0
skylerious 13 सितंबर 2020, 14:54

1 उत्तर

सबसे बढ़िया उत्तर

मैनहट्टन एक रिवर्स लुकअप टेबल का उपयोग कर सकता था।

कोड

def manhattan_distance(self, other):
    ' Distance between where tiles are and where they should be  '
    
    # Generate reverse lookup table so we know where each item should be located
    lookup_loc = {}
    if i in range(3):
        for j in range(3):
            # other.board[i][j] is the value
            # which has location at (i, j)
            lookup_loc[other.board[i][j]] = (i, j)
          
    dist = 0
    for i in range(3):
        for j in range(3):
            if self.board[i][j] != other.board[i][j]:
                # Find correct location for value self.board[i][j]
                correct_loc = lookup_loc[self.board[i][j]]

                # Manhattan distance between current location (i, j) and
                # correction location
                dist += abs(correct_loc[0]-i) + abs(correct_loc[1] - j)
    return dist
1
DarrylG 13 सितंबर 2020, 15:08