“This is the 12th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”
describe
Tic-tac-toe is played by two players A and B on a 3 x 3 grid. The rules of Tic-Tac-Toe are:
- Players take turns placing characters into empty squares ‘ ‘.
- The first player A always places ‘X’ characters, while the second player B always places ‘O’ characters.
- ‘X’ and ‘O’ characters are always placed into empty squares, never on filled ones.
- The game ends when there are three of the same (non-empty) character filling any row, column, or diagonal.
- The game also ends if all squares are non-empty.
- No more moves can be played if the game is over.
Given a 2D integer array moves where moves[i] = [rowi, coli] indicates that the ith move will be played on grid[rowi][coli]. return the winner of the game if it exists (A or B). In case the game ends in a draw return “Draw”. If there are still movements to play return “Pending”.
You can assume that moves is valid (i.e., it follows the rules of Tic-Tac-Toe), the grid is initially empty, and A will play first.
Example 1:
Input: moves = [[0,0],[2,0],[1,1],[2,1]] Output: "A" Explanation: A wins, they always play first.Copy the code
Example 2:
Input: moves = [[0, 0], [1, 1], [0, 1], [0, 2], [1, 0], [2, 0]] Output: "B" Explanation: B wins.Copy the code
Example 3:
Input: moves = [[0, 0], [1, 1], [2, 0], [1, 0], [1, 2], [2, 1], [0, 1], [0, 2], [2]] Output: "Draw" Explanation: The game ends in a draw since there are no moves to make.Copy the code
Example 4:
Input: moves = [[0,0],[1,1]]
Output: "Pending"
Explanation: The game has not finished yet.
Copy the code
Note:
1 <= moves.length <= 9
moves[i].length == 2
0 <= rowi, coli <= 2
There are no repeated elements on moves.
moves follow the rules of tic tac toe.
Copy the code
parsing
Two players play a game called Tic-Tac-toe on a 3 × 3 square with the following rules:
- Players take turns placing their characters into empty squares.
- The first player A always places the ‘X’ character, while the second player B always places the ‘O’ character.
- The ‘X’ and ‘O’ characters are always placed in empty squares
- The game ends when three identical (non-empty) characters fill any row, column, or diagonal.
- If all squares are not empty, the game ends.
- If the game is over, no more action can be taken.
Given a two-dimensional integer array moves, move[I] = [rowi,coli] indicates that the i-th operation is performed on grid[Rowi][coli]. If A or B meet three consecutive characters in the same row, column, or diagonal, the winner of the game is returned. If the game ends in a Draw, return Draw. Return Pending if there is still room to continue the game.
In fact, after understanding the topic is also very simple, the idea is as follows:
- I’m going to take all of A’s positions on this grid and put them in list A, and I’m going to take all of B’s positions on this grid and put them in list B
- Since there are only eight winning conditions, they are all listed and put into winCom
- Iterate over each win situation com in winCom, return A directly if it judges that the elements in list A and COM are equal, and return B directly if it judges that the elements in list B and COM are equal
- Otherwise, if the sum of the lengths of lists A and B is 9, it is A Draw and returns Draw
- Otherwise, return Pending
answer
class Solution(object): def tictactoe(self, moves): """ :type moves: List[List[int]] :rtype: STR "" "winCom = [[[0, 0], [0, 1], [0, 2]], [[1, 0], [1, 1], [1, 2]], [[2, 0], [2, 1], [2]]. [[0, 0], [1, 0], [2, 0]], [[0, 1], [1, 1], [2, 1]], [[0, 2], [1, 2], [2]]. [[0, 0], [1, 1], [2]], [[0, 2], [1, 1], [2, 0]]] A = [c for c moves in [0: len (moves) : 2]] B = [c for c moves in [1: len (moves) : 2]] for com in winCom: if self.isWin(A, com): return 'A' if self.isWin(B, com): return 'B' if len(A)+len(B)==9: return "Draw" return "Pending" def isWin(self, L, com): result = 0 for c in L: if c in com: result += 1 return result == 3Copy the code
The results
Each node in the given list has been linked to a Tic Tac Toe Game. Memory Usage: 10 ms Submissions in Python online submissions for Find winners on a Tic Tac Toe Game.Copy the code
Original link: leetcode.com/problems/fi…
Your support is my biggest motivation