NewThe Learning module is now liveSee what's new

(This question is a variation of the LeetCode question 1958. Check if Move is Legal . If you haven't completed that question yet, it is recommended to solve it first.)

You are simulating a single move in the board game Reversi on an n x n board.

Each cell in board contains 'B', 'W', or '.', where 'B' represents a black piece, 'W' represents a white piece, and '.' represents an empty cell. You are also given row, col, and player, and you need to return the board after trying to place player at position (row, col).

After the new piece is placed, opponent pieces may be flipped. A flip happens when there is a sequence of one or more consecutive opponent pieces in a single straight line starting from the new piece, and another piece of the current player immediately follows that sequence. This can happen in any of the 8 directions: horizontal, vertical, or diagonal. Every opponent piece in each valid bounded sequence must be flipped to the current player's color.

Your task is to return the new board after the potential movement. If the move is out of bounds, the target cell is not empty, or the move flips no pieces, return board unchanged.

Constraints:

  • 1 ≤ n ≤ 100
  • board.length = board[i].length = n
  • board[i][j] is 'B', 'W', or '.'
  • player is either 'B' or 'W'

Example 1:

Input: board = ["....", ".WB.", ".BW.", "...."], row = 1, col = 0, player = "B" Output: ["....", "BBB.", ".BW.", "...."] Explanation: Placing a black piece at (1, 0) creates a horizontal line where the white piece at (1, 1) is directly between two black pieces, so it is flipped. No other direction forms a valid bounded sequence.

Example 2:

Input: board = ["...", ".W.", ".B."], row = 0, col = 1, player = "B" Output: [".B.", ".B.", ".B."]

Example 3:

Input: board = ["...", ".W.", "..B"], row = 0, col = 0, player = "B" Output: ["B..", ".B.", "..B"]


Accepted
620/1.6k
Acceptance Rate38.3%
Test Cases
Input
board =
row =
col =
player =
Output
Expected