You are given an 8 x 8matrix representing a chessboard. There is exactly one white rook represented by 'R', some number of white bishops 'B', and some number of black pawns 'p'. Empty squares are represented by '.'.
A rook can move any number of squares horizontally or vertically (up, down, left, right) until it reaches another piece or the edge of the board. A rook is attacking a pawn if it can move to the pawn's square in one move.
Note: A rook cannot move through other pieces, such as bishops or pawns. This means a rook cannot attack a pawn if there is another piece blocking the path.
Return the number of pawns the white rook is attacking.
/**
* @param{character[][]}board
* @return{number}
*/varnumRookCaptures=function(board){let x =0,
y =0,
res =0;// 找到车 'R' 的位置for(let i =0; i <8; i++){for(let j =0; j <8; j++){if(board[i][j]==='R'){
x = i;
y = j;break;}}}// 定义四个方向const direction =[[1,0],[-1,0],[0,1],[0,-1]];// 遍历四个方向for(let[dx, dy]of direction){let i = x + dx,
j = y + dy;while(i >=0&& i <8&& j >=0&& j <8){if(board[i][j]==='p'){// 如果遇到卒
res++;break;}if(board[i][j]!=='.'){// 如果遇到阻挡(非空格)break;}
i += dx;
j += dy;}}return res;};