This is a typical chess game where your opponent first places a random number of Knights, Rooks, Bishop, and Queens on an ‘N*N’ chessboard, and then you have to place your king safely on the chessboard such that it should not be under attack by any piece.
Given an ‘N*N’ chessboard with ‘K’ number of Knights, ‘R’ number of Rooks, ‘B’ number of Bishops, and ‘Q’ number of queens. Your task is to find out the number of squares on the chessboard such that your King is not checked by any of your opponent's pieces.
NOTE: If you don't know how to play chess and how chess pieces move, please refer to the below link (you can concentrate only on how the above-mentioned pieces move).
EXAMPLE :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)’
Output format :
For each test case, print the number of squares where King can be placed safely.
Note :
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
2
4
2
0 0
1 1
1
2 2
0
1
3 3
8
4
2 6
3 2
5 6
7 7
4
2 2
4 6
6 4
7 5
4
0 4
1 1
1 6
5 1
2
3 5
6 0
2
5
For the first test case,

The safe places are (0, 1) and (1, 0)
Hence, the output will be: 2
For the second test case,

The safe places are (0, 1), (0, 3), (1, 7), (3, 1), and (5, 7).
Hence, the output will be: 5
2
4
2
0 0
1 1
3
2 2
3 0
0 3
0
1
3 3
8
5
2 6
3 2
5 6
7 7
4 0
4
2 2
4 6
6 4
7 5
4
0 4
1 1
1 6
5 1
3
3 5
6 0
4 2
0
6
Can we try to place our king at each position and check if it is attacked.
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[])
O(N * N * N), Where ‘N’ is the input integer.
The time complexity to check if any cell is attacked by any chess piece is O(N) as we check on each side, and we iterated through each cell once, hence the time complexity will be O(N * N * N).
O(N * N), Where ‘N’ is the input integer.
As we used a 2D character array of size ‘N * N’ to mark the chess piece's presence on the chessboard, hence the space complexity will be O(N * N).