Total number of cells covered in a matrix after D days | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int solve ( int n , int m , int x , int y , int d ) { int top = min ( d , x - 1 ) ; int down = min ( d , n - x ) ; int left = min ( d , y - 1 ) ; int right = 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 ; } int main ( ) { int n , m , x , y , d ; n = 10 , m = 10 ; x = 7 , y = 8 ; d = 4 ; d -- ; cout << solve ( n , m , x , y , d ) ; }