Per-Move Win Tracker for a Square Noughts-and-Crosses Grid
Arrays & HashingAuto-graded · 25 test casesLast updated
Replay a sequence of marks on an n-by-n grid and report after each one whether the mover has completed a row, column or diagonal, using per-line counters.
Square Grid Game Replay
Two players, numbered 1 and 2, take part in a game on a square grid with n rows and n columns. Each turn, a player puts their mark in a cell that is currently unoccupied. A player wins the moment one of the following is made up entirely of their own marks:
- any full row,
- any full column,
- the main diagonal,
- the anti-diagonal.
You are given the grid size and the list of placements in the order they happen. After each placement, report whether someone has won.
Task
Implement a function that takes n and moves, and returns a list with one integer per placement, in the same order.
Each entry moves[i] = [row, col, player] says that player marks the cell at 0-indexed position (row, col).
Definitions and rules
- The main diagonal consists of the cells
(i, i)for0 <= i < n. - The anti-diagonal consists of the cells
(i, n - 1 - i)for0 <= i < n. - The value reported after a placement is the number of the player who has won, or
0if there is no winner yet. Only the player who just moved can have just won. - Apply the placements exactly in the order given. Players are not required to alternate, so do not check whose turn it is.
- When
nis odd, the center cell belongs to both diagonals. - The game stops at the first winning placement, so only the final entry of the result can be nonzero.
- Return exactly one value per placement.
Constraints
2 <= n <= 1001 <= len(moves) <= n * n0 <= row, col < n- No two placements use the same cell.
playeris either1or2.- No placement follows a winning placement.
Examples
Example 1
Input: grid_game_results(3, [[1, 1, 1], [0, 0, 2], [0, 2, 1], [2, 0, 2], [1, 0, 1], [2, 2, 2], [1, 2, 1]])
Output: [0, 0, 0, 0, 0, 0, 1]
Example 2
Input: grid_game_results(2, [[0, 1, 2], [1, 1, 1], [1, 0, 2]])
Output: [0, 0, 2]
Example 3
Input: grid_game_results(4, [[0, 0, 2], [0, 1, 1], [1, 1, 2], [0, 2, 1], [2, 2, 2]])
Output: [0, 0, 0, 0, 0]
Hints
Hint 1
After a placement at (r, c), which lines could possibly have become complete? Rescanning the whole grid is unnecessary.
Hint 2
For each line, keep how many marks each player owns on it. A line is full for a player exactly when that player's count equals n, because cells are never reused.
Hint 3
A cell can sit on the main diagonal (r == c), the anti-diagonal (r + c == n - 1), both (the center when n is odd), or neither. Test the two diagonals with separate if statements.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Rescanning the grid after every placement would cost O(n) per move. Instead, notice that a placement at (r, c) can only complete lines that pass through that cell: row r, column c, the main diagonal if r == c, and the anti-diagonal if r + c == n - 1.
So we keep, for every row, every column and both diagonals, a count of marks for each player. When player p places a mark, we increment p's counter on each line through the cell. If any of those counters reaches n, player p has completed that line and the result for this move is p. Otherwise it is 0.
The counters are lists of length 3 so that players 1 and 2 can index them directly (index 0 is unused).
Why it is correct
- A line has exactly
ncells and no cell is used twice, so a player hasnmarks on a line if and only if every cell of that line is theirs. - Lines not passing through the new cell are unchanged. Since no move follows a winning move, none of them was complete before, so none is complete now.
- Only the mover's counters change, so only the mover can have just won.
- The center cell (odd
n) is on both diagonals. Using two independentifstatements, rather thanif/elif, updates both counters. - The check is "count equals
n", so partial lines (such as three of four diagonal cells) do not count, and lines with mixed marks never reachnfor either player. - Turn order is never examined, so non-alternating input is handled naturally.
Complexity
- Time:
O(n + m)formmoves (setup plus constant work per move), which is at mostO(n^2). - Space:
O(n)for counters plusO(m)for the output; the full grid is never stored.
Reference solution
def grid_game_results(n, moves):
# Per-line counters, indexed by player (index 0 unused).
row_cnt = [[0, 0, 0] for _ in range(n)]
col_cnt = [[0, 0, 0] for _ in range(n)]
main_cnt = [0, 0, 0]
anti_cnt = [0, 0, 0]
results = []
for r, c, p in moves:
row_cnt[r][p] += 1
col_cnt[c][p] += 1
won = row_cnt[r][p] == n or col_cnt[c][p] == n
if r == c:
main_cnt[p] += 1
if main_cnt[p] == n:
won = True
if r + c == n - 1:
anti_cnt[p] += 1
if anti_cnt[p] == n:
won = True
results.append(p if won else 0)
return results