Open In App

Total number of cells covered in a matrix after D days

Last Updated : 02 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given an N * M matrix and a starting position (X, Y) of a virus, the task is to find out the number of covered cells after D days, if the virus spreads from its current cell to its adjacent cells every day.
 

Examples: 
 

Input: N = 5, M = 5, X = 1, Y = 3, D = 3 
 

Output: 15 
Explanation: 
We can clearly see from the picture that 15 cells are covered after 3 days.
Input: N = 10, M = 10, X = 7, Y = 8, D = 4 
Output: 42 
Explanation: 
On making an N * M matrix and filling the adjacent cells for 4 days we will get 42 covered cells. 
 

 

Approach: 
To solve the problem mentioned above we have to observe clearly that from a starting cell, we just need to find out the extension of cells towards top, right, bottom and left after D days. Then calculate the total cells inside every quadrilateral of cells formed and add them all. 
Therefore, the total answer will be the sum of all cells of quadrilaterals after D days + the total cells that are along the top, right, down, left, and 1 (for Starting cell) keeping in consideration the boundaries of the quadrilateral. 
Below is the condition for extension in all four directions:
 

Extension upto Top -> min(D, X-1) 
Extension upto Down -> min(D, N-X) 
Extension upto Left -> min(D, Y-1) 
Extension upto Right -> min(D, M-Y) 
 

Look at the image below for clear understanding: 
 

Now multiply Top * Left, Top * Right, Down * Left, Down * Right and add all of them and also add the total cells along the line of 4 directions. We also add 1(for starting cell) to get the resultant cells.
Below is the implementation of above approach:
 

C++




// C++ implementation to find the
// Total number of cells covered
// in a matrix after D days
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to return the total
// infected cells after d days
int solve(int n, int m, int x, int y, int d)
{
    // Top extension
    int top = min(d, x - 1);
 
    // Bottom extension
    int down = min(d, n - x);
 
    // Left extension
    int left = min(d, y - 1);
 
    // Right extension
    int right = min(d, m - y);
 
    // Calculating the cells
    // in each quadrilateral
    int quad1 = top * left;
    int quad2 = left * down;
    int quad3 = down * right;
    int quad4 = right * top;
 
    // Sum all of them to get
    // total cells in each
    // quadrilateral
    int totalsq = quad1 + quad2 + quad3 + quad4;
 
    // Add the singleblocks
    // along the lines of top,
    // down, left, right
    int singleBlocks = top + down + left + right + 1;
 
    // Return the ans
    return totalsq + singleBlocks;
}
 
// Driver code
int main()
{
    int n, m, x, y, d;
 
    // Dimensions of cell
    n = 10, m = 10;
 
    // Starting Coordinates
    x = 7, y = 8;
 
    // Number of Days
    d = 4;
    d--;
 
    // Function Call
    cout << solve(n, m, x, y, d);
}


Java




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


Python3




# Python3 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
 
 
def solve(n, m, x, y, d):
 
    # Top extension
    top = min(d, x - 1)
 
    # Bottom extension
    down = min(d, n - x)
 
    # Left extension
    left = min(d, y - 1)
 
    # Right extension
    right = min(d, m - y)
 
    # Calculating the cells
    # in each quadrilateral
    quad1 = top * left
    quad2 = left * down
    quad3 = down * right
    quad4 = right * top
 
    # Sum all of them to get
    # total cells in each
    # quadrilateral
    totalsq = (quad1 + quad2 +
               quad3 + quad4)
 
    # Add the singleblocks
    # along the lines of top,
    # down, left, right
    singleBlocks = (top + down +
                    left + right + 1)
 
    # Return the ans
    return totalsq + singleBlocks
 
 
# Driver Code
if __name__ == '__main__':
 
    # Dimensions of cell
    n = 10
    m = 10
 
    # Starting Coordinates
    x = 7
    y = 8
 
    # Number of Days
    d = 4
    d -= 1
 
    # Function Call
    print(solve(n, m, x, y, d))
 
# This code is contributed by Shivam Singh


C#




// C# implementation to find the
// total number of cells covered
// in a matrix after D days
using System;
class GFG {
 
    // Function to return the total
    // infected cells after d days
    static int solve(int n, int m, int x, int y, int d)
    {
 
        // Top extension
        int top = Math.Min(d, x - 1);
 
        // Bottom extension
        int down = Math.Min(d, n - x);
 
        // Left extension
        int left = Math.Min(d, y - 1);
 
        // Right extension
        int right = Math.Min(d, m - y);
 
        // Calculating the cells
        // in each quadrilateral
        int quad1 = top * left;
        int quad2 = left * down;
        int quad3 = down * right;
        int quad4 = right * top;
 
        // Sum all of them to get
        // total cells in each
        // quadrilateral
        int totalsq = quad1 + quad2 + quad3 + quad4;
 
        // Add the singleblocks
        // along the lines of top,
        // down, left, right
        int singleBlocks = top + down + left + right + 1;
 
        // Return the ans
        return totalsq + singleBlocks;
    }
 
    // Driver code
    public static void Main(String[] args)
    {
 
        // Dimensions of cell
        int n = 10, m = 10;
 
        // Starting coordinates
        int x = 7, y = 8;
 
        // Number of days
        int d = 4;
        d--;
 
        // Function call
        Console.WriteLine(solve(n, m, x, y, d));
    }
}
 
// This code is contributed by Rohit_ranjan


Javascript




<script>
 
// JavaScript 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
function solve(n, m, x, y, d)
{
     
    // Top extension
    let top = Math.min(d, x - 1);
 
    // Bottom extension
    let down = Math.min(d, n - x);
 
    // Left extension
    let left = Math.min(d, y - 1);
 
    // Right extension
    let right = Math.min(d, m - y);
 
    // Calculating the cells
    // in each quadrilateral
    let quad1 = top * left;
    let quad2 = left * down;
    let quad3 = down * right;
    let quad4 = right * top;
 
    // Sum all of them to get
    // total cells in each
    // quadrilateral
    let totalsq = quad1 + quad2 +
                  quad3 + quad4;
 
    // Add the singleblocks
    // along the lines of top,
    // down, left, right
    let singleBlocks = top + down +
                      left + right + 1;
 
    // Return the ans
    return totalsq + singleBlocks;
}
  
// Driver Code
 
    // Dimensions of cell
    let n = 10, m = 10;
 
    // Starting coordinates
    let x = 7, y = 8;
 
    // Number of days
    let d = 4;
    d--;
 
    // Function call
    document.write(solve(n, m, x, y, d));
  
 // This code is contributed by sanjoy_62.
</script>


Output

42

Time Complexity: O(1)
Auxiliary Space: O(1) 



Similar Reads

Total number of unit cells covered by all given Rectangles
Given a matrix A[][] consisting of coordinates of N rectangles such that {A[i][0], A[i][1]} representing bottom left coordinate of rectangle and {A[i][2], A[i][3]} representing top right coordinate of rectangle, the task is to find the total number of cells covered by all rectangles. Examples: Input: N = 2, A[][] = {{1, 1, 3, 3}, {2, 1, 4, 2}} Outp
8 min read
Total number of days taken to complete the task if after certain days one person leaves
Given that person A takes a days to do a certain piece of work while person B takes b days to do the same work. If A and B started the work together and n days before the completion of work, A leaves the work. Find the total number of days taken to complete work. Examples: Input: a = 10, b = 20, n = 5 Output: 10 Input: a = 5, b = 15, n = 3 Output:
3 min read
Count of cells in a matrix which give a Fibonacci number when the count of adjacent cells is added
Given an M x N matrix mat[][]. The task is to count the number of good cells in the matrix. A cell will be good if the sum of the cell value and the number of the adjacent cells is a Fibonacci number.Examples: Input: mat[][] = { {1, 2}, {3, 4}} Output: 2 Only the cells mat[0][0] and mat[1][0] are good. i.e. (1 + 2) = 3 and (3 + 2) = 5 are both Fibo
9 min read
Count of cells in a matrix whose adjacent cells's sum is prime Number
Given a M x N matrix mat[][], the task is to count the number of cells which have the sum of its adjacent cells equal to a prime number. For a cell x[i][j], only x[i+1][j], x[i-1][j], x[i][j+1] and x[i][j-1] are the adjacent cells.Examples: Input : mat[][] = {{1, 3}, {2, 5}} Output :2 Explanation: Only the cells mat[0][0] and mat[1][1] satisfying t
10 min read
Active and Inactive cells after k Days
Given a binary array of size n where n &gt; 3. A true (or 1) value in the array means active and false (or 0) means inactive. Given a number k, the task is to find count of active and inactive cells after k days. After every day, status of i'th cell becomes active if left and right cells are not same and inactive if left and right cell are same (bo
10 min read
Count cells in a grid from which maximum number of cells can be reached by K vertical or horizontal jumps
Given a matrix mat[][] of dimensions N*M and a positive integer K, the task is to find the number of cells in a grid from which maximum cells can be reached by K jumps in the vertical or horizontal direction. Examples: Input: N = 3, M = 3, K = 2Output: 4Explanation:The cells represented as X are the cells from which maximum cells (= 2) can be reach
8 min read
Program to convert given number of days in terms of Years, Weeks and Days
Given number of days, convert it in terms of Years, Week and Days. Examples : Input : 30 Output : years = 0 week = 4 days = 2 Input : 20 Output : years = 0 week = 2 days = 6 Approach : Number of years will be the quotient when number of days will be divided by 365 i.e days / 365 = years.Number of weeks will be the result of (Number_of_days % 365) /
5 min read
Maximum points covered after removing an Interval
Given N intervals in the form [l, r] and an integer Q. The task is to find the interval which when removed results in the coverage of the maximum number of points (Union of all the rest of the intervals). Note that all the given intervals cover numbers between 1 to Q only. Examples: Input: intervals[][] = {{1, 4}, {4, 5}, {5, 6}, {6, 7}, {3, 5}}, Q
9 min read
Distance covered after last breakpoint with at most K cost
Given an array arr[] of size N, and integers K and D, consisting of breakpoints in journey, the task is to find the distance covered after the last breakpoint with at most K cost following the given conditions: Starting point is 01 unit distance is covered for 1 cost.For moving D continuous distance there is an extra cost of D i.e., total cost beco
11 min read
Find Number of Even cells in a Zero Matrix after Q queries
Given a Zero matrix of N x N size, the task is to find the numbers of cells containing even numbers after performing Q queries. Each query will be in the form of (X, Y) such that for a given cell (X, Y), an increment operation has to be performed on all cells in the X'th row and Y'th column.Note: The initial 0's are also to be taken as even numbers
9 min read
Article Tags :
Practice Tags :