Leetcode 1958: Check if Move is Legal

grid47
grid47
Exploring patterns and algorithms
Apr 25, 2024 8 min read

You are given a game board represented as an 8x8 grid, where each cell can either be free (’.’), white (‘W’), or black (‘B’). The objective is to determine if placing your piece (either ‘W’ or ‘B’) at a specified location will create a valid ‘good line’ where the line consists of three or more cells. A good line is defined as a line (horizontal, vertical, or diagonal) with endpoints of the same color and any cells between them of the opposite color.
Problem
Approach
Steps
Complexity
Input: The input consists of a board represented as an 8x8 grid, the row and column indices of the cell where you wish to place your piece, and the color of the piece you are playing as ('W' or 'B').
Example: board = [[".", ".", ".", "B", ".", ".", ".", "."], [".", ".", ".", "W", ".", ".", ".", "."], [".", ".", ".", "W", ".", ".", ".", "."], [".", ".", ".", "W", ".", ".", ".", "."], ["W", "B", "B", ".", "W", "W", "W", "B"], [".", ".", ".", "B", ".", ".", ".", "."], [".", ".", ".", "B", ".", ".", ".", "."], [".", ".", ".", "W", ".", ".", ".", "."]], rMove = 4, cMove = 3, color = "B"
Constraints:
• board.length == board[r].length == 8
• 0 <= rMove, cMove < 8
• board[rMove][cMove] == '.'
• color is either 'B' or 'W'
Output: Return true if placing your piece at the specified location will result in a valid move that creates a good line. Otherwise, return false.
Example: Output: true
Constraints:
• The board must contain only '.' for the move location, and the color must be either 'B' or 'W'.
Goal: The goal is to check if placing a piece at the specified location results in a good line. A good line requires that after placing the piece, the endpoints of the line are the same color, and the cells in the middle are the opposite color.
Steps:
• Step 1: Start by examining the 8 possible directions (horizontal, vertical, and diagonal).
• Step 2: For each direction, look for a potential good line starting from the specified move position and check the surrounding cells.
• Step 3: If a good line is formed in any direction, return true. Otherwise, return false.
Goal: The problem must be solved within the constraints where the board is fixed at 8x8 and the piece is being placed at a free cell.
Steps:
• The board is always 8x8.
• The cell where the piece is being placed is always free ('.').
Assumptions:
• The input will always provide a valid 8x8 grid with the specified row and column indices for the move.
Input: Input: board = [[".", ".", ".", "B", ".", ".", ".", "."], [".", ".", ".", "W", ".", ".", ".", "."], [".", ".", ".", "W", ".", ".", ".", "."], [".", ".", ".", "W", ".", ".", ".", "."], ["W", "B", "B", ".", "W", "W", "W", "B"], [".", ".", ".", "B", ".", ".", ".", "."], [".", ".", ".", "B", ".", ".", ".", "."], [".", ".", ".", "W", ".", ".", ".", "."]], rMove = 4, cMove = 3, color = "B"
Explanation: In this case, placing a black piece at the location (4, 3) will form valid lines in both the vertical and diagonal directions, hence the result is true.

Input: Input: board = [[".", ".", ".", ".", ".", ".", ".", "."], [".", "B", ".", ".", "W", ".", ".", "."], [".", ".", "W", ".", ".", ".", "."], [".", ".", ".", "W", "B", ".", ".", "."], [".", ".", ".", ".", ".", ".", ".", "."], [".", ".", ".", ".", "B", "W", ".", "."], [".", ".", ".", ".", ".", "W", ".", "."], [".", ".", ".", ".", ".", ".", "B", "."]], rMove = 4, cMove = 4, color = "W"
Explanation: In this case, no good lines are formed with the white piece at (4, 4), so the result is false.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus