You are simulating one move of a two-player board game played on an m x n grid. Each cell is 0 (empty), 1 (a piece of player 1) or 2 (a piece of player 2). Given the board and one move, in which player places a piece on cell (row, col), return the board after the move.
When a piece is placed, every straight run of the opponent's pieces that is enclosed between the new piece and another piece of the same player is flipped to the player's color. All eight directions must be checked: up, down, left, right and the four diagonals. If the move is illegal, return the original board unchanged.
Function Signature
def place_piece(board: list[list[int]], row: int, col: int, player: int) -> list[list[int]]:
Rules
-
Let
opponent = 3 - player
. In one direction, walk away from
(row, col)
one cell at a time. If the walk passes one or more consecutive
opponent
cells and then reaches a
player
cell, every
opponent
cell passed in that direction is flipped to
player
.
-
If the first cell in a direction is not an
opponent
cell, or if the walk reaches an empty cell or the edge of the board before reaching a
player
cell, nothing flips in that direction.
-
All flips are decided from the board as it was before the move. A piece flipped in one direction never causes further flips.
-
The move is legal only if
(row, col)
is empty and at least one opponent piece flips in at least one direction. A legal move places
player
at
(row, col)
and applies every flip.
-
If the move is illegal, return the board exactly as given: no piece is placed and nothing flips.
-
The returned grid has the same dimensions as the input. You may modify
board
in place or build a new grid; only the returned grid is checked.
Constraints
-
1 <= m, n <= 100
, where
m = len(board)
and
n = len(board[i])
for every row
i
-
Every cell is
0
,
1
or
2
.
-
0 <= row < m
and
0 <= col < n
-
player
is
1
or
2
.
-
The board may hold any arrangement of pieces; it does not have to be reachable in a real game.
Examples
Example 1
Input: board = [
[0, 0, 0, 0, 0],
[2, 1, 1, 0, 0],
[0, 0, 1, 1, 0],
[0, 2, 0, 0, 0]
]
row = 1, col = 3, player = 2
Output: [
[0, 0, 0, 0, 0],
[2, 2, 2, 2, 0],
[0, 0, 2, 1, 0],
[0, 2, 0, 0, 0]
]
To the left, the run (1, 2), (1, 1) of player 1 pieces ends at player 2's piece at (1, 0), so both flip. Down and to the left, (2, 2) is enclosed by player 2's piece at (3, 1) and flips. Straight down, (2, 3) is followed by the empty cell (3, 3), so it stays. Every other direction starts with an empty cell.
Example 2
Input: board = [
[0, 0, 0, 0, 0],
[2, 1, 1, 0, 0],
[0, 0, 1, 1, 0],
[0, 2, 0, 0, 0]
]
row = 3, col = 0, player = 1
Output: [
[0, 0, 0, 0, 0],
[2, 1, 1, 0, 0],
[0, 0, 1, 1, 0],
[0, 2, 0, 0, 0]
]
To the right, player 2's piece at (3, 1) is followed by the empty cell (3, 2), so it is not enclosed. Up and up-right start with empty cells, and the other directions leave the board at once. Nothing flips, so the move is illegal and the board is returned unchanged.
Example 3
Input: board = [
[0, 0, 0, 0, 0],
[2, 1, 1, 0, 0],
[0, 0, 1, 1, 0],
[0, 2, 0, 0, 0]
]
row = 1, col = 2, player = 2
Output: [
[0, 0, 0, 0, 0],
[2, 1, 1, 0, 0],
[0, 0, 1, 1, 0],
[0, 2, 0, 0, 0]
]
Cell (1, 2) is already occupied, so the move is illegal, even though (1, 1) lies between it and player 2's piece at (1, 0).