Four in a Row
Two players take turns dropping discs into an upright board with rows rows and cols columns; player 1 moves first. drops[i] is the column (counted from 0, left to right) chosen on move number i + 1, and the disc falls to the lowest empty cell of that column. A player wins as soon as four of their discs lie next to each other in a line: along a row, along a column, or along either diagonal. Return [player, move]: the winning player (1 or 2) and the number of the move that completed the line. Moves after the winning one are ignored. If nobody gets four in a line, return [0, 0]. No disc is ever dropped into a full column.
Example 1
Player 1 drops into columns 3, 4, 5 and 6 on moves 1, 3, 5 and 7, filling four neighbouring cells of the bottom row.
Example 2
Player 2 drops into column 1 on moves 2, 4, 6 and 8 and stacks four discs in that column. Player 1 only ever has three in a line.
Constraints
1 <= rows, cols <= 80 <= len(drops) <= rows * cols0 <= drops[i] < cols- no column number appears more than
rowstimes indrops
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Grids problems
- Life-Like GenerationsMedium
- Minutes to BurnMedium
- Paths Around RocksMedium
- Ramp DropMedium
- Settle the SandpileMedium
- Slide and MergeMedium
- Snake GameMedium
- Spiral FillMedium