-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1275.找出井字棋的获胜者.py
46 lines (40 loc) · 1.17 KB
/
1275.找出井字棋的获胜者.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#
# @lc app=leetcode.cn id=1275 lang=python3
#
# [1275] 找出井字棋的获胜者
#
# @lc code=start
class Solution:
def tictactoe(self, moves: List[List[int]]) -> str:
wins = [
[(0, 0), (0, 1), (0, 2)],
[(1, 0), (1, 1), (1, 2)],
[(2, 0), (2, 1), (2, 2)],
[(0, 0), (1, 0), (2, 0)],
[(0, 1), (1, 1), (2, 1)],
[(0, 2), (1, 2), (2, 2)],
[(0, 0), (1, 1), (2, 2)],
[(0, 2), (1, 1), (2, 0)],
]
def checkwin(S):
for win in wins:
flag = True
for pos in win:
if pos not in S:
flag = False
break
if flag:
return True
return False
A, B = set(), set()
for i, (x, y) in enumerate(moves):
if i % 2 == 0:
A.add((x, y))
if checkwin(A):
return "A"
else:
B.add((x, y))
if checkwin(B):
return "B"
return "Draw" if len(moves) == 9 else "Pending"
# @lc code=end