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 ; Dimensions of cell ; Starting coordinates ; Number of days ; Function call
function solve ( n , m , x , y , d ) { let top = Math . min ( d , x - 1 ) ; let down = Math . min ( d , n - x ) ; let left = Math . min ( d , y - 1 ) ; let right = Math . min ( d , m - y ) ; let quad1 = top * left ; let quad2 = left * down ; let quad3 = down * right ; let quad4 = right * top ; let totalsq = quad1 + quad2 + quad3 + quad4 ; let singleBlocks = top + down + left + right + 1 ; return totalsq + singleBlocks ; } let n = 10 , m = 10 ; let x = 7 , y = 8 ; let d = 4 ; d -- ; document . write ( solve ( n , m , x , y , d ) ) ;