![]() |
VOOZH | about |
Given a matrix board[][] consisting of the characters K or k, Q or q, B or b, N or n, R or r, and P or p (Upper case white and lower case black) representing the King, the Queen, the Bishop, the Knight, the Rook, and Pawns of Black and White color respectively, and empty spaces indicated by '-', the task is to check which king (black of white) is unsafe, i.e. if it is under attack (can be eliminated) by any of the other pieces and print the answer accordingly.
Note: If both kings are safe then output "No King in danger".
Examples:
Input:
board[][] = {{- - - k - - - -},
{p p p - p p p p},
{- - - - - b - -},
{- - - R - - - -},
{- - - - - - - -},
{- - - - - - - -},
{P - P P P P P P},
{K - - - - - - - }}
Output:White King in danger
Explanation: Black bishop can attack the white king.
Input:
board[][] = {{- - k - - - - -},
{p p p - p p p p},
{- - - - - - b -},
{- - - R - - - -},
{- - - - - - - -},
{- - - - - - - -}
{P - P P P P P P},
{K - - - - - - -}}
Output: No King in danger
Approach:
The approach is to check the moves of each and every piece on the chessboard:
Below is the implementation of this approach.
White king in danger
Time Complexity: O(N3)
Auxiliary Space: O(1)