Find any possible two coordinates of Rectangle whose two coordinates are given | C ++ program for the above approach ; Function to find the remaining two rectangle coordinates ; Pairs to store the position of given two coordinates of the rectangle . ; Pairs to store the remaining two coordinates of the rectangle . ; Traverse through matrix and find pairs p1 and p2 ; First Case ; Second Case ; Third Case ; Print the matrix ; Driver code ; Given Input ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void Create_Rectangle ( vector < string > arr , int n ) { pair < int , int > p1 = { -1 , -1 } ; pair < int , int > p2 = { -1 , -1 } ; pair < int , int > p3 ; pair < int , int > p4 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( arr [ i ] [ j ] == '1' ) if ( p1 . first == -1 ) p1 = { i , j } ; else p2 = { i , j } ; } } p3 = p1 ; p4 = p2 ; if ( p1 . first == p2 . first ) { p3 . first = ( p1 . first + 1 ) % n ; p4 . first = ( p2 . first + 1 ) % n ; } else if ( p1 . second == p2 . second ) { p3 . second = ( p1 . second + 1 ) % n ; p4 . second = ( p2 . second + 1 ) % n ; } else { swap ( p3 . first , p4 . first ) ; } arr [ p3 . first ] [ p3 . second ] = '1' ; arr [ p4 . first ] [ p4 . second ] = '1' ; for ( int i = 0 ; i < n ; i ++ ) { cout << arr [ i ] << endl ; } } int main ( ) { int n = 4 ; vector < string > arr { "0010" , "0000" , "1000" , "0000" } ; Create_Rectangle ( arr , n ) ; return 0 ; }