Minimum Numbers of cells that are connected with the smallest path between 3 given cells | Java 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 ; 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 code ; vector pair to store X , Y , Z
import java . util . * ; import java . awt . Point ; public class Main { static int Minimum_Cells ( Vector < Point > v ) { int [ ] col = new int [ 3 ] ; int i , j ; for ( i = 0 ; i < 3 ; i ++ ) { int column_number = v . get ( i ) . y ; col [ i ] = column_number ; } Arrays . sort ( col ) ; int MidRow = v . get ( 1 ) . x ; Set < Point > s = new HashSet < Point > ( ) ; int Maxcol = col [ 2 ] , MinCol = col [ 0 ] ; for ( i = MinCol ; i <= Maxcol ; i ++ ) { s . add ( new Point ( MidRow , i ) ) ; } for ( i = 0 ; i < 3 ; i ++ ) { if ( v . get ( i ) . x == MidRow ) continue ; for ( j = Math . min ( v . get ( i ) . x , MidRow ) ; j <= Math . max ( v . get ( i ) . x , MidRow ) ; j ++ ) { s . add ( new Point ( j , v . get ( i ) . x ) ) ; } } return s . size ( ) ; } public static void main ( String [ ] args ) { Vector < Point > v = new Vector < Point > ( ) ; v . add ( new Point ( 0 , 0 ) ) ; v . add ( new Point ( 1 , 1 ) ) ; v . add ( new Point ( 2 , 2 ) ) ; System . out . print ( Minimum_Cells ( v ) ) ; } }