Total number of cells covered in a matrix after D days | Java implementation to find the total number of cells covered in a matrix after D days ; Function to return the total infected cells after d days ; Top extension ; Bottom extension ; Left extension ; Right extension ; Calculating the cells in each quadrilateral ; Sum all of them to get total cells in each quadrilateral ; Add the singleblocks along the lines of top , down , left , right ; Return the ans ; Driver code ; Dimensions of cell ; Starting coordinates ; Number of days ; Function call
import java . util . * ; class GFG { static int solve ( int n , int m , int x , int y , int d ) { int top = Math . min ( d , x - 1 ) ; int down = Math . min ( d , n - x ) ; int left = Math . min ( d , y - 1 ) ; int right = Math . min ( d , m - y ) ; int quad1 = top * left ; int quad2 = left * down ; int quad3 = down * right ; int quad4 = right * top ; int totalsq = quad1 + quad2 + quad3 + quad4 ; int singleBlocks = top + down + left + right + 1 ; return totalsq + singleBlocks ; } public static void main ( String [ ] args ) { int n = 10 , m = 10 ; int x = 7 , y = 8 ; int d = 4 ; d -- ; System . out . println ( solve ( n , m , x , y , d ) ) ; } }