Similar Problems

Similar Problems not available

Check If Move Is Legal - Leetcode Solution

Companies:

LeetCode:  Check If Move Is Legal Leetcode Solution

Difficulty: Medium

Topics: matrix array  

The problem "Check If Move Is Legal" on Leetcode asks us to determine if a move made by a player in a game of Tic-Tac-Toe is legal. Given the current state of the board, the player's symbol ('X' or 'O'), and the cell they want to place their symbol in, we need to check if the move is valid.

We can approach this problem by checking if the move is valid in each of the eight possible directions from the cell we want to place the symbol in. For example, if the player wants to place their symbol in cell (i, j), we can check if there are two matching symbols in a row in each of the following directions:

  1. Horizontally, at (i, j-1) and (i, j+1)
  2. Vertically, at (i-1, j) and (i+1, j)
  3. Diagonally left to right, at (i-1, j-1) and (i+1, j+1)
  4. Diagonally right to left, at (i-1, j+1) and (i+1, j-1)

If we find two matching symbols in a row in any of these directions, the move is legal. Otherwise, it is not.

Here's the code that implements this algorithm in Python:

class Solution:
    def checkMove(self, board: List[List[str]], r: int, c: int, color: str) -> bool:
        # Check each of the eight directions from the cell (r, c)
        for dr, dc in [(0, 1), (0, -1), (1, 0), (-1, 0), (-1, -1), (-1, 1), (1, -1), (1, 1)]:
            # Check if the move is legal in this direction
            if self.checkDirection(board, r, c, dr, dc, color):
                return True
        return False
        
    def checkDirection(self, board: List[List[str]], r: int, c: int, dr: int, dc: int, color: str) -> bool:
        # Check if there is at least one symbol of the opposite color in this direction
        opposite_color = 'X' if color == 'O' else 'O'
        i = r + dr
        j = c + dc
        while 0 <= i < 8 and 0 <= j < 8 and board[i][j] == opposite_color:
            i += dr
            j += dc
        # If we reach the end of the board or find a blank space, this direction is not valid
        if not (0 <= i < 8 and 0 <= j < 8 and board[i][j] == color):
            return False
        # Check if there are two matching symbols in a row
        i -= dr
        j -= dc
        while 0 <= i < 8 and 0 <= j < 8 and board[i][j] == opposite_color:
            i -= dr
            j -= dc
        return (0 <= i < 8 and 0 <= j < 8 and board[i][j] == color)

The checkMove function takes the board, the row and column of the cell to place the symbol in, and the color of the player's symbol as input. It checks each of the eight directions from the cell using the checkDirection function. If it finds a valid direction, it returns True. If none of the directions are valid, it returns False.

The checkDirection function takes the board, the row and column of the starting cell, the row and column increments for the direction to check (dr and dc), and the color of the player's symbol as input. It first checks if there is at least one symbol of the opposite color in this direction. If not, the direction is not valid. If there is, it checks if there are two matching symbols in a row in this direction. If so, the direction is valid.

Check If Move Is Legal Solution Code

1