forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueens That Can Attack the King.py
67 lines (65 loc) · 1.88 KB
/
Queens That Can Attack the King.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
class Solution:
def queensAttacktheKing(self, queens: List[List[int]], king: List[int]) -> List[List[int]]:
ans = []
d = {(i[0],i[1]) : True for i in queens}
def goUp(r,c):
while r >=0:
if (r,c) in d:
ans.append([r,c])
break
r -= 1
def goDown(r,c):
while r < 8:
if (r,c) in d:
ans.append([r,c])
break
r += 1
def goLeft(r,c):
while c >= 0:
if (r,c) in d:
ans.append([r,c])
break
c -= 1
def goRight(r,c):
while c < 8:
if (r,c) in d:
ans.append([r,c])
break
c += 1
def goD1(r,c):
while r >=0 and c >= 0:
if (r,c) in d:
ans.append([r,c])
break
r -= 1
c -= 1
def goD2(r,c):
while r < 8 and c >= 0:
if (r,c) in d:
ans.append([r,c])
break
r += 1
c -= 1
def goD3(r,c):
while r < 8 and c < 8:
if (r,c) in d:
ans.append([r,c])
break
r += 1
c += 1
def goD4(r,c):
while r >= 0 and c < 8:
if (r,c) in d:
ans.append([r,c])
break
r -= 1
c += 1
goUp(king[0],king[1])
goDown(king[0],king[1])
goLeft(king[0],king[1])
goRight(king[0],king[1])
goD1(king[0],king[1])
goD2(king[0],king[1])
goD3(king[0],king[1])
goD4(king[0],king[1])
return ans