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++
// C++ Program to implement// the above approach#include <bits/stdc++.h>using namespace std;// Function to print the maximum rooks// and their positionsvoid countRooks(int n, int k, int pos[2][2]) { int row[n] = {0}; int col[n] = {0}; // Initialize row and col array for (int i = 0; i < n; i++) { row[i] = 0; col[i] = 0; } // Marking the location of // already placed rooks for (int i = 0; i < k; i++) { row[pos[i][0] - 1] = 1; col[pos[i][1] - 1] = 1; } int res = n - k; // Print number of non-attacking // rooks that can be placed cout << res << " " << endl; // To store the placed rook // location int ri = 0, ci = 0; while (res-- > 0) { // Print lexographically // smallest order while (row[ri] == 1) { ri++; } while (col[ci] == 1) { ci++; } cout << (ri + 1) << " " << (ci + 1) << " " <<endl; ri++; ci++; } }// Driver Codeint main(){ // Size of board int N = 4; // Number of rooks already placed int K = 2; // Position of rooks int pos[2][2] = {{1, 4}, {2, 2}}; // Function call countRooks(N, K, pos);}// This code is contributed by shikhasingrajput |
Java
// Java program for the above approachpublic class GFG { // Function to print the maximum rooks // and their positions private static void countRooks(int n, int k, int pos[][]) { int row[] = new int[n]; int col[] = new int[n]; // Initialize row and col array for (int i = 0; i < n; i++) { row[i] = 0; col[i] = 0; } // Marking the location of // already placed rooks for (int i = 0; i < k; i++) { row[pos[i][0] - 1] = 1; col[pos[i][1] - 1] = 1; } int res = n - k; // Print number of non-attacking // rooks that can be placed System.out.println(res + " "); // To store the placed rook // location int ri = 0, ci = 0; while (res-- > 0) { // Print lexographically // smallest order while (row[ri] == 1) { ri++; } while (col[ci] == 1) { ci++; } System.out.println( (ri + 1) + " " + (ci + 1) + " "); ri++; ci++; } } // Driver Code public static void main(String[] args) { // Size of board int N = 4; // Number of rooks already placed int K = 2; // Position of rooks int pos[][] = { { 1, 4 }, { 2, 2 } }; // Function call countRooks(N, K, pos); }} |
Python3
# Python3 program for the above approach# Function to prthe maximum rooks# and their positionsdef countRooks(n, k, pos): row = [0 for i in range(n)] col = [0 for i in range(n)] # Marking the location of # already placed rooks for i in range(k): row[pos[i][0] - 1] = 1 col[pos[i][1] - 1] = 1 res = n - k # Print number of non-attacking # rooks that can be placed print(res) # To store the placed rook # location ri = 0 ci = 0 while (res > 0): # Print lexographically # smallest order while (row[ri] == 1): ri += 1 while (col[ci] == 1): ci += 1 print((ri + 1), (ci + 1)) ri += 1 ci += 1 res -= 1# Driver Codeif __name__ == '__main__': # Size of board N = 4 # Number of rooks already placed K = 2 # Position of rooks pos= [ [ 1, 4 ], [ 2, 2 ] ] # Function call countRooks(N, K, pos)# This code is contributed by mohit kumar 29 |
C#
// C# program for the above approachusing System;class GFG{ // Function to print the maximum rooks // and their positions private static void countRooks(int n, int k, int [, ]pos) { int []row = new int[n]; int []col = new int[n]; // Initialize row and col array for (int i = 0; i < n; i++) { row[i] = 0; col[i] = 0; } // Marking the location of // already placed rooks for (int i = 0; i < k; i++) { row[pos[i, 0] - 1] = 1; col[pos[i, 1] - 1] = 1; } int res = n - k; // Print number of non-attacking // rooks that can be placed Console.WriteLine(res + " "); // To store the placed rook // location int ri = 0, ci = 0; while (res -- > 0) { // Print lexographically // smallest order while (row[ri] == 1) { ri++; } while (col[ci] == 1) { ci++; } Console.WriteLine((ri + 1) + " " + (ci + 1) + " "); ri++; ci++; } } // Driver Code public static void Main(String[] args) { // Size of board int N = 4; // Number of rooks already placed int K = 2; // Position of rooks int [, ]pos = {{1, 4}, {2, 2}}; // Function call countRooks(N, K, pos); }}// This code is contributed by Rajput-Ji |
2 3 1 4 3
Time Complexity: O(N2)
Auxiliary Space: O(N2)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Find position of non-attacking Rooks in lexicographic order that can be placed on N*N chessboard
- Maximum bishops that can be placed on N*N chessboard
- Maximum non-attacking Knights that can be placed on an N*M Chessboard
- Find the number of rectangles of size 2*1 which can be placed inside a rectangle of size n*m
- Check if all objects of type A and B can be placed on N shelves
- Count of smaller rectangles that can be placed inside a bigger rectangle
- Check if a king can move a valid move or not when N nights are there in a modified chessboard
- Minimum Cuts can be made in the Chessboard such that it is not divided into 2 parts
- Number of blocks in a chessboard a knight can move to in exactly k moves
- Total position where king can reach on a chessboard in exactly M moves
- Check if a Queen can attack a given cell on chessboard
- Total position where king can reach on a chessboard in exactly M moves | Set 2
- Count positions in a chessboard that can be visited by the Queen which are not visited by the King
- Sum of the products of same placed digits of two numbers
- Check if all enemies are killed with bombs placed in a matrix
- Count of packets placed in each box after performing given operations
- Probability of Knight to remain in the chessboard
- Puzzle | Program to find number of squares in a chessboard
- Save from Bishop in chessboard
- Check if the given chessboard is valid or not
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

