Refer to the explanation of Sample Input 1 for better understanding.
The first line will contain the integer 'T', the number of test cases.
The first line of each test case consists of integer 'N', the length of the given chessboard.
The next line contains ‘K’, no. of Knights. Next, ‘K’ lines provide 2 space-separated integers denoting the row and the column of the Knights ‘(i,j)’
The next line contains ‘R’, no. of Rooks. Next, ‘R’ lines provide 2 space-separated integers denoting the row and the column of the Rooks ‘(i,j)’
The next line contains ‘B’, no. of Bishops. Next, ‘B’ lines provide 2 space-separated integers denoting the row and the column of the Bishops ‘(i,j)’
The next line contains ‘Q’, no. of Queens. Next, ‘Q’ lines provide 2 space-separated integers denoting the row and the column of the Queens ‘(i,j)’
For each test case, print the number of squares where King can be placed safely.
You don't need to print anything. It has already been taken care of. Just implement the given function.
1 <= 'T' <= 10
2 <= 'N' <= 100
0 <= 'K' + ‘R’ + ‘B’ + ‘Q’ <= ‘N * N’
0 <= ‘i’, ‘j’ <= ‘N - 1’
It is guaranteed that sum of ‘N’ over all test cases is <= 1000
Time Limit: 1 sec
The idea of this approach is to place the king at each cell that is not occupied and check if it is getting attacked by any chess piece.
So we would iterate on each cell of the chessboard and if let’s suppose we are currently at cell (i, j) then
If all the above conditions are false then we can place the king here safely.
Algorithm:
// The function will check if there is a knight attacking the given cell.
Bool attackedByKnight(i, j)
Bool attackedFromLeft(i, j)
Bool attackedFromRight(i, j)
Bool attackedFromUp(i, j)
Bool attackedFromDown(i, j)
Bool attackedFromMainDiagonalUp(i, j)
Bool attackedFromMainDiagonalDown(i, j)
Bool attackedFromOtherDiagonalUp(i, j)
Bool attackedFromOtherDiagonalDown(i, j)
Bool attackedByAny(i, j)
// The function will return the count of safe positions to place the king on the chessboard.
Int kingPlacement(N, K, R, B, Q, pointsK[], pointsR[], pointsB[], pointsQ[])
The idea is to first iterate on the chessboard and mark all the attacked cells and then we again iterate on the chessboard and count the number of cells which remain unmarked.
The attacked cells can be marked using an ordinary iterative approach.
So in this approach based on the chess piece we are currently on we will keep marking all the cells in its attack range until we escape the chessboard or another chess piece is already present there.
Algorithm:
// Mark cells attacked by Knight
Void markAttackedByKnight(i, j, chessboard[][])
Void markUp(i, j, chessboard[][], chessPiece)
Void markDown(i, j, chessboard[][])
Void markLeft(i, j, chessboard[][])
Void markRight(i, j, chessboard[][])
Void markAttackedByRook(i, j, chessboard[][])
// Similar functions can be created for all chess pieces and called when encountered.
// The function will return the count of safe positions to place the king on the chessboard.
Int kingPlacement(N, K, R, B, Q, pointsK[], pointsR[], pointsB[], pointsQ[])