-
Notifications
You must be signed in to change notification settings - Fork 96
/
267.py
113 lines (101 loc) · 3.69 KB
/
267.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
"""
Problem:
You are presented with an 8 by 8 matrix representing the positions of pieces on a chess
board. The only pieces on the board are the black king and various white pieces. Given
this matrix, determine whether the king is in check.
For details on how each piece moves, see here.
For example, given the following matrix:
...K....
........
.B......
......P.
.......R
..N.....
........
.....Q..
You should return True, since the bishop is attacking the king diagonally.
"""
from typing import List
def is_attacking(board: List[List[str]]) -> bool:
for i in range(8):
for j in range(8):
# case pawn
if board[i][j] == "P":
if j > 0 and board[i - 1][j - 1] == "K":
return True
if j < 8 and board[i - 1][j + 1] == "K":
return True
# case rook
elif board[i][j] == "R":
for k in range(8):
if board[i][k] == "K" or board[k][j] == "K":
return True
# case knight
elif board[i][j] == "N":
moves = [
(i + 2, j + 1),
(i + 2, j - 1),
(i - 2, j + 1),
(i - 2, j - 1),
(i + 1, j + 2),
(i + 1, j - 2),
(i - 1, j + 2),
(i - 1, j - 2),
]
for y, x in moves:
if 0 <= y < 8 and 0 <= x < 8 and board[y][x] == "K":
return True
# case bishop
elif board[i][j] == "B":
for y, x in zip(range(i, -1, -1), range(j, -1, -1)):
if board[y][x] == "K":
return True
for y, x in zip(range(i, 8), range(j, -1, -1)):
if board[y][x] == "K":
return True
for y, x in zip(range(i, 8), range(j, 8)):
if board[y][x] == "K":
return True
for y, x in zip(range(i, -1, -1), range(j, 8)):
if board[y][x] == "K":
return True
# case queen
elif board[i][j] == "Q":
for y, x in zip(range(i, -1, -1), range(j, -1, -1)):
if board[y][x] == "K":
return True
for y, x in zip(range(i, 8), range(j, -1, -1)):
if board[y][x] == "K":
return True
for y, x in zip(range(i, 8), range(j, 8)):
if board[y][x] == "K":
return True
for y, x in zip(range(i, -1, -1), range(j, 8)):
if board[y][x] == "K":
return True
for k in range(8):
if board[i][k] == "K" or board[k][j] == "K":
return True
return False
if __name__ == "__main__":
print(
is_attacking(
[
[".", ".", ".", "K", ".", ".", ".", "."],
[".", ".", ".", ".", ".", ".", ".", "."],
[".", "B", ".", ".", ".", ".", ".", "."],
[".", ".", ".", ".", ".", ".", "P", "."],
[".", ".", ".", ".", ".", ".", ".", "R"],
[".", ".", "N", ".", ".", ".", ".", "."],
[".", ".", ".", ".", ".", ".", ".", "."],
[".", ".", ".", ".", ".", "Q", ".", "."],
]
)
)
"""
SPECS:
TIME COMPLEXITY: O(n)
SPACE COMPLEXITY: O(1)
[n = number of pieces on the board (as the board is of dimension 8 x 8, all checks
for if a piece is attacking the king take O(1) time)]
"""