“This is the 8th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021”

describe

On an 8 x 8 chessboard, there is exactly one white rook ‘R’ and some number of white bishops ‘B’, black pawns ‘p’, and empty squares ‘.’.

When the rook moves, it chooses one of four cardinal directions (north, east, south, or west), then moves in that direction until it chooses to stop, reaches the edge of the board, captures a black pawn, or is blocked by a white bishop. A rook is considered attacking a pawn if the rook can capture the pawn on the rook’s turn. The number of available captures for the white rook is the number of pawns that the rook is attacking.

Return the number of available captures for the white rook.

Example 1:

Input: board = [[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".","R",".",".",".","p"],[".",".",".",".", ".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".",".",".",".",".","."],[ ".",".",".",".",".",".",".","."]] Output: 3 Explanation: In this example, the rook is attacking all the pawns.Copy the code

Example 2:

Input: board = [[".",".",".",".",".",".",".","."],[".","p","p","p","p","p",".","."],[".","p","p","B","p","p",".","."],[".","p","B","R", "B","p",".","."],[".","p","p","B","p","p",".","."],[".","p","p","p","p","p",".","."],[".",".",".",".",".",".",".","."],[ ".",".",".",".",".",".",".","."]] Output: 0 Explanation: The bishops are blocking the rook from attacking any of the pawns.Copy the code

Example 3:

Input: board = [[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".","p",".",".",".","."],["p","p",".","R", ".","p","B","."],[".",".",".",".",".",".",".","."],[".",".",".","B",".",".",".","."],[".",".",".","p",".",".",".","."],[ ".",".",".",".",".",".",".","."]] Output: 3 Explanation: The rook is attacking the pawns at positions b5, d6, and f5.Copy the code

Note:

board.length == 8
board[i].length == 8
board[i][j] is either 'R', '.', 'B', or 'p'
There is exactly one cell with board[i][j] == 'R'
Copy the code

parsing

On an 8 x 8 board, there is a white rook R and some white B’s, black P’s and empty sides.

When the car moves, it selects four basic directions (north, east, south or west), and then to the direction, until it choose to stop, or to the board edge, or capture a black pieces or blocked by a white flag, then go to other directions for the same operation, until four directions are performed, and returns the number of black white car can capture.

The question is more round, because it is not very clear, I also read the online dagod explanation to understand the meaning of the question, choose a direction to go down, the termination condition is: encounter B or P, or reached the boundary, the general idea is as follows:

  • If you don’t find R, return 0. If you find R, then do the same thing in all four directions
  • When it hits a boundary or B, it stops in that direction and goes in the other direction
  • When p is encountered, the counter is incremented by one to capture black and stop moving in that direction to move in another direction
  • Return the counter when all four directions have been completed

answer

class Solution(object): def numRookCaptures(self, board): """ :type board: List[List[str]] :rtype: Int "" def check_direction(R,C,direction): if direction=="right": for col in range(C+1,8): if board[R][col]=='p':return 1 if board[R][col]=='B':return 0 if direction=="left": for col in range(C-1,-1,-1): If board[R][col]=='p':return 1 if board[R][col]=='B':return 0 if direction=="down": for row in range(R+1,8): if board[row][C]=='p':return 1 if board[row][C]=='B':return 0 if direction=="up": for row in range(R-1,-1,-1): if board[row][C]=='p':return 1 if board[row][C]=='B':return 0 return 0 for row in range(8): for col in range(8): if board[row][col]=="R": return sum([check_direction(row,col,"right"), check_direction(row,col,"left"), check_direction(row,col,"up"), check_direction(row,col,"down")])Copy the code

The results

Given in the Python online submissions for Available Captures for Rook. Memory Usage: Submissions in Python online submissions for Available Captures for Rook.Copy the code

Original link: leetcode.com/problems/av…

Your support is my biggest motivation