Given an integers N such that there is a chessboard of size N*N and an array pos[][] of K pairs of integers which represent the positions of placed rooked in the given chessboard. The task is to find the maximum number of rooks with their positions that can be placed on the given chessboard such that no rook attacks some other rook. Print the positions in lexicographical order.
Examples:
Input: N = 4, K = 2, pos[][] = {{1, 4}, {2, 2}}
Output:
2
3 1
4 3
Explanation:
Only 2 more rooks can be placed on the given chessboard and their positions are (3, 1) and (4, 3).
Input: N = 5, K = 0, pos[][] = {}
Output:
5
1 1
2 2
3 3
4 4
5 5
Explanation:
Since the chessboard is empty we can place 5 rooks the given chessboard and their positions are (1, 1), (2, 2), (3, 3), (4, 4) and (5, 5).
Naive Approach: The simplest approach is to try to place a rook at every empty position of the chessboard and check if it attacks the already placed rooks or not. Below are the steps:
- Initialize a 2D matrix M[][] of size N*N to represent the chessboard and place the already given rooks in it.
- Transverse the complete matrix M[][] and check if the ith row and jth column contains any rook
- If the ith row and jth column both don’t contain any rook, then a rook is placed there and this cell is added to the result.
- Otherwise, move to the next empty cell on the chessboard.
Time Complexity: O(N3)
Auxiliary Space: O(N2)
Efficient Approach: The approach is based on the idea that a maximum of (N – K) rooks can be placed on the chessboard according to the Pigeonhole Principle. Below are the steps:
- Since no two of the given rooks attack each other, all the rows given in the input must be unique. Similarly, all the columns given in the input must be unique.
- So, place the rooks only in N – K unused rows and N – K unused columns.
- Therefore, lexicographically minimum configuration can be achieved by pairing the smallest unused row with the smallest unused column, the second smallest unused row with the second smallest unused column, and so on.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void countRooks( int n, int k,
int pos[2][2])
{
int row[n] = {0};
int col[n] = {0};
for ( int i = 0; i < n; i++)
{
row[i] = 0;
col[i] = 0;
}
for ( int i = 0; i < k; i++)
{
row[pos[i][0] - 1] = 1;
col[pos[i][1] - 1] = 1;
}
int res = n - k;
cout << res << " " << endl;
int ri = 0, ci = 0;
while (res-- > 0)
{
while (row[ri] == 1)
{
ri++;
}
while (col[ci] == 1)
{
ci++;
}
cout << (ri + 1) << " " <<
(ci + 1) << " " <<endl;
ri++;
ci++;
}
}
int main()
{
int N = 4;
int K = 2;
int pos[2][2] = {{1, 4}, {2, 2}};
countRooks(N, K, pos);
}
|
Java
public class GFG {
private static void countRooks( int n, int k,
int pos[][])
{
int row[] = new int [n];
int col[] = new int [n];
for ( int i = 0 ; i < n; i++) {
row[i] = 0 ;
col[i] = 0 ;
}
for ( int i = 0 ; i < k; i++) {
row[pos[i][ 0 ] - 1 ] = 1 ;
col[pos[i][ 1 ] - 1 ] = 1 ;
}
int res = n - k;
System.out.println(res + " " );
int ri = 0 , ci = 0 ;
while (res-- > 0 ) {
while (row[ri] == 1 ) {
ri++;
}
while (col[ci] == 1 ) {
ci++;
}
System.out.println(
(ri + 1 )
+ " " + (ci + 1 )
+ " " );
ri++;
ci++;
}
}
public static void main(String[] args)
{
int N = 4 ;
int K = 2 ;
int pos[][] = { { 1 , 4 }, { 2 , 2 } };
countRooks(N, K, pos);
}
}
|
Python3
def countRooks(n, k, pos):
row = [ 0 for i in range (n)]
col = [ 0 for i in range (n)]
for i in range (k):
row[pos[i][ 0 ] - 1 ] = 1
col[pos[i][ 1 ] - 1 ] = 1
res = n - k
print (res)
ri = 0
ci = 0
while (res > 0 ):
while (row[ri] = = 1 ):
ri + = 1
while (col[ci] = = 1 ):
ci + = 1
print ((ri + 1 ), (ci + 1 ))
ri + = 1
ci + = 1
res - = 1
if __name__ = = '__main__' :
N = 4
K = 2
pos = [ [ 1 , 4 ], [ 2 , 2 ] ]
countRooks(N, K, pos)
|
C#
using System;
class GFG{
private static void countRooks( int n, int k,
int [, ]pos)
{
int []row = new int [n];
int []col = new int [n];
for ( int i = 0; i < n; i++)
{
row[i] = 0;
col[i] = 0;
}
for ( int i = 0; i < k; i++)
{
row[pos[i, 0] - 1] = 1;
col[pos[i, 1] - 1] = 1;
}
int res = n - k;
Console.WriteLine(res + " " );
int ri = 0, ci = 0;
while (res -- > 0)
{
while (row[ri] == 1)
{
ri++;
}
while (col[ci] == 1)
{
ci++;
}
Console.WriteLine((ri + 1) + " " +
(ci + 1) + " " );
ri++;
ci++;
}
}
public static void Main(String[] args)
{
int N = 4;
int K = 2;
int [, ]pos = {{1, 4}, {2, 2}};
countRooks(N, K, pos);
}
}
|
Javascript
<script>
function countRooks(n, k, pos)
{
let row = new Array(n).fill(0);
let col = new Array(n).fill(0);
for (let i = 0; i < n; i++) {
row[i] = 0;
col[i] = 0;
}
for (let i = 0; i < k; i++) {
row[pos[i][0] - 1] = 1;
col[pos[i][1] - 1] = 1;
}
let res = n - k;
document.write(res + " " + "<br/>" );
let ri = 0, ci = 0;
while (res-- > 0) {
while (row[ri] == 1) {
ri++;
}
while (col[ci] == 1) {
ci++;
}
document.write(
(ri + 1)
+ " " + (ci + 1)
+ " " + "<br/>" );
ri++;
ci++;
}
}
let N = 4;
let K = 2;
let pos = [[ 1, 4 ], [ 2, 2 ]];
countRooks(N, K, pos);
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(N2)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
07 Mar, 2022
Like Article
Save Article