-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1222. Queens That Can Attack the King.py
More file actions
69 lines (48 loc) · 1.66 KB
/
1222. Queens That Can Attack the King.py
File metadata and controls
69 lines (48 loc) · 1.66 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
class Solution:
def queensAttacktheKing(self, queens: List[List[int]], king: List[int]) -> List[List[int]]:
def leftdown(a, b):
if a == -1 or b == -1:
return
if [a,b] in queens:
return [a,b]
return leftdown(a-1, b-1)
def leftup(a, b):
if a == 8 or b == -1:
return
if [a,b] in queens:
return [a,b]
return leftup(a+1, b-1)
def rightup(a, b):
if a == 8 or b == 8:
return
if [a,b] in queens:
return [a,b]
return rightup(a+1, b+1)
def rightdown(a, b):
if a == -1 or b == 8:
return
if [a,b] in queens:
return [a,b]
return rightdown(a-1, b+1)
answer = []
try:
answer.append(sorted([[a,b] for a,b in queens if b == king[1] and a < king[0]])[-1])
except:
pass
try:
answer.append(sorted([[a,b] for a,b in queens if b == king[1] and a > king[0]])[0])
except:
pass
try:
answer.append(sorted([[a,b] for a,b in queens if a == king[0] and b < king[1]])[-1])
except:
pass
try:
answer.append(sorted([[a,b] for a,b in queens if a == king[0] and b > king[1]])[0])
except:
pass
answer.append(leftup(king[0],king[1]))
answer.append(rightup(king[0],king[1]))
answer.append(leftdown(king[0],king[1]))
answer.append(rightdown(king[0],king[1]))
return [arr for arr in answer if arr]