Free-Placement Line Game
MediumDesign ยท Databricks
Implement Game(rows, cols, k), a referee for two players, 1 and 2, claiming cells on a rows by cols board. Players may move in any order.
move(player, r, c) returns -1 and changes nothing if the cell is off the board, already taken, or the game is already won. Otherwise player takes the cell. If that cell now lies on at least k consecutive cells of player in a row, column or diagonal, the game is won and the call returns player. Otherwise it returns 0.
Example 1
- Input:
- Game(3, 4, 3), move(1, 0, 0), move(2, 1, 1), move(1, 0, 1), move(1, 0, 3), move(1, 0, 2)
- Output:
- [null, 0, 0, 0, 0, 1]
- Explanation
- The last move joins cells
(0, 0),(0, 1)and(0, 3)into a run of four, which is at leastk.
Example 2
- Input:
- Game(2, 2, 2), move(1, 0, 0), move(2, 0, 0), move(2, 2, 0), move(1, 1, 1), move(2, 0, 1)
- Output:
- [null, 0, -1, -1, 1, -1]
Example 3
- Input:
- Game(5, 5, 3), move(2, 0, 4), move(2, 2, 2), move(1, 0, 0), move(2, 1, 3)
- Output:
- [null, 0, 0, 0, 2]
Constraints
1 <= rows, cols <= 10001 <= k <= max(rows, cols)playeris1or2-1000 <= r, c <= 1000- At most
10^4calls are made