Skip to content

Commit 93dc6e8

Browse files
999. Available Captures for Rook
1 parent b8c6dbf commit 93dc6e8

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed

Easy/999.AvailableCapturesforRook.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
"""
2+
On an 8 x 8 chessboard, there is one white rook. There also may be empty
3+
squares, white bishops, and black pawns. These are given as characters
4+
'R', '.', 'B', and 'p' respectively. Uppercase characters represent white
5+
pieces, and lowercase characters represent black pieces.
6+
7+
The rook moves as in the rules of Chess: it chooses one of four cardinal
8+
directions (north, east, west, and south), then moves in that direction
9+
until it chooses to stop, reaches the edge of the board, or captures an
10+
opposite colored pawn by moving to the same square it occupies. Also,
11+
rooks cannot move into the same square as other friendly bishops.
12+
13+
Return the number of pawns the rook can capture in one move.
14+
15+
Example:
16+
Input: [[".",".",".",".",".",".",".","."],
17+
[".",".",".","p",".",".",".","."],
18+
[".",".",".","R",".",".",".","p"],
19+
[".",".",".",".",".",".",".","."],
20+
[".",".",".",".",".",".",".","."],
21+
[".",".",".","p",".",".",".","."],
22+
[".",".",".",".",".",".",".","."],
23+
[".",".",".",".",".",".",".","."]]
24+
Output: 3
25+
Explanation:
26+
In this example the rook is able to capture all the pawns.
27+
28+
Example:
29+
Input: [[".",".",".",".",".",".",".","."],
30+
[".","p","p","p","p","p",".","."],
31+
[".","p","p","B","p","p",".","."],
32+
[".","p","B","R","B","p",".","."],
33+
[".","p","p","B","p","p",".","."],
34+
[".","p","p","p","p","p",".","."],
35+
[".",".",".",".",".",".",".","."],
36+
[".",".",".",".",".",".",".","."]]
37+
Output: 0
38+
Explanation:
39+
Bishops are blocking the rook to capture any pawn.
40+
41+
Example:
42+
Input: [[".",".",".",".",".",".",".","."],
43+
[".",".",".","p",".",".",".","."],
44+
[".",".",".","p",".",".",".","."],
45+
["p","p",".","R",".","p","B","."],
46+
[".",".",".",".",".",".",".","."],
47+
[".",".",".","B",".",".",".","."],
48+
[".",".",".","p",".",".",".","."],
49+
[".",".",".",".",".",".",".","."]]
50+
Output: 3
51+
Explanation:
52+
The rook can capture the pawns at positions b5, d6 and f5.
53+
54+
Note:
55+
1. board.length == board[i].length == 8
56+
2. board[i][j] is either 'R', '.', 'B', or 'p'
57+
3. There is exactly one cell with board[i][j] == 'R'
58+
"""
59+
#Difficulty: Easy
60+
#22 / 22 test cases passed.
61+
#Runtime: 40 ms
62+
#Memory Usage: 14.2 MB
63+
64+
#Runtime: 40 ms, faster than 6.85% of Python3 online submissions for Available Captures for Rook.
65+
#Memory Usage: 14.1 MB, less than 44.63% of Python3 online submissions for Available Captures for Rook.
66+
67+
class Solution:
68+
def numRookCaptures(self, board: List[List[str]]) -> int:
69+
pawns = 0
70+
for i in range(8):
71+
if 'R' in board[i]:
72+
j = board[i].index('R')
73+
vert = [board[i][j] for i in range(8)]
74+
pawns += self.counter(board[i][:j][::-1])
75+
pawns += self.counter(board[i][j+1:])
76+
pawns += self.counter(vert[:i][::-1])
77+
pawns += self.counter(vert[i+1:])
78+
break
79+
return pawns
80+
81+
def counter(self, line):
82+
for char in line:
83+
if char == 'p':
84+
return 1
85+
if char == 'B':
86+
break
87+
return 0

0 commit comments

Comments
 (0)