Problem K
Gravity Grid
Alice and Bob are playing a generalized version of Connect Four. In their game, the board consists of $w$ columns of height $h$ and the goal is to be the first player to complete a row of $k$ tiles of equal colour, either vertically, horizontally or diagonally. The two players alternate dropping their tiles into one of the columns, with Alice using red tiles and going first and Bob using yellow tiles and going second. Once a tile is dropped, it falls down to the bottommost available position, making that position no longer available. Once a column has $h$ tiles in it, it becomes full and the players can no longer drop their tiles there.
As Alice and Bob found it quite challenging to keep track of the winning condition, they just kept playing until the board was completely filled with tiles. They recorded a log of the moves they made and asked you to tell them who won the game, and on what turn they did so. If neither player managed to complete a row, the game ends in a draw, so report that instead.
Input
The input consists of:
-
One line with three integers $h$, $w$ and $k$ ($h, w \ge 1, h \cdot w \le 250\, 000, 1 \le k \le \max (h,w)$). The columns are numbered from $1$ to $w$.
-
One line with $h\cdot w$ integers $a_1,\dots ,a_{h\cdot w}$ ($1 \le a_ i \le w$ for each $i$), where $a_ i$ is the index of the column that the $i$th tile was dropped in. Odd indices correspond to Alice’s moves and even indices correspond to Bob’s moves. Each column appears exactly $h$ times in this list.
Output
Output the winner of the game (A for Alice or B for Bob), followed by the number of moves needed to decide the winner. If the game ends in a draw, output D instead.
Sample Input 1 | Sample Output 1 |
---|---|
4 3 2 1 1 2 3 3 2 2 1 1 2 3 3 |
A 3 |
Sample Input 2 | Sample Output 2 |
---|---|
4 3 3 1 1 2 3 3 2 2 1 1 2 3 3 |
B 8 |
Sample Input 3 | Sample Output 3 |
---|---|
4 3 4 1 1 2 3 3 2 2 1 1 2 3 3 |
D |