


Ninja is learning how to play chess. He is still a beginner, so his friend gave him a simple puzzle. The given situation of the chessboard has only one white Rook ‘R’, some white bishops ‘B’ and some black pawns ‘P’. The task is to find the number of pawns that can be captured by the Rook. Can you help Ninja to solve this problem?
You are given a list of 8 strings of size 8 characters each to describe the position of the pieces. Find the number of pawns that can be captured by the Rook
Each test case contains a list of 8 space-separated strings.
Output Format:
For each test case, print ‘an integer corresponding to the number of pawns that can be captured by the Rook.
Print the output of each test case in a separate line.
Note:
You do not need to print anything. It has already been taken care of. Just implement the given function.
Board[i][j] = {‘R’ , ‘B’, ‘P’, ‘ * ’ }.
Time limit: 1 sec
******** ******** ***R***P ******** ***P**** ******** ***P**** ********
2
The white rook will attack the pawns at d5 and h6. Hence, the answer is 2.
***P**** ***B**** P**R***P ******** ***P**** ******** ***P**** ********
3
Find the position of the rook and iterate left, right, up, and down.
In this approach, we will first iterate through the board and find the position of the white rook. After that, we will iterate left, right, up, and down and find if there exists a pawn or the path is blocked by the bishop. We will count the number of pawns under attack and return the answer.
Algorithm:
O(1).
In this approach, in the worst case, we will be iterating over all 64 squares to find the position of the Rook and then iterating in all four directions to check for pawns under attack. Hence, it takes constant time. Hence, the overall time complexity is O(1).
O(1)
In this approach, we are using constant space. Hence, the overall space complexity is O(1).