Apply a Single Capture Move on a Two-Colour Grid
Arrays & HashingAuto-graded · 29 test casesLast updated
Apply one move on a grid: validate the target cell, find enclosed opponent runs in eight directions, and flip them together, or return the grid unchanged if no capture is possible.
Capture Move on a Grid
A rectangular grid holds stones for two players. Each cell contains 0 (vacant), 1 (player 1's stone) or 2 (player 2's stone). One player now drops a stone on a chosen cell, and you must produce the grid that results.
Write a function that receives the grid, the target cell (row, col) and the acting player, and returns the grid after the move.
How a move works
Define enemy = 3 - player. From the target cell, look along each of the eight compass lines: horizontal, vertical and the four diagonals. For one line:
- Step away from the target one cell at a time.
- Skip over consecutive
enemystones. These form the run. - If the run contains at least one stone and the cell right after it is on the grid and holds a
playerstone, the whole run is captured and becomesplayer. - Otherwise nothing is captured along that line. This includes the case where the first neighbour is not an enemy stone, and the case where the run ends at a vacant cell or at the grid border.
Legality
- The move is legal only if the target cell is vacant and at least one enemy stone is captured on at least one line.
- For a legal move, put a
playerstone on the target and flip all captured stones. - For an illegal move, return the grid exactly as it was given. Nothing is placed and nothing flips.
Important detail
Every capture is determined using the grid as it stood before the move. A stone that gets flipped along one line never creates or extends a capture along another line.
You may either mutate the input or build a new grid. Only the returned grid is checked, and it must have the same dimensions as the input.
Constraints
1 <= m, n <= 100, wheremis the number of rows andnthe length of every row- Each cell is
0,1or2 0 <= row < mand0 <= col < nplayeris1or2- The arrangement of stones can be arbitrary; it need not come from a real game
Examples
Example 1
Input: apply_capture_move([[0, 0, 0, 0, 0], [2, 1, 1, 0, 0], [0, 0, 1, 1, 0], [0, 2, 0, 0, 0]], 1, 3, 2)
Output: [[0, 0, 0, 0, 0], [2, 2, 2, 2, 0], [0, 0, 2, 1, 0], [0, 2, 0, 0, 0]]
Example 2
Input: apply_capture_move([[0, 0, 0, 0, 0], [2, 1, 1, 0, 0], [0, 0, 1, 1, 0], [0, 2, 0, 0, 0]], 3, 0, 1)
Output: [[0, 0, 0, 0, 0], [2, 1, 1, 0, 0], [0, 0, 1, 1, 0], [0, 2, 0, 0, 0]]
Example 3
Input: apply_capture_move([[0, 0, 0, 0, 0], [2, 1, 1, 0, 0], [0, 0, 1, 1, 0], [0, 2, 0, 0, 0]], 1, 2, 2)
Output: [[0, 0, 0, 0, 0], [2, 1, 1, 0, 0], [0, 0, 1, 1, 0], [0, 2, 0, 0, 0]]
Hints
Hint 1
Handle the easy rejection first: if the target cell is not vacant, nothing can happen.
Hint 2
For each of the 8 direction pairs (dr, dc) other than (0, 0), walk while you see enemy stones, remember them, and then check the cell where the walk stopped, including a bounds check before reading it.
Hint 3
Do not write to the grid while scanning. Gather all stones to flip first, return the original if there are none, and only then place the new stone and flip everything.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
This is a straightforward simulation split into two phases: decide, then apply.
- If the target cell is not vacant, the move is illegal, so return the grid as is.
- For each of the eight direction vectors
(dr, dc)withdr, dcin{-1, 0, 1}(excluding(0, 0)), walk outward from the target:- Collect consecutive enemy stones into a run.
- Stop at the first cell that is off the grid or not an enemy stone.
- If the run is non-empty and the stopping cell is on the grid and holds a
playerstone, the run is captured. Add it to a list of cells to flip.
- If the list is empty, the move is illegal, so return the grid unchanged.
- Otherwise copy the grid, place the stone at the target, and set every listed cell to
player.
Why it is correct
- Per-line rule. A walk only passes over enemy stones. It stops at the border, a vacant cell, or a
playerstone. Only the last case encloses the run, and the non-empty check covers the case where the first neighbour is not an enemy. - Independence of lines. No writes happen during scanning, so every line is judged against the original grid. A flipped stone therefore cannot trigger further flips.
- Legality. The move is legal exactly when the target is vacant and the flip list is non-empty. Both illegal paths return before any change is made.
- Straight lines only. A fixed
(dr, dc)step keeps each walk on one line. - Bounds. The cell where the walk stopped is bounds-checked explicitly before it is read. This also prevents Python's negative indexing from silently reading the wrong cell.
Complexity
- Time: each of the 8 walks covers at most
max(m, n)cells, so scanning is O(m + n). Copying on a legal move is O(m * n), which dominates and is at most 10^4. - Space: O(m + n) for the flip list, plus O(m * n) for the copy on a legal move.
Reference solution
from typing import List
def apply_capture_move(grid: List[List[int]], row: int, col: int, player: int) -> List[List[int]]:
m, n = len(grid), len(grid[0])
if grid[row][col] != 0:
return grid
enemy = 3 - player
to_flip = []
for dr in (-1, 0, 1):
for dc in (-1, 0, 1):
if dr == 0 and dc == 0:
continue
r, c = row + dr, col + dc
run = []
while 0 <= r < m and 0 <= c < n and grid[r][c] == enemy:
run.append((r, c))
r += dr
c += dc
if run and 0 <= r < m and 0 <= c < n and grid[r][c] == player:
to_flip.extend(run)
if not to_flip:
return grid
result = [list(line) for line in grid]
result[row][col] = player
for r, c in to_flip:
result[r][c] = player
return result