Question
Design A tic-tac-toe game this is played between and the players on a n x n grid.
Assume the following rules:
- A move is guaranteed to being valid and is placed on an empty block.
- Once a winning condition is reached, no more moves is allowed.
- A player succeeds in placing n of their marks in a horizontal, vertical, or diagonal row wins the game.
Example:
N = 3, assume that player 1 is ' X ' and Player 2 is ' O ' in the board. TicTacToe toe = new TicTacToe (3); Toe.move (0, 0, 1); -Returns 0 (no one wins) | x| | | | | | | Player 1 makes a move at (0, 0). | | | |toe.move (0, 2, 2); -Returns 0 (no one wins) | x| | o| | | | | Player 2 makes a move at (0, 2). | | | |toe.move (2, 2, 1); -Returns 0 (no one wins) | x| | o| | | | | Player 1 makes a move at (2, 2). | | X|toe.move (1, 1, 2); -Returns 0 (no one wins) | x| | o| | | o| | Player 2 makes a move at (1, 1). | | X|toe.move (2, 0, 1); -Returns 0 (no one wins) | x| | o| | | o| | Player 1 makes a move at (2, 0). x| | X|toe.move (1, 0, 2); -Returns 0 (no one wins) | x| | o| | o| o| | Player 2 makes a move at (1, 0). x| | X|toe.move (2, 1, 1); -Returns 1 (Player 1 wins) | x| | o| | o| o| | Player 1 makes a move at (2, 1). X| X| X|
Follow up:
Could do better than O (N2) per move()
operation?
Hint:
- Could you trade extra space such that
move()
operation can be do in O (1)?
- You need arrays:int rows[n], int cols[n], plus, variables:diagonal, anti_diagonal.
Answer
For a move operation, the simple and rude way is to traverse the entire two-dimensional array, checking each row for each column and the diagonal. Time, space complexity of O (n^2)
We can consider the row, column, and diagonal separately.
For a row of row[i], if there is a player in this row 1 down, then +1. If there is a player 2 down, then-1. So if this line is all for Player 1, row[i]=n. Player 1 wins.
Similar Case for Col and diagonal, anti diagonal.
Time complexity O (1) Space complexity O (n)
1 Public classTicTacToe {2 3 Private int[] row;4 Private int[] col;5 Private intDiagonal;6 Private intanti_diagonal;7 Private intsize;8 /**Initialize your data structure here.*/9 PublicTicTacToe (intN) {TenSize =N; Onerow =New int[n]; ACol =New int[n]; -Arrays.fill (Row, 0); -Arrays.fill (col, 0); theDiagonal = 0; -anti_diagonal = 0; - } - + /**player {player} makes a move at ({row}, {col}). - @paramrow The row of the board. + @paramcol The column of the board. A @paramplayer The player, can be either 1 or 2. at @returnthe current winning condition, can be either: - 0:no one wins. - 1:player 1 wins. - 2:player 2 wins.*/ - Public intMoveintRowintColintplayer) { - intChange = Player = = 1? 1:-1; in This. row[row] + =Change ; - This. col[col] + =Change ; to if(Row = =Col) { +Diagonal + =Change ; - } the if(Row = = (size-col-1)) { *Anti_diagonal + =Change ; $ }Panax Notoginseng if( This. row[row] = = Size | | This. col[col] = = Size | | Diagonal = = Size | | Anti_diagonal = =size) { - return1; the } + if( This. row[row] = =-size | | This. col[col] = =-size | | Diagonal = =-size | | Anti_diagonal = =-size) { A return2; the } + return0; - } $ } $ - /** - * Your TicTacToe object would be instantiated and called as such: the * TicTacToe obj = new TicTacToe (n); - * int param_1 = Obj.move (row,col,player);Wuyi */
Design Tic-Tac-Toe Solutions