-
Notifications
You must be signed in to change notification settings - Fork 0
/
rookCapture.py
57 lines (49 loc) · 1.86 KB
/
rookCapture.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
class Solution:
def numRookCaptures(self, board):
# Find the position of the rook (R)
for i in range(8):
for j in range(8):
if board[i][j] == 'R':
rook_row, rook_col = i, j
break
captures = 0
# Check upwards (towards row 0)
for row in range(rook_row - 1, -1, -1):
if board[row][rook_col] == 'B': # Blocked by a bishop
break
if board[row][rook_col] == 'p': # Pawn found
captures += 1
break
# Check downwards (towards row 7)
for row in range(rook_row + 1, 8):
if board[row][rook_col] == 'B': # Blocked by a bishop
break
if board[row][rook_col] == 'p': # Pawn found
captures += 1
break
# Check left (towards column 0)
for col in range(rook_col - 1, -1, -1):
if board[rook_row][col] == 'B': # Blocked by a bishop
break
if board[rook_row][col] == 'p': # Pawn found
captures += 1
break
# Check right (towards column 7)
for col in range(rook_col + 1, 8):
if board[rook_row][col] == 'B': # Blocked by a bishop
break
if board[rook_row][col] == 'p': # Pawn found
captures += 1
break
return captures
# Example usage
board = [[".",".",".",".",".",".",".","."],
[".",".",".","p",".",".",".","."],
[".",".",".","R",".",".",".","p"],
[".",".",".",".",".",".",".","."],
[".",".",".",".",".",".",".","."],
[".",".",".","p",".",".",".","."],
[".",".",".",".",".",".",".","."],
[".",".",".",".",".",".",".","."]]
solution = Solution()
print(solution.numRookCaptures(board)) # This will work