Minimum Numbers of cells that are connected with the smallest path between 3 given cells | C ++ implementation of the approach ; Function to return the minimum cells that are connected via the minimum length path ; Array to store column number of the given cells ; Sort cells in ascending order of row number ; Middle row number ; Set pair to store required cells ; Range of column number ; Store all cells of middle row within column number range ; Final step to store all the column number of 1 st and 3 rd cell upto MidRow ; Driver Function ; vector pair to store X , Y , Z
#include <bits/stdc++.h> NEW_LINE using namespace std ; int Minimum_Cells ( vector < pair < int , int > > v ) { int col [ 3 ] , i , j ; for ( i = 0 ; i < 3 ; i ++ ) { int column_number = v [ i ] . second ; col [ i ] = column_number ; } sort ( col , col + 3 ) ; sort ( v . begin ( ) , v . end ( ) ) ; int MidRow = v [ 1 ] . first ; set < pair < int , int > > s ; int Maxcol = col [ 2 ] , MinCol = col [ 0 ] ; for ( i = MinCol ; i <= Maxcol ; i ++ ) { s . insert ( { MidRow , i } ) ; } for ( i = 0 ; i < 3 ; i ++ ) { if ( v [ i ] . first == MidRow ) continue ; for ( j = min ( v [ i ] . first , MidRow ) ; j <= max ( v [ i ] . first , MidRow ) ; j ++ ) { s . insert ( { j , v [ i ] . second } ) ; } } return s . size ( ) ; } int main ( ) { vector < pair < int , int > > v = { { 0 , 0 } , { 1 , 1 } , { 2 , 2 } } ; cout << Minimum_Cells ( v ) ; return 0 ; }