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.
Input: N = 5, M = 5, X = 1, Y = 3, D = 3
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
On making an N * M matrix and filling the adjacent cells for 4 days we will get 42 covered cells.
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:
Time Complexity: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Total number of unit cells covered by all given Rectangles
- Count of cells in a matrix which give a Fibonacci number when the count of adjacent cells is added
- Count of cells in a matrix whose adjacent cells's sum is prime Number
- Minimum Numbers of cells that are connected with the smallest path between 3 given cells
- Find Number of Even cells in a Zero Matrix after Q queries
- Construct a Matrix such that each cell consists of sum of adjacent elements of respective cells in given Matrix
- Find the maximum distance covered using n bikes
- Number of cells in the right and left diagonals passing through (x, y) in a matrix
- Number of cells in a matrix that satisfy the given condition
- Number of cells in matrix which are equidistant from given two points
- Min number of moves to traverse entire Matrix through connected cells with equal values
- Longest subarray of non-empty cells after removal of at most a single empty cell
- Check if cells numbered 1 to K in a grid can be connected after removal of atmost one blocked cell
- Print cells with same rectangular sums in a matrix
- Unique cells in a binary matrix
- Shortest distance between two cells in a matrix or grid
- Find safe cells in a matrix
- Size of all connected non-empty cells of a Matrix
- Minimum Distance from a given Cell to all other Cells of a Matrix
- Check if a given string can be formed using characters of adjacent cells of a Matrix
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.