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.

&nbsp;
Example 1:

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


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.


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.


&nbsp;
Constraints:


	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'


class Solution:
    def numRookCaptures(self, board: List[List[str]]) -> int:
		# Checking for possible case to the right of Rook
        def right_position(n,i):
            List = i[0:n][::-1] # taking list to the right of rook
            pIndex,bIndex = -1,-1
            if 'p' in List: # Checking if  'p' in list   
                pIndex = List.index('p')
            if 'B' in List: # Checking if 'B' in list
                bIndex = List.index('B')
            print(bIndex,pIndex,List)
            if bIndex == -1 and pIndex >-1: # if list does not have 'B' and have 'p'
                return True
            if pIndex == -1: # if list does not have 'p'
                return False
            return bIndex>pIndex 
        def left_position(n,i):
            List = i[n+1:]# taking list to the right of rook
            pIndex,bIndex = -1,-1
            if 'p' in List:
                pIndex = List.index('p')
            if 'B' in List:
                bIndex = List.index('B')
            print(bIndex,pIndex,List)
            if bIndex == -1 and pIndex >-1:
                return True
            if pIndex == -1:
                return False
            return bIndex>pIndex
        Count = 0
		# Checking for possibilites in row
        for i in board:
            if 'R' in i:
                print(i)
                n = i.index('R')
                if left_position(n,i):
                    Count += 1
                if right_position(n,i):
                    Count += 1
        Col = []
		# checking for possibilites in col
        for i in range(0,len(board)):
            Col.append(board[i][n]) # taking the elements from the same col of Rook
        n = Col.index('R')
        if left_position(n,Col):
            Count += 1
        if right_position(n,Col):
            Count += 1
        return Count
class Solution {
    public int numRookCaptures(char[][] board) {
        int ans = 0;

        int row = 0;
        int col = 0;
        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 8; j++) {
                if (board[i][j] == 'R') {
                    row = i;
                    col = j;
                    break;
                }
            }
        }

        int j = col;
        while (j >= 0) {
            if (board[row][j] == 'B') {
                break;
            } else if (board[row][j] == 'p') {
                ans++;
                break;
            }

            j--;
        }

        j = col;
        while (j <= board[0].length - 1) {
            if (board[row][j] == 'B') {
                break;
            } else if (board[row][j] == 'p') {
                ans++;
                break;
            }

            j++;
        }

        int i = row;
        while (i <= board.length - 1) {
            if (board[i][col] == 'B') {
                break;
            } else if (board[i][col] == 'p') {
                ans++;
                break;
            }

            i++;
        }

        i = row;
        while (i >= 0) {
            if (board[i][col] == 'B') {
                break;
            } else if (board[i][col] == 'p') {
                ans++;
                break;
            }

            i--;
        }

        return ans;
    }
}
class Solution {
public:
    int numRookCaptures(vector<vector<char>>& board) {
        int res=0;
        int p=-1,q=-1;
        for(int i=0;i<board.size();i++)
        {
            for(int j=0;j<board[0].size();j++)
            {
                if(board[i][j]=='R') // storing position of R
                {
                    p=i;
                    q=j;
                    break;
                }
            }
            if(p>=0)
                break;
        }
        // traverse Up, Down, Left and Right from R
        for(int i=p+1;i<board.size();i++)
        {
            if(board[i][q]!='.')
            {
                if(board[i][q]=='p')
                    res++;
                break;
            }
        }
        for(int i=p-1;i>=0;i--)
        {
            if(board[i][q]!='.')
            {
                if(board[i][q]=='p')
                    res++;
                break;
            }
        }
        
        for(int j=q+1;j<board[0].size();j++)
        {
            if(board[p][j]!='.')
            {
                if(board[p][j]=='p')
                    res++;
                break;
            }
        }
        for(int j=q-1;j>=0;j--)
        {
            if(board[p][j]!='.')
            {
                if(board[p][j]=='p')
                    res++;
                break;
            }
        }
        return res;
    }
};
var numRookCaptures = function(board) {
    const r=board.length;
    const c=board[0].length;

    let res=0
    const dir=[[0,1],[0,-1],[1,0],[-1,0]]; // all the 4 possible directions
    let rook=[];

    // finding the rook's position
    for(let i=0;i<r;i++){
        for(let j=0;j<c;j++){
            if(board[i][j]==='R'){
                rook=[i,j]
                break;
            }
        }
    }

    // traversing all the 4 directions
    for(let [x,y] of dir){
        res+=helper(x,y,rook[0],rook[1])
    }

    return res;

    function helper(x,y,i,j){
        let newX=x+i
        let newY=y+j
        let attack=0
        while(newX<r&&newY<c&&newX>=0&&newY>=0){
            if(board[newX][newY]==='p'){
                attack++
                break; // break because Rook can't attack more than one pawn in same direction
            }else if(board[newX][newY]==='B'){
                break
            }
            // keep moving towards the same direction
            newX+=x
            newY+=y
        }
        return attack
    }
};
Available Captures for Rook