Design Tic-Tac-Toe Problem
Description
LeetCode Problem 348.
Assume the following rules are for the tic-tac-toe game on an n x n board between two players:
- A move is guaranteed to be valid and is placed on an empty block.
- Once a winning condition is reached, no more moves are allowed.
- A player who succeeds in placing
nof their marks in a horizontal, vertical, or diagonal row wins the game.
Implement the TicTacToe class:
TicTacToe(int n)Initializes the object the size of the boardn.int move(int row, int col, int player)Indicates that the player with idplayerplays at the cell(row, col)of the board. The move is guaranteed to be a valid move, and the two players alternate in making moves. Return0if there is no winner after the move,1if player 1 is the winner after the move, or2if player 2 is the winner after the move.
Example 1:
Input
["TicTacToe", "move", "move", "move", "move", "move", "move", "move"]
[[3], [0, 0, 1], [0, 2, 2], [2, 2, 1], [1, 1, 2], [2, 0, 1], [1, 0, 2], [2, 1, 1]]
Output
[null, 0, 0, 0, 0, 0, 0, 1]
Explanation
TicTacToe ticTacToe = new TicTacToe(3);
Assume that player 1 is "X" and player 2 is "O" in the board.
ticTacToe.move(0, 0, 1); // return 0 (no one wins)
|X| | |
| | | | // Player 1 makes a move at (0, 0).
| | | |
ticTacToe.move(0, 2, 2); // return 0 (no one wins)
|X| |O|
| | | | // Player 2 makes a move at (0, 2).
| | | |
ticTacToe.move(2, 2, 1); // return 0 (no one wins)
|X| |O|
| | | | // Player 1 makes a move at (2, 2).
| | |X|
ticTacToe.move(1, 1, 2); // return 0 (no one wins)
|X| |O|
| |O| | // Player 2 makes a move at (1, 1).
| | |X|
ticTacToe.move(2, 0, 1); // return 0 (no one wins)
|X| |O|
| |O| | // Player 1 makes a move at (2, 0).
|X| |X|
ticTacToe.move(1, 0, 2); // return 0 (no one wins)
|X| |O|
|O|O| | // Player 2 makes a move at (1, 0).
|X| |X|
ticTacToe.move(2, 1, 1); // return 1 (player 1 wins)
|X| |O|
|O|O| | // Player 1 makes a move at (2, 1).
|X|X|X|
Constraints:
2 <= n <= 100- player is
1or2. 0 <= row, col < n(row, col)are unique for each different call tomove.- At most
n^2calls will be made tomove.
Follow-up: Could you do better than O(n^2) per move() operation?
Solution
Approach: Count Marks per Line
Scanning the whole board after every move costs O(n2). We don't need to: a move at (row, col) can only complete one of at most four lines, namely its row, its column, and the two diagonals if the cell lies on them. A player wins on a line once they have n marks on it, so all we need is a count of each player's marks on every line.
Give each player an array of 2n + 2 counters:
0 .. n-1for the rows,n .. 2n-1for the columns,2nfor the main diagonal (cells withrow == col),2n + 1for the anti-diagonal (cells withrow + col == n - 1).
For a move, add one to the player's counter for the row and the column, and to each diagonal the cell is on. If any of those counters is now n, the player has filled that line and wins. Otherwise return 0.
This works because moves are always valid: a cell is never played twice, so a counter of n really means n different cells of that line. The counts are kept per player, so a line with marks from both players can never reach n for either one. The center of an odd board lies on both diagonals, so both of those counters go up.
Complexity: O(1) time per move. O(n) space for the counters.
class TicTacToe:
def __init__(self, n: int):
self.n = n
# counts[p]: marks of player p+1 in each row (0..n-1), each column (n..2n-1),
# the main diagonal (2n) and the anti-diagonal (2n+1)
self.counts = [[0] * (2 * n + 2) for _ in range(2)]
def move(self, row: int, col: int, player: int) -> int:
n = self.n
cur = self.counts[player - 1]
lines = [row, n + col]
if row == col:
lines.append(2 * n)
if row + col == n - 1:
lines.append(2 * n + 1)
for line in lines:
cur[line] += 1
return player if any(cur[line] == n for line in lines) else 0