Maximum non | C ++ Program to implement the above approach ; Function to print the maximum rooks and their positions ; Initialize row and col array ; Marking the location of already placed rooks ; Print number of non - attacking rooks that can be placed ; To store the placed rook location ; Print lexographically smallest order ; Driver Code ; Size of board ; Number of rooks already placed ; Position of rooks ; Function call
#include <bits/stdc++.h> NEW_LINE 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 ) ; }