Maximum non | C # program for 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
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 ) ; } }