Count Pieces After Othello Moves
Updated
Problem statement
Black and White place pieces on an n x n board, as in the game Othello (also called Reversi). You are given the starting board and a list of moves. Apply the moves in order, then return how many black pieces and how many white pieces are on the board.
board is a list of n strings, each of length n. board[y][x] is the cell in row y and column x. 'B' is a black piece, 'W' is a white piece, and '.' is an empty cell. Column 0 is on the left and row 0 is at the top, so x grows to the right and y grows toward the bottom.
Each move is a string "P X Y". P is the color of the new piece, B or W. X is its column and Y is its row. The players do not have to take turns, so the same color can move several times in a row.
When a piece is placed, check each of the 8 directions from it: left, right, up, down, and the 4 diagonals.
- Start at the cell next to the new piece and walk outward while the cells hold pieces of the other color.
- If at least one such piece was passed and the walk stops on a piece of the mover's color, every piece passed flips to the mover's color.
- If the walk stops on an empty cell or at the edge of the board, nothing flips in that direction.
- Flipped pieces do not cause any more flips.
- A move is placed even when it flips nothing.
Every move goes on a cell that is empty at that moment.
Return an array [black, white] with the number of black pieces and the number of white pieces after all the moves.
Function
countPieces(board: String[], moves: String[]) → int[]Examples
Example 1
board = ["....",".WB.",".BW.","...."]moves = ["B 3 2","B 0 0"]return = [6,0]The board starts with 2 black and 2 white pieces.
"B 3 2"puts a black piece in column3, row2. To its left are the white piece in column2and then a black piece in column1, so that white piece flips. No other direction has a white piece next to the new piece. Now there are 4 black and 1 white."B 0 0"is Black again, because the players do not have to take turns. The piece goes in the top-left corner. Down and to the right are the white piece in column1, row1and then the black piece in column2, row2, which the first move flipped. So that white piece flips too. Now there are 6 black and 0 white.
Example 2
board = ["B.B.B",".WWW.","BW.WB",".WWW.","B.B.B"]moves = ["B 2 2","W 1 0"]return = [17,1]The board starts with 8 black and 8 white pieces.
"B 2 2"puts a black piece in the center. In each of the 8 directions there is one white piece next to it and then a black piece, so all 8 white pieces flip. Now there are 17 black and 0 white."W 1 0"puts a white piece in column1of the top row. Every line from it reaches an empty cell or the edge of the board without meeting another white piece, so nothing flips. The piece is still placed, so the answer is 17 black and 1 white.
Example 3
board = [".BW.","....","BW.W","B.W."]moves = ["W 0 0","B 2 2","B 3 3"]return = [5,5]The board starts with 3 black and 4 white pieces.
"W 0 0": to the right are a black piece and then a white piece, so the black piece in column1, row0flips. Now there are 2 black and 6 white."B 2 2": to the left are a white piece and then a black piece, so the white piece in column1, row2flips. To the right, the white piece in column3is followed by the edge of the board. Below, the white piece in row3is also followed by the edge. Neither of those flips. Now there are 4 black and 5 white."B 3 3": to the left, the white piece in column2is followed by an empty cell. Above, the white piece in row2is also followed by an empty cell. So nothing flips, and the answer is 5 black and 5 white.
Constraints
1 <= n <= 50, wheren == board.lengthboard[i].length == n- Each
board[i][j]is'B','W', or'.'. 1 <= moves.length <= 2500- Each
moves[i]has the form"P X Y"with single spaces, wherePisBorW, andXandYare whole numbers from0ton - 1written without leading zeros. - Each move's cell is empty at the moment that move is made.
More Hudson River Trading Online Assessment questions
- Count Palindrome Addition ChoicesOA · Seen Nov 2026
- Count Multiples of Three with Two SevensOA · Seen Oct 2026
- Recursively Expanded Shell Command CountsOA · Seen Oct 2026
- Rotate Matrix Regions Around the DiagonalsOA · Seen Oct 2026
- Maximum Diamond SumOA · Seen Oct 2026
- Smallest Most-Lit Street PositionOA · Seen Oct 2026
- Rearrange String by Alternating EndsOA · Seen Sep 2026
- Most Items Within Budget from a Start IndexOA · Seen Sep 2026
- Count Sawtooth SubarraysOA · Seen Aug 2026
- Backtick Identifier ConverterOA · Seen Jun 2026
Similar questions at other companies
- Wind-Blown Leaves Remaining in a GridAbridge · OAAbridge interview questions
- Trapped Characters in a GridAdobe · OAAdobe interview questions
- Tic-Tac-Toe Board StatusPinecone · OA
- Robot Navigation Around LasersRoblox · OARoblox interview questions
- Apply a Reversi MoveAmazon · OA
- First Completed Mingo LineEpic · OAEpic interview questions
- Rotate a Square Matrix ClockwiseVisa · OAVisa interview questions
- Sort Concentric Matrix Borders ClockwiseByteDance · OAByteDance interview questions
- Sparse Valid Conv2D From ScratchNVIDIA · OANVIDIA interview questions
- Apply Matrix OperationsCapital One · OACapital One interview questions